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 ;
98
+ #includeStorePaths = false;
58
99
contents = [
59
100
# Common debugging tools
60
- pkgs . bashInteractive pkgs . coreutils pkgs . util-linuxMinimal
101
+ pkgsTarget . bashInteractive
102
+ pkgsTarget . coreutils
103
+ pkgsTarget . util-linuxMinimal
61
104
# Kerberos 5 must be installed globally to load plugins correctly
62
- pkgs . krb5
105
+ pkgsTarget . krb5
63
106
# Make the whole cargo workspace available on $PATH
64
107
build
65
108
] ;
@@ -69,27 +112,27 @@ rec {
69
112
fileRefVars = {
70
113
PRODUCT_CONFIG = deploy/config-spec/properties.yaml ;
71
114
} ;
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 ) ;
73
116
Entrypoint = [ entrypoint ] ;
74
117
Cmd = [ "run" ] ;
75
118
} ;
76
119
} ;
77
- docker = pkgs . linkFarm "listener-operator-docker" [
120
+ docker = pkgsLocal . linkFarm "listener-operator-docker" [
78
121
{
79
122
name = "load-image" ;
80
123
path = dockerImage ;
81
124
}
82
125
{
83
126
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 } " ;
85
128
}
86
129
{
87
130
name = "image-repo" ;
88
- path = pkgs . writeText "${ dockerImage . name } -repo" dockerImage . imageName ;
131
+ path = pkgsLocal . writeText "${ dockerImage . name } -repo" dockerImage . imageName ;
89
132
}
90
133
{
91
134
name = "image-tag" ;
92
- path = pkgs . writeText "${ dockerImage . name } -tag" dockerImage . imageTag ;
135
+ path = pkgsLocal . writeText "${ dockerImage . name } -tag" dockerImage . imageTag ;
93
136
}
94
137
{
95
138
name = "crds.yaml" ;
@@ -98,10 +141,10 @@ rec {
98
141
] ;
99
142
100
143
# 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 ;
103
146
104
- regenerateNixLockfiles = pkgs . writeScriptBin "regenerate-nix-lockfiles"
147
+ regenerateNixLockfiles = pkgsLocal . writeScriptBin "regenerate-nix-lockfiles"
105
148
''
106
149
#!/usr/bin/env bash
107
150
set -euo pipefail
0 commit comments