flake: link host code with mold on Linux; DECISIONS: mold, and no LTO

mold applies to the host target only, in the dev shell, the crane builds,
and the packaged binary (proc macros and build scripts of plugins). A
rebuild of the host test binary after a change takes 0.9 s (15 s with
GNU ld). Measured LTO: no gain worth 4 more minutes per release build.
This commit is contained in:
2026-10-23 10:20:39 +01:00
parent 9fb1e05686
commit ab1adbd5e3
2 changed files with 26 additions and 7 deletions
+19 -7
View File
@@ -25,7 +25,17 @@
# the native linker, even when the host itself is started with a bare
# PATH. Keep this same runtime path in the packaged host and exercise
# the installed binary below with PATH deliberately empty.
runtimePath = pkgs.lib.makeBinPath [ rust pkgs.stdenv.cc ];
# mold links host code much faster than GNU ld. Only on Linux, and only
# for the host target: plugins for wasm32-wasip2 link with rust-lld. Nix
# brings mold along, so a build without Nix keeps the default linker.
mold = pkgs.lib.optionals pkgs.stdenv.hostPlatform.isLinux [ pkgs.mold ];
hostTarget = pkgs.lib.toUpper (builtins.replaceStrings [ "-" ] [ "_" ]
pkgs.stdenv.hostPlatform.rust.rustcTarget);
moldVar = "CARGO_TARGET_${hostTarget}_RUSTFLAGS";
moldEnv = pkgs.lib.optionalAttrs pkgs.stdenv.hostPlatform.isLinux {
${moldVar} = "-C link-arg=-fuse-ld=mold";
};
runtimePath = pkgs.lib.makeBinPath ([ rust pkgs.stdenv.cc ] ++ mold);
# A WASI clang with wasi-libc, so that build scripts (the `cc` crate)
# can compile C for wasm32-wasip2. Its headers are WASI 0.1: C code
# that needs sockets does not build.
@@ -39,7 +49,8 @@
pname = "sausage";
version = "0.1.0";
src = ./.;
};
nativeBuildInputs = mold;
} // moldEnv;
# The test packages for `checks.tests`.
testPackages = "--workspace";
cargoArtifacts = craneLib.buildDepsOnly (common // {
@@ -55,13 +66,14 @@
doNotRemoveReferencesToRustToolchain = true;
cargoExtraArgs = "--locked -p sausage";
doCheck = false;
nativeBuildInputs = [ pkgs.makeWrapper ];
nativeBuildInputs = [ pkgs.makeWrapper ] ++ mold;
# The native `cargo` plugin runs cargo at runtime. Build scripts need a C linker,
# and C code for WASI needs the WASI clang.
postInstall = ''
wrapProgram $out/bin/sausage --prefix PATH : ${runtimePath} \
--set-default CC_wasm32_wasip2 ${wasiCcEnv.CC_wasm32_wasip2} \
--set-default AR_wasm32_wasip2 ${wasiCcEnv.AR_wasm32_wasip2}
--set-default AR_wasm32_wasip2 ${wasiCcEnv.AR_wasm32_wasip2} \
${pkgs.lib.optionalString pkgs.stdenv.hostPlatform.isLinux "--set-default ${moldVar} '-C link-arg=-fuse-ld=mold'"}
'';
meta.mainProgram = "sausage";
});
@@ -101,7 +113,7 @@
# Nothing builds on the test run's target folder: don't store it.
doInstallCargoArtifacts = false;
# The SDK tests run sample plugins with the wasmtime CLI.
nativeBuildInputs = [ pkgs.wasmtime ];
nativeBuildInputs = [ pkgs.wasmtime ] ++ mold;
});
# Exercise the installed host exactly as the loop launches it: the
# caller has no Cargo on PATH, so only the packaged runtime toolchain
@@ -191,8 +203,8 @@
apps.default = { type = "app"; program = "${sausage}/bin/sausage"; };
apps.loop = { type = "app"; program = "${loop}/bin/sausage-loop"; };
devShells.default = pkgs.mkShell (wasiCcEnv // {
packages = [ rust pkgs.wasmtime pkgs.wasm-tools ];
devShells.default = pkgs.mkShell (wasiCcEnv // moldEnv // {
packages = [ rust pkgs.wasmtime pkgs.wasm-tools ] ++ mold;
shellHook = ''
export CARGO_HOME="$PWD/.cargo-home"
'';