@@ -74,6 +74,13 @@ pkgs.stdenv.mkDerivation {
7474 mkdir -p $out/bin
7575 cp build/tests/lgx_tests $out/bin/
7676 cp build/tests/lgx_lib_tests $out/bin/
77+
78+ # lgx_lib_tests links against liblgx.so; CMake embeds the build
79+ # directory in its RPATH. Fix it now so the standard Nix fixupPhase
80+ # check for /build/ references does not fail (postFixup runs too late).
81+ if command -v patchelf &>/dev/null; then
82+ patchelf --set-rpath '$ORIGIN:$ORIGIN/lib:$ORIGIN/../lib' $out/bin/lgx_lib_tests
83+ fi
7784
7885 runHook postInstall
7986 '' ;
@@ -237,20 +244,32 @@ pkgs.stdenv.mkDerivation {
237244 else
238245 echo "=== Making portable (Linux) ==="
239246
247+ # Glibc libraries that must come from the system. They contain
248+ # nix store paths in their data sections (locale/gconv paths) and
249+ # the dynamic linker must live at a fixed absolute path.
250+ is_system_lib() {
251+ case "$1" in
252+ ld-linux*|libc.so*|libpthread.so*|libdl.so*|libm.so*|librt.so*) return 0 ;;
253+ *) return 1 ;;
254+ esac
255+ }
256+
240257 # Bundle /nix/store/ deps from all binaries and libraries
241258 bundle_nix_deps() {
242259 local target="$1"
243- ldd "$target" 2>/dev/null | grep '/nix/store/' | awk '{print $3} ' | while IFS= read -r dep_path; do
260+ ldd "$target" 2>/dev/null | grep -o '/nix/store/[^ )]* ' | while IFS= read -r dep_path; do
244261 [ -z "$dep_path" ] && continue
262+ [ -f "$dep_path" ] || continue
245263 local dep_name
246264 dep_name=$(basename "$dep_path")
265+ is_system_lib "$dep_name" && continue
247266 if [ ! -f "$out/lib/$dep_name" ]; then
248267 echo " bundling $dep_name"
249268 mkdir -p "$out/lib"
250269 cp "$dep_path" "$out/lib/$dep_name"
251270 chmod u+w "$out/lib/$dep_name"
252271 fi
253- done
272+ done || true
254273 }
255274
256275 # Bundle deps from the shared library
@@ -292,6 +311,29 @@ pkgs.stdenv.mkDerivation {
292311 done
293312 fi
294313
314+ # Fix ELF interpreter: replace /nix/store/ interpreter with
315+ # the standard system path so binaries work without Nix.
316+ if [ -d "$out/bin" ]; then
317+ for exe in "$out/bin/"*; do
318+ [ -f "$exe" ] || continue
319+ if file "$exe" 2>/dev/null | grep -q 'ELF'; then
320+ local current_interp
321+ current_interp=$(patchelf --print-interpreter "$exe" 2>/dev/null) || continue
322+ case "$current_interp" in
323+ /nix/store/*)
324+ local interp_name
325+ interp_name=$(basename "$current_interp")
326+ local new_interp="/lib/$interp_name"
327+ # x86_64 conventionally uses /lib64
328+ [[ "$interp_name" == *x86-64* ]] && new_interp="/lib64/$interp_name"
329+ echo " setting interpreter on $(basename "$exe"): $new_interp"
330+ patchelf --set-interpreter "$new_interp" "$exe"
331+ ;;
332+ esac
333+ fi
334+ done
335+ fi
336+
295337 echo "=== Linux portability fixup complete ==="
296338 fi
297339 '' ;
0 commit comments