Skip to content

Commit 41d7962

Browse files
committed
Explicitly compile the Docker image for Linux
1 parent a9c9bb2 commit 41d7962

File tree

1 file changed

+60
-17
lines changed

1 file changed

+60
-17
lines changed

template/default.nix

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,41 @@
11
{ sources ? import ./nix/sources.nix # managed by https://github.com/nmattia/niv
22
, nixpkgs ? sources.nixpkgs
3-
, pkgs ? import nixpkgs {}
4-
, cargo ? import ./Cargo.nix {
5-
inherit nixpkgs pkgs; release = false;
3+
, overlays ? [ (self: super: {
4+
# fakeroot (used for building the Docker image) seems to freeze or crash
5+
# on Darwin (macOS), but doesn't seem to actually be necessary beyond
6+
# production hardening.
7+
fakeroot =
8+
if self.buildPlatform.isDarwin then
9+
self.writeScriptBin "fakeroot" ''exec "$@"''
10+
else
11+
super.fakeroot;
12+
}) ]
13+
# When cross-/remote-building, some binaries still need to run on the local machine instead
14+
# (non-Nix build tools like Tilt, as well as the container composition scripts)
15+
, pkgsLocal ? import nixpkgs { inherit overlays; }
16+
# Default to building for the local CPU architecture
17+
, targetArch ? pkgsLocal.hostPlatform.linuxArch
18+
, targetSystem ? "${targetArch}-unknown-linux-gnu"
19+
, pkgsTarget ? import nixpkgs {
20+
inherit overlays;
21+
22+
# Build our containers for Linux for the local CPU architecture
23+
# A remote Linux builder can be set up using https://github.com/stackabletech/nix-docker-builder
24+
system = targetSystem;
25+
26+
# Currently using remote builders rather than cross-compilation,
27+
# because the latter requires us to recompile the world several times
28+
# just to get the full cross-toolchain up and running.
29+
# (Or I (@nightkr) am just dumb and missing something obvious.)
30+
# If uncommenting this, make sure to comment the `system =` clause above.
31+
#crossSystem = { config = targetSystem; };
32+
}
33+
, cargo ? import ./Cargo.nix rec {
34+
inherit nixpkgs;
35+
pkgs = pkgsTarget;
36+
# We're only using this for dev builds at the moment,
37+
# so don't pay for release optimization.
38+
release = false;
639
defaultCrateOverrides = pkgs.defaultCrateOverrides // {
740
prost-build = attrs: {
841
buildInputs = [ pkgs.protobuf ];
@@ -39,27 +72,37 @@
3972
};
4073
};
4174
}
42-
, meta ? pkgs.lib.importJSON ./nix/meta.json
75+
, meta ? pkgsLocal.lib.importJSON ./nix/meta.json
4376
, dockerName ? "oci.stackable.tech/sandbox/${meta.operator.name}"
4477
, dockerTag ? null
4578
}:
4679
rec {
47-
inherit cargo sources pkgs meta;
80+
inherit cargo sources pkgsLocal pkgsTarget meta;
81+
inherit (pkgsLocal) lib;
82+
pkgs = lib.warn "pkgs is not cross-compilation-aware, explicitly use either pkgsLocal or pkgsTarget" pkgsLocal;
4883
build = cargo.allWorkspaceMembers;
4984
entrypoint = build+"/bin/stackable-${meta.operator.name}";
50-
crds = pkgs.runCommand "${meta.operator.name}-crds.yaml" {}
85+
# Run crds in the target environment, to avoid compiling everything twice
86+
crds = pkgsTarget.runCommand "${meta.operator.name}-crds.yaml" {}
5187
''
5288
${entrypoint} crd > $out
5389
'';
5490

55-
dockerImage = pkgs.dockerTools.streamLayeredImage {
91+
# We're building the docker image *for* Linux, but we need to
92+
# build it in the local environment so that the generated load-image
93+
# can run locally.
94+
# That's still fine, as long as we only refer to pkgsTarget *inside* of the image.
95+
dockerImage = pkgsLocal.dockerTools.streamLayeredImage {
5696
name = dockerName;
5797
tag = dockerTag;
98+
#includeStorePaths = false;
5899
contents = [
59100
# Common debugging tools
60-
pkgs.bashInteractive pkgs.coreutils pkgs.util-linuxMinimal
101+
pkgsTarget.bashInteractive
102+
pkgsTarget.coreutils
103+
pkgsTarget.util-linuxMinimal
61104
# Kerberos 5 must be installed globally to load plugins correctly
62-
pkgs.krb5
105+
pkgsTarget.krb5
63106
# Make the whole cargo workspace available on $PATH
64107
build
65108
];
@@ -69,27 +112,27 @@ rec {
69112
fileRefVars = {
70113
PRODUCT_CONFIG = deploy/config-spec/properties.yaml;
71114
};
72-
in pkgs.lib.concatLists (pkgs.lib.mapAttrsToList (env: path: pkgs.lib.optional (pkgs.lib.pathExists path) "${env}=${path}") fileRefVars);
115+
in lib.concatLists (lib.mapAttrsToList (env: path: lib.optional (lib.pathExists path) "${env}=${path}") fileRefVars);
73116
Entrypoint = [ entrypoint ];
74117
Cmd = [ "run" ];
75118
};
76119
};
77-
docker = pkgs.linkFarm "listener-operator-docker" [
120+
docker = pkgsLocal.linkFarm "listener-operator-docker" [
78121
{
79122
name = "load-image";
80123
path = dockerImage;
81124
}
82125
{
83126
name = "ref";
84-
path = pkgs.writeText "${dockerImage.name}-image-tag" "${dockerImage.imageName}:${dockerImage.imageTag}";
127+
path = pkgsLocal.writeText "${dockerImage.name}-image-tag" "${dockerImage.imageName}:${dockerImage.imageTag}";
85128
}
86129
{
87130
name = "image-repo";
88-
path = pkgs.writeText "${dockerImage.name}-repo" dockerImage.imageName;
131+
path = pkgsLocal.writeText "${dockerImage.name}-repo" dockerImage.imageName;
89132
}
90133
{
91134
name = "image-tag";
92-
path = pkgs.writeText "${dockerImage.name}-tag" dockerImage.imageTag;
135+
path = pkgsLocal.writeText "${dockerImage.name}-tag" dockerImage.imageTag;
93136
}
94137
{
95138
name = "crds.yaml";
@@ -98,10 +141,10 @@ rec {
98141
];
99142

100143
# need to use vendored crate2nix because of https://github.com/kolloch/crate2nix/issues/264
101-
crate2nix = import sources.crate2nix {};
102-
tilt = pkgs.tilt;
144+
crate2nix = import sources.crate2nix { pkgs = pkgsLocal; };
145+
tilt = pkgsLocal.tilt;
103146

104-
regenerateNixLockfiles = pkgs.writeScriptBin "regenerate-nix-lockfiles"
147+
regenerateNixLockfiles = pkgsLocal.writeScriptBin "regenerate-nix-lockfiles"
105148
''
106149
#!/usr/bin/env bash
107150
set -euo pipefail

0 commit comments

Comments
 (0)