Skip to content

ELF on NixOS: add RUNPATH for Nix-store -L directories, as nixpkgs' ld-wrapper does #110

Closed

Description

@zackees

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 by NIX_DONT_SET_RPATH=1 and for fully static links.
  • For every -L<dir> / -L <dir> under the store, in command-line order, add <dir> to RUNPATH if it contains lib<name>.so for some -l<name> that was linked; add the directory of any store .so given by path. Each directory once.
  • Explicit -rpath entries keep their position first; derived ones follow, matching the wrapper's extraAfter placement.
  • 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=reld with -L/nix/store/…-openssl-…/lib -lssl -lcrypto yields a RUNPATH containing that directory; the binary runs; ldd reports 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 -L directories, with NIX_DONT_SET_RPATH=1, for static links, or on non-Nix hosts.
  • Output byte-identical to today when no store paths are involved.

Context

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions