Skip to content

Commit df4e232

Browse files
committed
opencode: add baseline (x86-64-v2) variant for pre-Haswell CPUs
pkgs.opencode builds from source via Bun, but Bun's Linux x86_64 build is itself x86-64-v3 (Haswell+). The first call into V8 hits SHLX (BMI2) and SIGILLs on older CPUs like Ivy Bridge (Xeon E3-1220 V2, 2012). This adds passthru.baseline, which downloads the upstream prebuilt opencode-linux-x64-baseline.tar.gz and patchelfs it. No Bun involvement at build or run time, so it works on hosts that can't run the v3 native build or even execute Bun itself. Accessible via pkgs.opencode.passthru.baseline. The default (pkgs.opencode) is unchanged for Haswell+ hosts. Ref: anomalyco/opencode#24876 Ref: oven-sh/bun#30613 Ref: NixOS#547108
1 parent d12d81e commit df4e232

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
lib,
3+
stdenv,
4+
fetchurl,
5+
patchelf,
6+
makeBinaryWrapper,
7+
ripgrep,
8+
glibc,
9+
}:
10+
11+
stdenv.mkDerivation (finalAttrs: {
12+
pname = "opencode";
13+
version = "1.18.18";
14+
15+
src = fetchurl {
16+
url = "https://github.com/anomalyco/opencode/releases/download/v${finalAttrs.version}/opencode-linux-x64-baseline.tar.gz";
17+
hash = "sha256-BZFGVYVMHatI8NUqivj3SWfk4m2vh9/mU09X55moU9c=";
18+
};
19+
20+
dontConfigure = true;
21+
dontBuild = true;
22+
dontStrip = true;
23+
24+
# The upstream tarball has a single `opencode` file at the archive root
25+
# (no subdir). With sourceRoot left as the default, the unpacker rejects
26+
# tarballs that don't produce any directories under it.
27+
sourceRoot = ".";
28+
29+
nativeBuildInputs = [
30+
patchelf
31+
makeBinaryWrapper
32+
];
33+
34+
installPhase = ''
35+
runHook preInstall
36+
37+
# The upstream tarball extracts to a single `opencode` file at the root
38+
# (no subdir). Install to $out/libexec/opencode so the wrapper below can
39+
# safely create $out/bin/opencode without colliding.
40+
install -Dm755 opencode $out/libexec/opencode
41+
42+
# patchelf --no-sort avoids autoPatchelf's program-header expansion, which
43+
# corrupts Bun-compiled binaries (observed by the libexec binary
44+
# reporting Bun's version instead of opencode's own after autoPatchelf).
45+
patchelf --no-sort --set-interpreter "${lib.getLib glibc}/lib/ld-linux-x86-64.so.2" $out/libexec/opencode
46+
47+
# Shell wrapper so makeBinaryWrapper's ELF-vs-script heuristics don't
48+
# fight us; also keeps ripgrep on PATH (opencode shells out to `rg`).
49+
makeWrapper $out/libexec/opencode $out/bin/opencode \
50+
--prefix PATH : ${lib.makeBinPath [ ripgrep ]}
51+
52+
runHook postInstall
53+
'';
54+
55+
passthru.updateScript = [
56+
"nix-update"
57+
"--flake"
58+
"opencode"
59+
];
60+
61+
meta = {
62+
description = "AI coding agent built for the terminal (pre-Haswell baseline x86_64 build)";
63+
homepage = "https://github.com/anomalyco/opencode";
64+
changelog = "https://github.com/anomalyco/opencode/releases/tag/v${finalAttrs.version}";
65+
license = lib.licenses.mit;
66+
sourceProvenance = [ lib.sourceTypes.binaryNativeCode ];
67+
platforms = [ "x86_64-linux" ];
68+
mainProgram = "opencode";
69+
};
70+
})

‎pkgs/by-name/op/opencode/package.nix‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
lib,
33
stdenv,
44
bun,
5+
callPackage,
56
darwin,
67
fetchFromGitHub,
78
makeBinaryWrapper,
@@ -206,6 +207,10 @@ stdenv.mkDerivation (finalAttrs: {
206207
tui = "${finalAttrs.finalPackage}/share/tui.json";
207208
};
208209
node_modules = node_modules finalAttrs;
210+
# Pre-built upstream `opencode-linux-x64-baseline.tar.gz` for hosts that
211+
# can't run the v3 native build (e.g. Ivy Bridge, no BMI2/AVX2). Boots
212+
# from V8's `SHLX` trap; see ./baseline.nix for details.
213+
baseline = callPackage ./baseline.nix { };
209214
updateScript = nix-update-script {
210215
extraArgs = [
211216
"--subpackage"

0 commit comments

Comments
 (0)