36
36
# We're only using this for dev builds at the moment,
37
37
# so don't pay for release optimization.
38
38
release = false ;
39
- defaultCrateOverrides = pkgs . defaultCrateOverrides // {
40
- prost-build = attrs : {
41
- buildInputs = [ pkgs . protobuf ] ;
42
- } ;
43
- tonic-reflection = attrs : {
44
- buildInputs = [ pkgs . rustfmt ] ;
45
- } ;
46
- csi-grpc = attrs : {
47
- nativeBuildInputs = [ pkgs . protobuf ] ;
48
- } ;
49
- stackable-secret-operator = attrs : {
50
- buildInputs = [ pkgs . protobuf pkgs . rustfmt ] ;
51
- } ;
52
- stackable-opa-user-info-fetcher = attrs : {
53
- # TODO: why is this not pulled in via libgssapi-sys?
54
- buildInputs = [ pkgs . krb5 ] ;
55
- } ;
56
- krb5-sys = attrs : {
57
- nativeBuildInputs = [ pkgs . pkg-config ] ;
58
- buildInputs = [ pkgs . krb5 ] ;
59
- LIBCLANG_PATH = "${ pkgs . libclang . lib } /lib" ;
60
- # Clang's resource directory is located at ${pkgs.clang.cc.lib}/lib/clang/<version>.
61
- # Starting with Clang 16, only the major version is used for the resource directory,
62
- # whereas the full version was used in prior Clang versions (see
63
- # https://github.com/llvm/llvm-project/commit/e1b88c8a09be25b86b13f98755a9bd744b4dbf14).
64
- # The clang wrapper ${pkgs.clang} provides a symlink to the resource directory, which
65
- # we use instead.
66
- BINDGEN_EXTRA_CLANG_ARGS = "-I${ pkgs . glibc . dev } /include -I${ pkgs . clang } /resource-root/include" ;
67
- } ;
68
- libgssapi-sys = attrs : {
69
- buildInputs = [ pkgs . krb5 ] ;
70
- LIBCLANG_PATH = "${ pkgs . libclang . lib } /lib" ;
71
- BINDGEN_EXTRA_CLANG_ARGS = "-I${ pkgs . glibc . dev } /include -I${ pkgs . clang } /resource-root/include" ;
39
+
40
+ buildRustCrateForPkgs = pkgs : attrs : pkgs . buildRustCrate . override {
41
+ # Consider migrating to mold for faster linking, but in my (@nightkr's)
42
+ # quick testing so far it actually seems to perform slightly worse than
43
+ # the default one.
44
+ # stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv;
45
+
46
+ defaultCrateOverrides = pkgs . defaultCrateOverrides // {
47
+ # Attributes applied here apply to a single crate
48
+
49
+ prost-build = attrs : {
50
+ buildInputs = [ pkgs . protobuf ] ;
51
+ } ;
52
+ tonic-reflection = attrs : {
53
+ buildInputs = [ pkgs . rustfmt ] ;
54
+ } ;
55
+ csi-grpc = attrs : {
56
+ nativeBuildInputs = [ pkgs . protobuf ] ;
57
+ } ;
58
+ stackable-secret-operator = attrs : {
59
+ buildInputs = [ pkgs . protobuf pkgs . rustfmt ] ;
60
+ } ;
61
+ stackable-opa-user-info-fetcher = attrs : {
62
+ # TODO: why is this not pulled in via libgssapi-sys?
63
+ buildInputs = [ pkgs . krb5 ] ;
64
+ } ;
65
+ krb5-sys = attrs : {
66
+ nativeBuildInputs = [ pkgs . pkg-config ] ;
67
+ buildInputs = [ pkgs . krb5 ] ;
68
+ LIBCLANG_PATH = "${ pkgs . libclang . lib } /lib" ;
69
+ # Clang's resource directory is located at ${pkgs.clang.cc.lib}/lib/clang/<version>.
70
+ # Starting with Clang 16, only the major version is used for the resource directory,
71
+ # whereas the full version was used in prior Clang versions (see
72
+ # https://github.com/llvm/llvm-project/commit/e1b88c8a09be25b86b13f98755a9bd744b4dbf14).
73
+ # The clang wrapper ${pkgs.clang} provides a symlink to the resource directory, which
74
+ # we use instead.
75
+ BINDGEN_EXTRA_CLANG_ARGS = "-I${ pkgs . glibc . dev } /include -I${ pkgs . clang } /resource-root/include" ;
76
+ } ;
77
+ libgssapi-sys = attrs : {
78
+ buildInputs = [ pkgs . krb5 ] ;
79
+ LIBCLANG_PATH = "${ pkgs . libclang . lib } /lib" ;
80
+ BINDGEN_EXTRA_CLANG_ARGS = "-I${ pkgs . glibc . dev } /include -I${ pkgs . clang } /resource-root/include" ;
81
+ } ;
72
82
} ;
73
- } ;
83
+ } ( attrs // {
84
+ # Attributes applied here apply to all built crates
85
+ # Note that these *take precedence over* per-crate overrides
86
+
87
+ dontStrip = ! strip ;
88
+
89
+ extraRustcOpts = [
90
+ "-C debuginfo=${ toString debuginfo } "
91
+ # Enabling optimization shrinks the binaries further, but also *vastly*
92
+ # increases the build time.
93
+ # "-C opt-level=3"
94
+ ] ++ attrs . extraRustcOpts ;
95
+
96
+ # Parallel codegen allows Rustc to use more cores.
97
+ # This should help speed up compiling "bottleneck" crates that Nix can't
98
+ # parallelize (like the operator binary itself).
99
+ codegenUnits = 32 ;
100
+ } ) ;
74
101
}
75
102
, meta ? pkgsLocal . lib . importJSON ./nix/meta.json
76
103
, dockerName ? "oci.stackable.tech/sandbox/${ meta . operator . name } "
77
104
, dockerTag ? null
105
+ # Controls the amount of debug information included in the built operator binaries,
106
+ # see https://doc.rust-lang.org/rustc/codegen-options/index.html#debuginfo
107
+ # For comparison, `cargo build --release` defaults to 0, and the debug profile
108
+ # (no `--release`) defaults to 2.
109
+ # see https://doc.rust-lang.org/cargo/reference/profiles.html#debug
110
+ # Set to 2 if you want to run a debugger, but note that it bloats the Docker
111
+ # images *significantly* (hundreds of megabytes).
112
+ , debuginfo ? 0
113
+ # Strip operator binaries if we don't include debuginfo, because *something*
114
+ # still something still includes a reference to gcc (~230MiB), causing it to be
115
+ # added to the docker images.
116
+ , strip ? if debuginfo == 0 then true else false
117
+ # We normally don't include a shell in the (dev) operator images, but it can be
118
+ # enabled by enabling this flag.
119
+ # TODO(@nightkr): Re-enabled for now, since some operators ship with bash init
120
+ # scripts (like secret-operator's CSI path migration job). Consider either
121
+ # removing them or integrating them into the main operator binary instead.
122
+ , includeShell ? true
78
123
} :
79
124
rec {
80
125
inherit cargo sources pkgsLocal pkgsTarget meta ;
@@ -96,14 +141,14 @@ rec {
96
141
name = dockerName ;
97
142
tag = dockerTag ;
98
143
contents = [
99
- # Common debugging tools
100
- pkgsTarget . bashInteractive
101
- pkgsTarget . coreutils
102
- pkgsTarget . util-linuxMinimal
103
144
# Kerberos 5 must be installed globally to load plugins correctly
104
145
pkgsTarget . krb5
105
146
# Make the whole cargo workspace available on $PATH
106
147
build
148
+ ] ++ lib . optional includeShell [
149
+ pkgsTarget . bashInteractive
150
+ pkgsTarget . coreutils
151
+ pkgsTarget . util-linuxMinimal
107
152
] ;
108
153
config = {
109
154
Env =
@@ -156,6 +201,6 @@ rec {
156
201
# (see https://github.com/pre-commit/pre-commit-hooks?tab=readme-ov-file#trailing-whitespace).
157
202
# So, remove the trailing newline already here to avoid that an
158
203
# unnecessary change is shown in Git.
159
- ${ pkgs . gnused } /bin/sed -i '$d' Cargo.nix
204
+ ${ pkgsLocal . gnused } /bin/sed -i '$d' Cargo.nix
160
205
'' ;
161
206
}
0 commit comments