1
1
{ sources ? import ./nix/sources.nix # managed by https://github.com/nmattia/niv
2
2
, 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 ;
6
39
defaultCrateOverrides = pkgs . defaultCrateOverrides // {
7
40
prost-build = attrs : {
8
41
buildInputs = [ pkgs . protobuf ] ;
39
72
} ;
40
73
} ;
41
74
}
42
- , meta ? pkgs . lib . importJSON ./nix/meta.json
75
+ , meta ? pkgsLocal . lib . importJSON ./nix/meta.json
43
76
, dockerName ? "oci.stackable.tech/sandbox/${ meta . operator . name } "
44
77
, dockerTag ? null
45
78
} :
46
79
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 ;
48
83
build = cargo . allWorkspaceMembers ;
49
84
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" { }
51
87
''
52
88
${ entrypoint } crd > $out
53
89
'' ;
54
90
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 {
56
96
name = dockerName ;
57
97
tag = dockerTag ;
58
98
contents = [
59
99
# Common debugging tools
60
- pkgs . bashInteractive pkgs . coreutils pkgs . util-linuxMinimal
100
+ pkgsTarget . bashInteractive
101
+ pkgsTarget . coreutils
102
+ pkgsTarget . util-linuxMinimal
61
103
# Kerberos 5 must be installed globally to load plugins correctly
62
- pkgs . krb5
104
+ pkgsTarget . krb5
63
105
# Make the whole cargo workspace available on $PATH
64
106
build
65
107
] ;
@@ -69,27 +111,27 @@ rec {
69
111
fileRefVars = {
70
112
PRODUCT_CONFIG = deploy/config-spec/properties.yaml ;
71
113
} ;
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 ) ;
73
115
Entrypoint = [ entrypoint ] ;
74
116
Cmd = [ "run" ] ;
75
117
} ;
76
118
} ;
77
- docker = pkgs . linkFarm "listener-operator -docker" [
119
+ docker = pkgsLocal . linkFarm "${ dockerImage . name } -docker" [
78
120
{
79
121
name = "load-image" ;
80
122
path = dockerImage ;
81
123
}
82
124
{
83
125
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 } " ;
85
127
}
86
128
{
87
129
name = "image-repo" ;
88
- path = pkgs . writeText "${ dockerImage . name } -repo" dockerImage . imageName ;
130
+ path = pkgsLocal . writeText "${ dockerImage . name } -repo" dockerImage . imageName ;
89
131
}
90
132
{
91
133
name = "image-tag" ;
92
- path = pkgs . writeText "${ dockerImage . name } -tag" dockerImage . imageTag ;
134
+ path = pkgsLocal . writeText "${ dockerImage . name } -tag" dockerImage . imageTag ;
93
135
}
94
136
{
95
137
name = "crds.yaml" ;
@@ -98,10 +140,10 @@ rec {
98
140
] ;
99
141
100
142
# 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 ;
103
145
104
- regenerateNixLockfiles = pkgs . writeScriptBin "regenerate-nix-lockfiles"
146
+ regenerateNixLockfiles = pkgsLocal . writeScriptBin "regenerate-nix-lockfiles"
105
147
''
106
148
#!/usr/bin/env bash
107
149
set -euo pipefail
@@ -114,10 +156,6 @@ rec {
114
156
# (see https://github.com/pre-commit/pre-commit-hooks?tab=readme-ov-file#trailing-whitespace).
115
157
# So, remove the trailing newline already here to avoid that an
116
158
# 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
122
160
'' ;
123
161
}
0 commit comments