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 ] ;
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} :
4679rec {
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 ;
5898 contents = [
5999 # Common debugging tools
60- pkgs . bashInteractive pkgs . coreutils pkgs . util-linuxMinimal
100+ pkgsTarget . bashInteractive
101+ pkgsTarget . coreutils
102+ pkgsTarget . util-linuxMinimal
61103 # Kerberos 5 must be installed globally to load plugins correctly
62- pkgs . krb5
104+ pkgsTarget . krb5
63105 # Make the whole cargo workspace available on $PATH
64106 build
65107 ] ;
@@ -69,27 +111,27 @@ rec {
69111 fileRefVars = {
70112 PRODUCT_CONFIG = deploy/config-spec/properties.yaml ;
71113 } ;
72- in pkgs . lib . concatLists ( pkgs . lib . mapAttrsToList ( env : path : pkgs . lib . optional ( pkgs . lib . pathExists path ) "${ env } =${ path } " ) fileRefVars ) ;
114+ in lib . concatLists ( lib . mapAttrsToList ( env : path : lib . optional ( lib . pathExists path ) "${ env } =${ path } " ) fileRefVars ) ;
73115 Entrypoint = [ entrypoint ] ;
74116 Cmd = [ "run" ] ;
75117 } ;
76118 } ;
77- docker = pkgs . linkFarm "listener-operator -docker" [
119+ docker = pkgsLocal . linkFarm "${ dockerImage . name } -docker" [
78120 {
79121 name = "load-image" ;
80122 path = dockerImage ;
81123 }
82124 {
83125 name = "ref" ;
84- path = pkgs . writeText "${ dockerImage . name } -image-tag" "${ dockerImage . imageName } :${ dockerImage . imageTag } " ;
126+ path = pkgsLocal . writeText "${ dockerImage . name } -image-tag" "${ dockerImage . imageName } :${ dockerImage . imageTag } " ;
85127 }
86128 {
87129 name = "image-repo" ;
88- path = pkgs . writeText "${ dockerImage . name } -repo" dockerImage . imageName ;
130+ path = pkgsLocal . writeText "${ dockerImage . name } -repo" dockerImage . imageName ;
89131 }
90132 {
91133 name = "image-tag" ;
92- path = pkgs . writeText "${ dockerImage . name } -tag" dockerImage . imageTag ;
134+ path = pkgsLocal . writeText "${ dockerImage . name } -tag" dockerImage . imageTag ;
93135 }
94136 {
95137 name = "crds.yaml" ;
@@ -98,10 +140,10 @@ rec {
98140 ] ;
99141
100142 # need to use vendored crate2nix because of https://github.com/kolloch/crate2nix/issues/264
101- crate2nix = import sources . crate2nix { } ;
102- tilt = pkgs . tilt ;
143+ crate2nix = import sources . crate2nix { pkgs = pkgsLocal ; } ;
144+ tilt = pkgsLocal . tilt ;
103145
104- regenerateNixLockfiles = pkgs . writeScriptBin "regenerate-nix-lockfiles"
146+ regenerateNixLockfiles = pkgsLocal . writeScriptBin "regenerate-nix-lockfiles"
105147 ''
106148 #!/usr/bin/env bash
107149 set -euo pipefail
@@ -114,10 +156,6 @@ rec {
114156 # (see https://github.com/pre-commit/pre-commit-hooks?tab=readme-ov-file#trailing-whitespace).
115157 # So, remove the trailing newline already here to avoid that an
116158 # unnecessary change is shown in Git.
117- if [[ "$(uname)" == "Darwin" ]]; then
118- sed -i \"\" '$d' Cargo.nix
119- else
120- sed -i '$d' Cargo.nix
121- fi
159+ ${ pkgs . gnused } /bin/sed -i '$d' Cargo.nix
122160 '' ;
123161}
0 commit comments