fix(sf-from-source): inject Nix-store zlib into LD_LIBRARY_PATH

bun's loader doesn't inherit the same library search path as node under
Nix, so require('forge_engine.linux-x64.node') fails with
'libz.so.1: cannot open shared object file' even when the native addon
exists at the expected path. Result: sf-from-source ran in
JS-fallback mode, and we'd been working around it by switching to
node dist/loader.js — which forces a manual `npm run copy-resources`
after every src/ change to keep dist in sync.

This wraps sf-from-source to find a Nix-store zlib at startup and
prepend it to LD_LIBRARY_PATH before exec'ing bun. The native addon
loads cleanly; from-source becomes the reliable default again; no
more dist drift to worry about.

Find pattern: /nix/store/*-zlib-*/lib/libz.so.1 at maxdepth 4
(maxdepth 2 was too shallow — the hash dir is depth 1, lib is depth 2,
the .so.1 file is depth 3, plus we want the parent dir for
LD_LIBRARY_PATH so '%h' on a depth-3 match gives the lib dir).

Outside Nix (no /nix/store), this is a no-op and falls through to
the existing exec.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Mikael Hugo 2026-04-29 15:01:55 +02:00
parent a2b709f669
commit 4912f6ea80

View file

@ -21,4 +21,17 @@ SF_SOURCE_ROOT=$(cd -- "$SCRIPT_DIR/.." &>/dev/null && pwd)
export SF_BIN_PATH="$SCRIPT_DIR/sf-from-source" export SF_BIN_PATH="$SCRIPT_DIR/sf-from-source"
# Native addon (forge_engine.node) links against libz / libstdc++ that are
# resolved automatically when run under node, but bun's loader doesn't
# inherit the same library search path under Nix. Prepend a Nix-store zlib
# (and stdc++ where present) so require('forge_engine.linux-x64.node')
# succeeds. No-op outside Nix (find returns nothing → empty prefix).
if [ -d /nix/store ]; then
_sf_libz_dir=$(find /nix/store -maxdepth 4 -path '*-zlib-*/lib/libz.so.1' -printf '%h\n' 2>/dev/null | head -1)
if [ -n "${_sf_libz_dir:-}" ]; then
export LD_LIBRARY_PATH="${_sf_libz_dir}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
fi
unset _sf_libz_dir
fi
exec bun run "$SF_SOURCE_ROOT/src/loader.ts" "$@" exec bun run "$SF_SOURCE_ROOT/src/loader.ts" "$@"