Skip to content

Commit aa9aa60

Browse files
authored
Slim down Nix builds (#530)
* Disable debug info for Nix builds For example, this slims down the airflow-operator image from 615MiB to 147MiB. This improvement is a combination of the size of the debuginfo itself (~200MiB per binary), and not having to include build dependencies just because our debuginfo refers to them (primarily, this ended up pulling in GCC, which sits at ~260MiB just on its own). * Also remove the shell (by default) * Fix a remote build awareness warning * Re-enable shell for now
1 parent e62e8ac commit aa9aa60

File tree

1 file changed

+84
-39
lines changed

1 file changed

+84
-39
lines changed

template/default.nix

Lines changed: 84 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,45 +36,90 @@
3636
# We're only using this for dev builds at the moment,
3737
# so don't pay for release optimization.
3838
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+
};
7282
};
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+
});
74101
}
75102
, meta ? pkgsLocal.lib.importJSON ./nix/meta.json
76103
, dockerName ? "oci.stackable.tech/sandbox/${meta.operator.name}"
77104
, 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
78123
}:
79124
rec {
80125
inherit cargo sources pkgsLocal pkgsTarget meta;
@@ -96,14 +141,14 @@ rec {
96141
name = dockerName;
97142
tag = dockerTag;
98143
contents = [
99-
# Common debugging tools
100-
pkgsTarget.bashInteractive
101-
pkgsTarget.coreutils
102-
pkgsTarget.util-linuxMinimal
103144
# Kerberos 5 must be installed globally to load plugins correctly
104145
pkgsTarget.krb5
105146
# Make the whole cargo workspace available on $PATH
106147
build
148+
] ++ lib.optional includeShell [
149+
pkgsTarget.bashInteractive
150+
pkgsTarget.coreutils
151+
pkgsTarget.util-linuxMinimal
107152
];
108153
config = {
109154
Env =
@@ -156,6 +201,6 @@ rec {
156201
# (see https://github.com/pre-commit/pre-commit-hooks?tab=readme-ov-file#trailing-whitespace).
157202
# So, remove the trailing newline already here to avoid that an
158203
# unnecessary change is shown in Git.
159-
${pkgs.gnused}/bin/sed -i '$d' Cargo.nix
204+
${pkgsLocal.gnused}/bin/sed -i '$d' Cargo.nix
160205
'';
161206
}

0 commit comments

Comments
 (0)