Skip to content

Commit c1afe3a

Browse files
committed
fix(nix): patch the watcher addon for libstdc++ on NixOS
The glibc `@parcel/watcher` addon links libstdc++ without a RUNPATH, so the load fails and every directory watch dies (see #53210). Patch it in `postConfigure`, before `buildPhase` embeds it into the standalone library binary. Keeping the fix in the addon avoids putting libstdc++ on `LD_LIBRARY_PATH`, which every spawned process would inherit and which outranks that program's own RUNPATH. The dev shell keeps its own libstdc++ export, limited to Linux.
1 parent 69269e4 commit c1afe3a

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎flake.nix‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
openssl
2727
git
2828
];
29+
shellHook = pkgs.lib.optionalString pkgs.stdenv.hostPlatform.isLinux ''
30+
# bun dev loads the unpatched registry addon, which needs libstdc++.
31+
# The packaged build patches the addon instead (nix/opencode.nix).
32+
export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath [ (pkgs.lib.getLib pkgs.stdenv.cc.cc) ]}''${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}
33+
'';
2934
};
3035
});
3136

‎nix/opencode.nix‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{
22
lib,
3+
stdenv,
34
stdenvNoCC,
45
callPackage,
56
bun,
67
nodejs,
8+
patchelf,
79
sysctl,
810
makeBinaryWrapper,
911
models-dev,
@@ -26,7 +28,7 @@ stdenvNoCC.mkDerivation (finalAttrs: {
2628
makeBinaryWrapper
2729
models-dev
2830
writableTmpDirAsHomeHook
29-
];
31+
] ++ lib.optionals stdenvNoCC.hostPlatform.isLinux [ patchelf ];
3032

3133
postPatch = ''
3234
# NOTE: Relax Bun version check to be a warning instead of an error
@@ -45,6 +47,37 @@ stdenvNoCC.mkDerivation (finalAttrs: {
4547
runHook postConfigure
4648
'';
4749

50+
# bun dlopens this addon, which links libstdc++ with no RUNPATH, so on NixOS the
51+
# load fails and every directory watch dies (#53210).
52+
# Patch it here, before buildPhase embeds it into the standalone binary, rather
53+
# than exporting LD_LIBRARY_PATH: every spawned process inherits that, and it
54+
# outranks the program's own RUNPATH.
55+
postConfigure = lib.optionalString stdenvNoCC.hostPlatform.isLinux /* sh */ ''
56+
shopt -s nullglob
57+
watcher_paths=(
58+
node_modules/.bun/@parcel+watcher-linux-*-glibc@*/node_modules/@parcel/watcher-linux-*-glibc/watcher.node
59+
)
60+
shopt -u nullglob
61+
62+
echo "postConfigure: Patching ''${#watcher_paths[@]} @parcel/watcher addon(s)" >&2
63+
patched_watchers=0
64+
for watcher_path in "''${watcher_paths[@]}"; do
65+
chmod u+w "$watcher_path"
66+
patchelf --add-rpath ${lib.makeLibraryPath [ (lib.getLib stdenv.cc.cc) ]} "$watcher_path"
67+
# Check the patch worked so the lib can be loaded successfully
68+
bun -e 'require(process.argv[1])' "$PWD/$watcher_path"
69+
(( patched_watchers += 1 ))
70+
done
71+
72+
if (( patched_watchers != 0 )); then
73+
echo "postConfigure: Patched $patched_watchers native FS watcher(s)" >&2
74+
else
75+
# Fail the build if nothing was patched rather than ship an un-bootable watcher.
76+
echo "postConfigure: no @parcel/watcher glibc addon to patch under node_modules/.bun" >&2
77+
exit 1
78+
fi
79+
'';
80+
4881
env.MODELS_DEV_API_JSON = "${models-dev}/dist/_api.json";
4982
env.OPENCODE_DISABLE_MODELS_FETCH = true;
5083
env.OPENCODE_VERSION = finalAttrs.version;

0 commit comments

Comments
 (0)