Repository navigation
Description
Problem
On NixOS there is no /usr/lib. A locally linked binary gets the Nix store glibc as its interpreter, so nix-ld never applies, and it finds its shared libraries only through DT_RUNPATH. nixpkgs normally supplies that RUNPATH in its ld wrapper (ld-wrapper.sh): for each -L directory inside /nix/store that provides a shared library named by an -l flag, in -L order, it appends -rpath <dir>; a .so passed by path gets its directory added too; NIX_DONT_SET_RPATH=1 turns it off. That is why cc foo.c $(pkg-config --libs openssl) produces a runnable binary on NixOS.
A linker reached through -fuse-ld=<linker> never passes through that wrapper, so the rule is lost. rustc's default rust-lld (since 1.90, rust-lang/rust#140525) is a live example on this machine. The same crate, built through the same nixpkgs cc:
| Build | Linker | RUNPATH | ldd not found |
Run |
|---|---|---|---|---|
| default | LLD 22.1.2 (rust-lld) |
none | 3 (libssl.so.3, libcrypto.so.3, libsqlite3.so) |
exit 127 |
-Clinker-features=-lld -Clink-self-contained=-linker |
GNU ld via the wrapper | /nix/store/…-sqlite-3.51.2/lib:/nix/store/…-openssl-3.6.3/lib:… |
0 | exit 0 |
reld is selected the same way (-fuse-ld=reld; the ld script execs reld -flavor gnu), and its RUNPATH comes only from explicit -rpath arguments (rpath_set in crates/reld-core/src/args/elf.rs); there is no Nix-specific handling anywhere in the tree. So a reld link on NixOS should produce the same RUNPATH-less binary. That conclusion is from the code and the rust-lld result; it has not yet been reproduced with a reld build.
Proposal
Bake the ld-wrapper's rule into reld's ELF path so reld links correctly on NixOS without depending on a wrapper it bypasses:
- Enabled when targeting a dynamic ELF output and a Nix store is in use (
NIX_STORE, default/nix/store); disabled byNIX_DONT_SET_RPATH=1and for fully static links. - For every
-L<dir>/-L <dir>under the store, in command-line order, add<dir>to RUNPATH if it containslib<name>.sofor some-l<name>that was linked; add the directory of any store.sogiven by path. Each directory once. - Explicit
-rpathentries keep their position first; derived ones follow, matching the wrapper'sextraAfterplacement. - An explicit override (e.g.
--nix-rpath=auto|on|off) so it can be forced off in hermetic builds.
Acceptance
- A C link through
-fuse-ld=reldwith-L/nix/store/…-openssl-…/lib -lssl -lcryptoyields a RUNPATH containing that directory; the binary runs;lddreports nothing "not found". - A cargo build that uses reld as its linker against pkg-config-discovered openssl/sqlite runs without
LD_LIBRARY_PATH. - No RUNPATH is derived for non-store
-Ldirectories, withNIX_DONT_SET_RPATH=1, for static links, or on non-Nix hosts. - Output byte-identical to today when no store paths are involved.
Context
- System-side workaround currently used: zackees/nixos@91d74c1 bakes
-rpathfor a development library set into the gcc/clang wrappers'cc-ldflags, which reach any linker the cc wrapper calls. - Use lld by default on
x86_64-unknown-linux-gnustable rust-lang/rust#140525 (lld default onx86_64-unknown-linux-gnu); LLD linker does not have required nixpkgs wrapper script and flags NixOS/nixpkgs#24744 (lld has no nixpkgs wrapper).
Activity