nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{
2 lib,
3 stdenvNoCC,
4 bun,
5 fetchFromGitHub,
6 makeBinaryWrapper,
7 models-dev,
8 nix-update-script,
9 ripgrep,
10 sysctl,
11 installShellFiles,
12 versionCheckHook,
13 writableTmpDirAsHomeHook,
14}:
15stdenvNoCC.mkDerivation (finalAttrs: {
16 pname = "opencode";
17 version = "1.1.47";
18 src = fetchFromGitHub {
19 owner = "anomalyco";
20 repo = "opencode";
21 tag = "v${finalAttrs.version}";
22 hash = "sha256-f6TVxKV9q2yEQ9r9VCTttXLqpOrYdTEKDUJs+MuQJCQ=";
23 };
24
25 node_modules = stdenvNoCC.mkDerivation {
26 pname = "${finalAttrs.pname}-node_modules";
27 inherit (finalAttrs) version src;
28
29 impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
30 "GIT_PROXY_COMMAND"
31 "SOCKS_SERVER"
32 ];
33
34 nativeBuildInputs = [
35 bun
36 writableTmpDirAsHomeHook
37 ];
38
39 dontConfigure = true;
40
41 buildPhase = ''
42 runHook preBuild
43
44 bun install \
45 --cpu="*" \
46 --frozen-lockfile \
47 --filter ./packages/opencode \
48 --filter ./packages/desktop \
49 --ignore-scripts \
50 --no-progress \
51 --os="*"
52
53 bun --bun ./nix/scripts/canonicalize-node-modules.ts
54 bun --bun ./nix/scripts/normalize-bun-binaries.ts
55
56 runHook postBuild
57 '';
58
59 installPhase = ''
60 runHook preInstall
61
62 mkdir -p $out
63 find . -type d -name node_modules -exec cp -R --parents {} $out \;
64
65 runHook postInstall
66 '';
67
68 # NOTE: Required else we get errors that our fixed-output derivation references store paths
69 dontFixup = true;
70
71 outputHash =
72 if stdenvNoCC.hostPlatform.isDarwin then
73 "sha256-BJmyALnZEiAnC8+bsri5Me1Z0Vtf6A/27e7Eve4IvDo="
74 else
75 "sha256-zkinMkPR1hCBbB5BIuqozQZDpjX4eiFXjM6lpwUx1fM=";
76 outputHashAlgo = "sha256";
77 outputHashMode = "recursive";
78 };
79
80 nativeBuildInputs = [
81 bun
82 installShellFiles
83 makeBinaryWrapper
84 models-dev
85 writableTmpDirAsHomeHook
86 ];
87
88 postPatch = ''
89 # NOTE: Relax Bun version check to be a warning instead of an error
90 substituteInPlace packages/script/src/index.ts \
91 --replace-fail 'throw new Error(`This script requires bun@''${expectedBunVersionRange}' \
92 'console.warn(`Warning: This script requires bun@''${expectedBunVersionRange}'
93 '';
94
95 configurePhase = ''
96 runHook preConfigure
97
98 cp -R ${finalAttrs.node_modules}/. .
99
100 runHook postConfigure
101 '';
102
103 env.MODELS_DEV_API_JSON = "${models-dev}/dist/_api.json";
104 env.OPENCODE_VERSION = finalAttrs.version;
105 env.OPENCODE_CHANNEL = "stable";
106
107 buildPhase = ''
108 runHook preBuild
109
110 cd ./packages/opencode
111 bun --bun ./script/build.ts --single --skip-install
112 bun --bun ./script/schema.ts schema.json
113
114 runHook postBuild
115 '';
116
117 installPhase = ''
118 runHook preInstall
119
120 install -Dm755 dist/opencode-*/bin/opencode $out/bin/opencode
121 wrapProgram $out/bin/opencode \
122 --prefix PATH : ${
123 lib.makeBinPath (
124 [
125 ripgrep
126 ]
127 ++ lib.optionals stdenvNoCC.hostPlatform.isDarwin [
128 sysctl
129 ]
130 )
131 }
132
133 install -Dm644 schema.json $out/share/opencode/schema.json
134
135 runHook postInstall
136 '';
137
138 postInstall = lib.optionalString (stdenvNoCC.buildPlatform.canExecute stdenvNoCC.hostPlatform) ''
139 installShellCompletion --cmd opencode \
140 --bash <($out/bin/opencode completion) \
141 --zsh <(SHELL=/bin/zsh $out/bin/opencode completion)
142 '';
143
144 nativeInstallCheckInputs = [
145 versionCheckHook
146 writableTmpDirAsHomeHook
147 ];
148 doInstallCheck = true;
149 versionCheckKeepEnvironment = [ "HOME" ];
150 versionCheckProgramArg = "--version";
151
152 passthru = {
153 jsonschema = "${placeholder "out"}/share/opencode/schema.json";
154 updateScript = nix-update-script {
155 extraArgs = [
156 "--subpackage"
157 "node_modules"
158 ];
159 };
160 };
161
162 meta = {
163 description = "AI coding agent built for the terminal";
164 homepage = "https://github.com/anomalyco/opencode";
165 license = lib.licenses.mit;
166 maintainers = with lib.maintainers; [
167 delafthi
168 graham33
169 ];
170 sourceProvenance = with lib.sourceTypes; [ fromSource ];
171 platforms = [
172 "aarch64-linux"
173 "x86_64-linux"
174 "aarch64-darwin"
175 "x86_64-darwin"
176 ];
177 mainProgram = "opencode";
178 };
179})