buildRustCrate: remap rustc store path to break rust-src closure leak

When the rust-src component is installed (common with rust-overlay
toolchains via rust-toolchain.toml), rustc unvirtualises libstd source
paths. Panic locations from monomorphised generic std code
(BTreeMap, VecDeque, sync primitives, etc.) then embed the toolchain
store path in .rodata, pulling the entire multi-GB toolchain into the
runtime closure.

This is not an RPATH issue — stdenv's patchelf --shrink-rpath already
cleans that. The reference is a core::panic::Location string.

Remap the rustc store path to /rustc so panic messages stay readable
but no longer create a store-path dependency. This is a no-op for
toolchains without rust-src (the prefix simply never matches).
This commit is contained in:
Bernardo Meurer Costa
2026-03-23 16:39:28 +00:00
parent 9a66659266
commit e85060f58c
@@ -31,6 +31,12 @@ let
(if release then "-C opt-level=3" else "-C debuginfo=2")
"-C codegen-units=${toString codegenUnits}"
"--remap-path-prefix=$NIX_BUILD_TOP=/"
# When the rust-src component is present (common with rust-overlay
# toolchains), rustc unvirtualises libstd source paths. Panic
# locations from monomorphised generic std code then embed the
# toolchain store path in .rodata, pulling the entire toolchain into
# the closure. Remap to a stable placeholder to break the reference.
"--remap-path-prefix=${rustc}=/rustc"
(mkRustcDepArgs dependencies crateRenames)
(mkRustcFeatureArgs crateFeatures)
]