Skip to content

Commit d9bd774

Browse files
committed
fix(opencode): install upstream's x86_64 baseline build
opencode is a Bun-compiled binary, and nixpkgs compiles it against the Bun build that targets CPUs with AVX2. On the ThinkPad T420 (Sandy Bridge: AVX but no AVX2) it died immediately with: Illegal instruction (core dumped) Upstream publishes a "baseline" flavour of every release for exactly those CPUs (anomalyco/opencode#13379), so overlays/opencode.nix now installs that prebuilt binary (1.18.13, the version pkgs-unstable packages) instead of building opencode from source. It runs on modern CPUs too, so it is the default for x86_64, and it keeps the same wrapper as the nixpkgs package: ripgrep in PATH + OPENCODE_DISABLE_AUTOUPDATE. Nothing has to be compiled locally; the derivation just downloads the tarball and patchelfs it. Wired into the pkgs / pkgs-unstable imports in flake.nix (pkgs-unstable is also what the devshell uses) and documented in README.md.
1 parent 883dd1e commit d9bd774

4 files changed

Lines changed: 94 additions & 2 deletions

File tree

‎README.md‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,17 @@ Run it with `opencode`, or pick another model with `opencode --model <provider/m
170170
opencode auth login
171171
```
172172

173-
> **Status: in development.** The package and config are wired up, but it has not been
174-
> validated on a real NixOS machine yet.
173+
opencode is a Bun-compiled binary, and nixpkgs builds it for CPUs **with AVX2**.
174+
On older machines — a ThinkPad T420, for example, whose Sandy Bridge CPU has AVX
175+
but no AVX2 — that build dies immediately with
176+
`Illegal instruction (core dumped)`. `overlays/opencode.nix` therefore installs
177+
upstream's own **baseline** build of opencode instead of compiling it, which runs
178+
on those CPUs and on modern ones alike. To bump it, edit the version and the hash
179+
in that file.
180+
181+
> **Status: in use.** Validated on a T420 (old CPU, no AVX2) and on modern
182+
> machines; the model config above is what both the devshell and
183+
> `~/.config/opencode/opencode.json` use.
175184
176185
## Secrets
177186

‎flake.nix‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
overlays = [
9696
nur.overlays.default
9797
overlaysWithInputs.fonts
98+
overlaysWithInputs.opencode
9899
];
99100
};
100101

@@ -106,6 +107,7 @@
106107
overlays = [
107108
nur.overlays.default
108109
overlaysWithInputs.fonts
110+
overlaysWithInputs.opencode
109111
];
110112
};
111113

‎overlays/default.nix‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{ inputs }:
22
{
33
fonts = import ./fonts.nix { inherit inputs; };
4+
opencode = import ./opencode.nix { inherit inputs; };
45
}

‎overlays/opencode.nix‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{ ... }:
2+
# ───────────────────────────────────────────────────────────────────────────
3+
# opencode — x86_64 "baseline" build
4+
# ───────────────────────────────────────────────────────────────────────────
5+
# opencode is a Bun-compiled binary, and nixpkgs compiles it with the regular
6+
# Bun build, which targets CPUs that have AVX2. On older machines (ThinkPad
7+
# T420 / Sandy Bridge and Ivy Bridge: AVX but no AVX2) the result dies right
8+
# away with:
9+
#
10+
# Illegal instruction (core dumped)
11+
#
12+
# Upstream publishes an extra "baseline" flavour of every release for exactly
13+
# those CPUs (see anomalyco/opencode#13379), so here we install that prebuilt
14+
# binary instead of building opencode from source. It runs on modern CPUs too,
15+
# so this is the default for every x86_64 machine.
16+
#
17+
# It stays a normal Nix derivation (downloaded + patched at build time), so the
18+
# usual `just switch` / `nix develop` flow keeps working and nothing has to be
19+
# compiled locally.
20+
#
21+
# To bump it: bump `version` and refresh the hash with
22+
# nix-prefetch-url https://github.com/anomalyco/opencode/releases/download/vX.Y.Z/opencode-linux-x64-baseline.tar.gz
23+
final: prev:
24+
let
25+
version = "1.18.13";
26+
src = prev.fetchurl {
27+
url = "https://github.com/anomalyco/opencode/releases/download/v${version}/opencode-linux-x64-baseline.tar.gz";
28+
hash = "sha256-1EccwmDSRrmy3yN6wD+vp1tnIzBqa8MG5U3ObpqoJNs=";
29+
};
30+
in
31+
prev.lib.optionalAttrs prev.stdenv.hostPlatform.isx86_64 {
32+
opencode = prev.stdenvNoCC.mkDerivation {
33+
pname = "opencode";
34+
inherit version src;
35+
36+
strictDeps = true;
37+
38+
# autoPatchelfHook gives the binary the RPATH it needs for glibc /
39+
# libstdc++ and for the libraries it loads at runtime (desktop
40+
# notifications via libsecret, sound via alsa/pulse).
41+
nativeBuildInputs = [
42+
prev.autoPatchelfHook
43+
prev.makeWrapper
44+
];
45+
buildInputs = [
46+
prev.alsa-lib
47+
prev.libsecret
48+
prev.libpulseaudio
49+
prev.openssl
50+
prev.stdenv.cc.cc.lib
51+
];
52+
53+
dontConfigure = true;
54+
dontBuild = true;
55+
# Never strip it: the Bun runtime lives inside this same file.
56+
dontStrip = true;
57+
58+
installPhase = ''
59+
runHook preInstall
60+
61+
install -Dm755 opencode $out/bin/opencode
62+
63+
wrapProgram $out/bin/opencode \
64+
--prefix PATH : ${prev.lib.makeBinPath [ prev.ripgrep ]} \
65+
--set OPENCODE_DISABLE_AUTOUPDATE true
66+
67+
runHook postInstall
68+
'';
69+
70+
meta = {
71+
description = "AI coding agent built for the terminal (x86_64 baseline build)";
72+
homepage = "https://github.com/anomalyco/opencode";
73+
changelog = "https://github.com/anomalyco/opencode/releases/tag/v${version}";
74+
license = prev.lib.licenses.mit;
75+
sourceProvenance = with prev.lib.sourceTypes; [ binaryNativeCode ];
76+
mainProgram = "opencode";
77+
platforms = [ "x86_64-linux" ];
78+
};
79+
};
80+
}

0 commit comments

Comments
 (0)