Skip to content

Commit 1463efe

Browse files
Fix opencode WSL2 segfault in place instead of pinning stable
opencode built by nixpkgs segfaults at startup under WSL2 (nixpkgs' bun produces compiled binaries whose ELF layout WSL2's loader rejects — anomalyco/opencode#26846, NixOS/nixpkgs#520383). The old workaround pinned opencode to nixos-25.11, which is stuck at 1.1.14 and predates the --auto flag (added in 1.17.12). Re-running patchelf --set-interpreter on the finished binary rewrites the ELF cleanly and fixes the load, so post-process the cache-fetched unstable package instead: copy it, re-patchelf .opencode-wrapped, and retarget the compiled C wrapper's baked-in store path via equal-length substitution (same derivation name, so same path length). A source rebuild via overrideAttrs was tried first but cannot succeed on WSL2: opencode's build script smoke-tests the fresh binary, which segfaults before any fixup runs — and it would forfeit the binary cache anyway. With the pin gone, the nixpkgs-stable input existed only for it, so remove it from the flake as well. Verified on this WSL2 host: the overlaid package prints its version and lists --auto in its help, where the unpatched unstable binary segfaults (exit 139) on --version.
1 parent 8be3489 commit 1463efe

3 files changed

Lines changed: 27 additions & 30 deletions

File tree

‎flake.lock‎

Lines changed: 1 addition & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎flake.nix‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
# Track nixos-unstable (this system reports 26.11pre / "Zokor").
66
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
77

8-
# Stable channel, used only to swap in packages that are broken on
9-
# unstable (currently opencode on WSL2 — see modules/wsl.nix).
10-
nixpkgs-stable.url = "github:NixOS/nixpkgs/nixos-25.11";
11-
128
nixos-wsl = {
139
url = "github:nix-community/NixOS-WSL/main";
1410
# Build NixOS-WSL against the same nixpkgs pinned above.
@@ -26,7 +22,6 @@
2622
{
2723
self,
2824
nixpkgs,
29-
nixpkgs-stable,
3025
nixos-wsl,
3126
home-manager,
3227
...
@@ -36,7 +31,6 @@
3631
# WSL host, graphical (GNOME). This is the default.
3732
nixos = nixpkgs.lib.nixosSystem {
3833
system = "x86_64-linux";
39-
specialArgs = { inherit nixpkgs-stable; };
4034
modules = [
4135
nixos-wsl.nixosModules.default
4236
home-manager.nixosModules.home-manager
@@ -48,7 +42,6 @@
4842
# Switch on demand: nixos-rebuild switch --flake .#nixos-headless
4943
nixos-headless = nixpkgs.lib.nixosSystem {
5044
system = "x86_64-linux";
51-
specialArgs = { inherit nixpkgs-stable; };
5245
modules = [
5346
nixos-wsl.nixosModules.default
5447
home-manager.nixosModules.home-manager

‎modules/wsl.nix‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# WSL-specific configuration. Only imported by the WSL host; the nixos-wsl
22
# module itself (which provides the `wsl.*` options) is added in flake.nix.
3-
# nixpkgs-stable comes from flake.nix via specialArgs.
4-
{ lib, nixpkgs-stable, ... }:
3+
{ lib, ... }:
54

65
let
76
flakePath = "/home/nixos/oss/nixos-config";
@@ -20,11 +19,33 @@ in
2019

2120
# opencode from nixos-unstable segfaults at startup under WSL2 — the crash
2221
# is in glibc's ld.so while it loads the Bun-compiled binary
23-
# (https://github.com/anomalyco/opencode/issues/26846). The nixos-25.11
24-
# build works, so swap it in here until unstable is fixed.
22+
# (https://github.com/anomalyco/opencode/issues/26846, root cause in
23+
# nixpkgs' bun: https://github.com/NixOS/nixpkgs/issues/520383). Re-running
24+
# patchelf on the finished binary rewrites the ELF layout cleanly and fixes
25+
# the load (verified on 1.17.12/13). Post-process the cache-fetched package
26+
# rather than overrideAttrs: a source rebuild can't even succeed on WSL2
27+
# (opencode's build script smoke-tests the fresh binary, which segfaults
28+
# before any fixup runs) and would forfeit the binary cache. bin/opencode
29+
# is a compiled C wrapper with the original out-path baked in; reusing the
30+
# original derivation name makes both store paths the same length, so the
31+
# self-reference can be retargeted with an in-place equal-length rewrite.
32+
# Drop the overlay once upstream is fixed.
2533
nixpkgs.overlays = [
2634
(final: prev: {
27-
opencode = nixpkgs-stable.legacyPackages.${prev.stdenv.hostPlatform.system}.opencode;
35+
opencode =
36+
final.runCommand prev.opencode.name
37+
{
38+
nativeBuildInputs = [ final.patchelf ];
39+
inherit (prev.opencode) meta;
40+
}
41+
''
42+
cp -a ${prev.opencode} $out
43+
chmod -R u+w $out
44+
find $out -type f -exec sed -i "s|${prev.opencode}|$out|g" {} +
45+
patchelf --set-interpreter \
46+
"$(patchelf --print-interpreter "$out/bin/.opencode-wrapped")" \
47+
"$out/bin/.opencode-wrapped"
48+
'';
2849
})
2950
];
3051

0 commit comments

Comments
 (0)