-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode.nix
More file actions
110 lines (97 loc) · 3.21 KB
/
Copy pathopencode.nix
File metadata and controls
110 lines (97 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{
lib,
stdenv,
fetchurl,
makeWrapper,
unzip,
}:
let
version = (builtins.fromJSON (builtins.readFile ./version.json)).version;
system = stdenv.hostPlatform.system;
isDarwin = stdenv.hostPlatform.isDarwin;
# Platform-specific archive format and naming
platformAttrs = {
x86_64-linux = {
arch = "x64";
ext = "tar.gz";
hash = "sha256-ha6slSWNQJ0WyjTxz810x42dGnCwpBVBKLWI4UBThPk="; # cli-linux-x64
};
aarch64-linux = {
arch = "arm64";
ext = "tar.gz";
hash = "sha256-jMUR+XlOV15dPEwmVJMNBWcBht9knCa1CImsc8Zd3iE="; # cli-linux-arm64
};
x86_64-darwin = {
arch = "x64";
ext = "zip";
hash = "sha256-gXSlOrP4u8xjPG59kUJY8VcuEzvQCIgsSJy026xgEV0="; # cli-darwin-x64
};
aarch64-darwin = {
arch = "arm64";
ext = "zip";
hash = "sha256-kT2BOojKT2IJucSOVIvTdu700edMK7ETqpGqlseE0zI="; # cli-darwin-arm64
};
};
attrs = platformAttrs.${system} or (throw "Unsupported system: ${system}");
os = if isDarwin then "darwin" else "linux";
url = "https://github.com/anomalyco/opencode/releases/download/v${version}/opencode-${os}-${attrs.arch}.${attrs.ext}";
# The dynamic linker path for Linux wrapper
dynamicLinker = lib.optionalString stdenv.hostPlatform.isLinux stdenv.cc.bintools.dynamicLinker;
in
stdenv.mkDerivation {
pname = "opencode";
inherit version;
src = fetchurl {
inherit url;
hash = attrs.hash;
};
# NOTE: Do NOT use autoPatchelfHook or patchelf on this binary.
#
# The opencode CLI is a Bun single-file executable (SFE). Bun SFEs store
# their bundled JS bytecode appended at the tail of the ELF/Mach-O binary,
# located via offsets relative to the end of the file. Any tool that modifies
# binary sections (autoPatchelfHook, patchelf --set-interpreter) will change
# the binary's size, corrupting the bytecode offset and causing it to fall
# back to bare Bun CLI help instead of launching OpenCode.
nativeBuildInputs =
lib.optional (!isDarwin) makeWrapper
++ lib.optional isDarwin unzip;
dontAutoPatchelf = true;
dontStrip = true;
dontFixup = true;
unpackPhase = ''
runHook preUnpack
${if isDarwin then "unzip $src" else "tar -xzf $src"}
runHook postUnpack
'';
installPhase = ''
runHook preInstall
'' + (if isDarwin then ''
# On Darwin, the binary runs natively — no interpreter patching needed
mkdir -p $out/bin
install -Dm755 opencode $out/bin/opencode
'' else ''
mkdir -p $out/lib/opencode $out/bin
# Install the binary untouched — do NOT modify it
install -Dm755 opencode $out/lib/opencode/opencode
# Create a wrapper that invokes the binary through the Nix dynamic linker,
# bypassing the need to patch the ELF interpreter in-place.
makeWrapper ${dynamicLinker} $out/bin/opencode \
--add-flags "$out/lib/opencode/opencode"
'') + ''
runHook postInstall
'';
meta = {
description = "OpenCode - AI-powered terminal code editor";
homepage = "https://opencode.ai";
license = lib.licenses.mit;
sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
mainProgram = "opencode";
platforms = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
};
}