Skip to content

Commit 91d74c1

Browse files
zackeesclaude
andcommitted
Give the system compilers a standard development library set
OpenSSL could not be found outside a dev shell: pkg-config listed zero modules, there is no /usr/include, and a cargo build that did find openssl through a hand-set PKG_CONFIG_PATH produced a binary that would not start ("libssl.so.3: cannot open shared object file"). bosn's wheel backend runs ldd on its CLI to bundle OpenSSL and failed on exactly that. The second half is the counter-intuitive part. Nix's ld wrapper writes a RUNPATH for every store -L, which is why C links have always just worked, but rustc 1.95 links with its bundled rust-lld and never goes through that wrapper -- readelf -p .comment on the output says "Linker: LLD 22.1.2". So the library set is baked into the system gcc and clang wrappers as -idirafter, -L and an explicit -rpath: cc-ldflags reach whatever linker the cc wrapper calls, rust-lld included. PKG_CONFIG_PATH and LIBCLANG_PATH point at the same environment, and protoc is added for prost-build. The list is what the Cargo.lock files under ~/dev actually link, not a guess. A global LD_LIBRARY_PATH was rejected: it outranks every Nix program's own RUNPATH and would feed these versions to apps built from other nixpkgs revisions. Dev shells are unaffected -- they bring their own cc wrapper, and their pkg-config wins over the global path. Verified against the built generation: gcc and clang bare links, an openssl-sys + libz-sys + libsqlite3-sys crate and a clang-sys crate all get a RUNPATH, show nothing "not found" in ldd, and run; the same Rust crate exited 127 without this. pkg-config resolves openssl, gtk+-3.0, webkit2gtk-4.1 and ayatana-appindicator3. Closure grows about 0.1 GiB. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01Dg1Yhhff4kjhimDyLsSdBx
1 parent b3848ca commit 91d74c1

2 files changed

Lines changed: 122 additions & 9 deletions

File tree

‎AGENTS.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,18 @@ it `pip install` reports success and the *import* is what breaks, so nix-ld
330330
and the venv are one setting in two places. Test a change to either with an
331331
actual `import`, not with a successful install.
332332

333+
**A Rust binary built here can link fine and still not find its libraries.**
334+
Nix's ld wrapper writes a RUNPATH for each store `-L`, which is why C links
335+
against a library just work. rustc does not go through it: it links with its
336+
bundled rust-lld (`readelf -p .comment` says `Linker: LLD`), so a cargo build
337+
against openssl produced a binary with no RUNPATH that failed to start, and
338+
`ldd` reported `libssl.so.3 => not found` -- the failure bosn's wheel build
339+
hit. The fix is the `-rpath` that `withDevLibraries` bakes into the system gcc
340+
and clang wrappers, which reaches whatever linker the cc wrapper calls. Do not
341+
"fix" it with a global `LD_LIBRARY_PATH`; the `devLibraryEnv` comment in
342+
`system/configuration.nix` says why. Judge a built binary by
343+
`readelf -d <bin> | grep RUNPATH`, not by whether the build succeeded.
344+
333345
**A package in `systemPackages` whose own test suite fails takes the whole
334346
rebuild down.** `pipx` 1.8.0 on the 26.05 channel fails seven assertions in
335347
`tests/test_package_specifier.py` -- `packaging` >= 24 normalises `black@ url`

‎system/configuration.nix‎

Lines changed: 110 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,87 @@ let
454454
legacySonameShims
455455
];
456456

457+
# ── A standard set of development libraries ──
458+
#
459+
# The NixOS answer to what `apt install libssl-dev` and friends give Ubuntu
460+
# in /usr/include and /usr/lib. The list is not a guess: it is what the
461+
# Cargo.lock files under ~/dev actually link (openssl-sys, libz-sys,
462+
# libgit2-sys, libssh2-sys, zstd-sys, lzma-sys, libsqlite3-sys, bzip2-sys,
463+
# the webkit2gtk-sys stack for Tauri, clang-sys, libdbus-sys, alsa-sys and
464+
# the appindicator pair), plus the usual C set a Python or autotools build
465+
# reaches for.
466+
#
467+
# It reaches a build by two routes, and both are needed:
468+
#
469+
# - PKG_CONFIG_PATH (sessionVariables, further down), which is how every
470+
# -sys crate and most C build systems find a library at all.
471+
# - The system gcc and clang wrappers (`withDevLibraries` below), for
472+
# headers, -L, and -- the part that matters -- an rpath.
473+
#
474+
# The rpath is the non-obvious half. A binary linked here gets the store
475+
# glibc as its loader, so nix-ld never sees it, and it finds libraries only
476+
# through its RUNPATH. Nix's ld wrapper normally writes that for each -L in
477+
# the store, which is why `cc foo.c $(pkg-config --libs openssl)` has always
478+
# worked. But rustc (1.95 here) links with its bundled rust-lld, which never
479+
# goes through the ld wrapper -- `readelf -p .comment` on the output reads
480+
# "Linker: LLD 22.1.2". So cargo found openssl, linked it, and produced a
481+
# binary with no RUNPATH that dies on "libssl.so.3: cannot open shared
482+
# object file", with ldd agreeing. bosn's wheel backend runs ldd to bundle
483+
# OpenSSL and failed on exactly that. cc-ldflags reach whatever linker the
484+
# cc wrapper hands off to, rust-lld included. Verified: an openssl-sys +
485+
# libz-sys + libsqlite3-sys test crate runs with this and exits 127 without.
486+
#
487+
# Only the system compilers carry it. A `nix develop` / direnv shell puts
488+
# its own cc wrapper first on PATH and is untouched. Headers go in with
489+
# -idirafter, after glibc's and after any -I or -isystem a build passes, so
490+
# this set fills a gap and never shadows a project's own copy (checked: no
491+
# header here shares a path with glibc's or gcc's include-fixed).
492+
#
493+
# A binary built this way holds a RUNPATH into this environment's store path,
494+
# so once the list changes and the old generation is garbage-collected it
495+
# loses its libraries like any Nix-built binary would. Rebuild it.
496+
#
497+
# Deliberately NOT done: a global LD_LIBRARY_PATH. It outranks every Nix
498+
# program's own RUNPATH, so it would feed these exact library versions to
499+
# apps built from other nixpkgs revisions -- the hermes flake input, the user
500+
# nix profile -- and GTK is on this list. gcc_multi is not wrapped either;
501+
# these are 64-bit libraries with no business in a -m32 link.
502+
devLibraries = with pkgs; [
503+
# Crypto, compression and the network stack.
504+
openssl zlib zstd xz bzip2 lz4 brotli libssh2 curl
505+
# Version control, databases, parsers.
506+
libgit2 sqlite libpq libxml2 libxslt libyaml expat pcre2 icu
507+
# What Python, Ruby and autotools builds want from the C side.
508+
libffi readline ncurses gmp libuv libevent libunwind elfutils
509+
# Linux system interfaces.
510+
util-linux systemdLibs dbus libseccomp libcap alsa-lib libusb1 hidapi
511+
# Graphics and the Tauri / WebKitGTK stack. The appindicator pair is two
512+
# entries because ayatana-appindicator3's .pc requires ayatana-indicator3,
513+
# and pkg-config fails the whole query when a Requires is missing.
514+
fontconfig freetype harfbuzz libGL libdrm libxkbcommon wayland
515+
glib gtk3 cairo pango atk gdk-pixbuf webkitgtk_4_1 libsoup_3
516+
libayatana-appindicator libayatana-indicator
517+
# libclang for clang-sys / bindgen; LIBCLANG_PATH points into this.
518+
libclang
519+
];
520+
521+
devLibraryEnv = pkgs.buildEnv {
522+
name = "dev-libraries";
523+
# Headers and .pc files live in `dev`, the shared objects in `lib` or
524+
# `out` depending on the package; get* picks whichever exists. Listing
525+
# the packages bare would miss both -- openssl's default install outputs
526+
# are `bin` and `man`.
527+
paths = map lib.getDev devLibraries ++ map lib.getLib devLibraries;
528+
pathsToLink = [ "/include" "/lib" "/share/pkgconfig" ];
529+
};
530+
531+
withDevLibraries = cc: cc.override (old: {
532+
extraBuildCommands = (old.extraBuildCommands or "") + ''
533+
echo "-idirafter ${devLibraryEnv}/include" >> $out/nix-support/cc-cflags
534+
echo "-L${devLibraryEnv}/lib -rpath ${devLibraryEnv}/lib" >> $out/nix-support/cc-ldflags
535+
'';
536+
});
537+
457538
in
458539
{
459540
# home-manager's NixOS module is added by flake.nix, alongside this file.
@@ -773,21 +854,25 @@ in
773854
# lowPrio makes gcc the deterministic winner; `clang` and `clang++` are
774855
# unique names and are unaffected.
775856
#
776-
# These compile self-contained code and nothing else. There is no
777-
# /usr/include on NixOS, so anything that wants zlib, openssl or any other
778-
# library's headers still needs a `shell.nix` / `nix develop` (direnv is
779-
# enabled below) -- the nix-ld library list above puts shared objects on a
780-
# *runtime* search path and deliberately does not reach the compiler.
781-
# pkg-config is here for the same reason and with the same caveat: it
782-
# finds nothing until a dev shell populates PKG_CONFIG_PATH.
783-
gcc
784-
(lib.lowPrio clang)
857+
# Both carry the development library set (`devLibraryEnv` in the let
858+
# block): its headers, its libraries and an rpath to them, so openssl,
859+
# zlib, sqlite and the rest compile, link and RUN without a dev shell, and
860+
# pkg-config finds them through the PKG_CONFIG_PATH set further down. A
861+
# `shell.nix` / `nix develop` (direnv is enabled below) still wins inside
862+
# a project, with its own compiler and its own versions. The nix-ld list
863+
# above is a different thing: it serves prebuilt foreign binaries at run
864+
# time and never reaches the compiler.
865+
(withDevLibraries gcc)
866+
(lib.lowPrio (withDevLibraries clang))
785867
clang-tools # clangd, clang-format, clang-tidy; not in `clang`
786868
binutils # ld, as, ar, nm, objdump, readelf, strings
787869
gnumake
788870
cmake
789871
ninja
790872
pkg-config
873+
# protoc. prost-build -- in 57 Cargo.lock entries under ~/dev -- shells
874+
# out to it from its build script and fails the build without it.
875+
protobuf
791876
autoconf
792877
automake
793878
libtool
@@ -964,6 +1049,22 @@ in
9641049
LADSPA_PATH = "${pkgs.ladspaPlugins}/lib/ladspa";
9651050
};
9661051

1052+
# The global half of the development library set; `devLibraryEnv` in the let
1053+
# block says why it exists and what else it takes. sessionVariables rather
1054+
# than environment.variables so an editor started from Plasma -- where
1055+
# rust-analyzer runs build scripts -- gets them too, not only a shell.
1056+
environment.sessionVariables = {
1057+
# A dev shell's own pkg-config still wins over this: nixpkgs' pkg-config
1058+
# wrapper puts the shell's paths first. Verified with openssl_3 in a
1059+
# nix-shell reporting 3.0.21 while this pointed at 3.6.3.
1060+
PKG_CONFIG_PATH = "${devLibraryEnv}/lib/pkgconfig:${devLibraryEnv}/share/pkgconfig";
1061+
# clang-sys (bindgen's libclang loader) looks here first, then falls back
1062+
# to `llvm-config`, which is not on PATH -- it lives in llvm's dev output.
1063+
# Without this its build script fails before compiling anything; verified
1064+
# with clang-sys 1.9.1.
1065+
LIBCLANG_PATH = "${devLibraryEnv}/lib";
1066+
};
1067+
9671068
# fzf: the module installs pkgs.fzf and sources the bash integration,
9681069
# giving ctrl+r fuzzy history, ctrl+t file picker and alt+c directory jump.
9691070
programs.fzf = {

0 commit comments

Comments
 (0)