Skip to content

feat(elf): derive RUNPATH for Nix-store -L directories - #111

Merged
zackees merged 2 commits into
mainfrom
nix-rpath
Sep 15, 2026
Merged

zackees merged 2 commits into
mainfrom
nix-rpath

Conversation

@zackees

@zackees zackees commented Sep 15, 2026

Copy link
Copy Markdown
Collaborator

Summary

Implements reld's half of #110: bake nixpkgs' ld-wrapper.sh RUNPATH rule into reld's ELF path, so a binary linked through -fuse-ld=reld on NixOS gets the same DT_RUNPATH it would get from the ld wrapper — without depending on that wrapper (which -fuse-ld bypasses).

Behavior

When targeting a dynamic ELF output and a Nix store is in use (NIX_STORE, default /nix/store):

  • For every -L <dir> under the store, in command-line order, add <dir> to RUNPATH if it provides lib<name>.so for a linked -l<name>.
  • Add the directory of any store .so given by path.
  • Each directory is added once.
  • Explicit -rpath entries keep their position first; derived entries follow (matching the wrapper's extraAfter placement).
  • Disabled by NIX_DONT_SET_RPATH=1, and overridable with --nix-rpath=auto|on|off (for hermetic builds).

When no store paths are involved, nothing changes — output is byte-identical to today. Static (non-PIE) and relocatable outputs never emit a dynamic section, so no RUNPATH is written for them regardless.

Implementation

crates/reld-core/src/args/elf.rs:

  • New NixRpathMode { Auto, On, Off } and --nix-rpath flag.
  • ElfArgs::add_nix_rpath_entries reads NIX_STORE / NIX_DONT_SET_RPATH and appends derived entries to rpath_set after explicit -rpath entries. The derivation is split into add_nix_rpath_entries_with so it can be exercised without touching the environment.

Tests

  • 10 unit tests in args/elf.rs covering: store-dir derivation, non-store skip, no-lib skip, NIX_DONT_SET_RPATH, on/off modes, direct .so path, explicit-first ordering, dedup, and flag parsing.
  • 3 end-to-end tests (crates/reld/tests/nix_rpath.rs) that compile a real shared lib with cc, link it with reld under a fake NIX_STORE, and assert DT_RUNPATH via the object crate: derivation, NIX_DONT_SET_RPATH=1 disables, and non-store dir yields no RUNPATH.

Verified end-to-end on this machine (NixOS): NIX_STORE=$dir reld -shared -o out.so main.o -L $dir/lib -lfoo emits DT_RUNPATH=[$dir/lib] and ldd resolves libfoo.so.

Closes #110.

🤖 Generated with Claude Code

zackees and others added 2 commits September 15, 2026 11:28
Mirror nixpkgs' ld-wrapper.sh so a linker reached through -fuse-ld
produces the same RUNPATH on NixOS. For each -L directory under the Nix
store (NIX_STORE, default /nix/store) that provides a shared library
named by a linked -l flag, append it to RUNPATH after any explicit
-rpath entries. The directory of a store .so passed by path is also
added. Disabled by NIX_DONT_SET_RPATH=1 and overridable with
--nix-rpath=auto|on|off.

Closes #110.

Co-Authored-By: Claude Code <[email protected]>
The test builds ELF shared objects with cc, which only works on Linux; gate it so macOS/Windows --all-targets builds compile it out.

Co-Authored-By: Claude Code <[email protected]>
@zackees
zackees merged commit 6840aab into main Sep 15, 2026
17 of 25 checks passed
@zackees
zackees deleted the nix-rpath branch November 5, 2026 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant