-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpackage.nix
More file actions
206 lines (167 loc) · 4.73 KB
/
package.nix
File metadata and controls
206 lines (167 loc) · 4.73 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
{
lib,
stdenv,
stdenvNoCC,
buildGoModule,
bun,
fetchFromGitHub,
makeBinaryWrapper,
models-dev,
nix-update-script,
testers,
writableTmpDirAsHomeHook,
}:
let
bun-target = {
"aarch64-darwin" = "bun-darwin-arm64";
"aarch64-linux" = "bun-linux-arm64";
"x86_64-darwin" = "bun-darwin-x64";
"x86_64-linux" = "bun-linux-x64";
};
in
stdenvNoCC.mkDerivation (finalAttrs: {
pname = "opencode";
version = "github-v1.2.16";
src = fetchFromGitHub {
owner = "sst";
repo = "opencode";
tag = "v${finalAttrs.version}";
hash = "sha256-GiByJg4NpllA4N4QGSyWsBNqKqKIdxicIjQpc7mHgEs=";
};
tui = buildGoModule {
pname = "opencode-tui";
inherit (finalAttrs) version src;
modRoot = "packages/tui";
vendorHash = "sha256-H+TybeyyHTbhvTye0PCDcsWkcN8M34EJ2ddxyXEJkZI=";
subPackages = [ "cmd/opencode" ];
env.CGO_ENABLED = 0;
ldflags = [
"-s"
"-X=main.Version=${finalAttrs.version}"
];
installPhase = ''
runHook preInstall
install -Dm755 $GOPATH/bin/opencode $out/bin/tui
runHook postInstall
'';
};
node_modules = stdenvNoCC.mkDerivation {
pname = "opencode-node_modules";
inherit (finalAttrs) version src;
impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
"GIT_PROXY_COMMAND"
"SOCKS_SERVER"
];
nativeBuildInputs = [
bun
writableTmpDirAsHomeHook
];
dontConfigure = true;
buildPhase = ''
runHook preBuild
export BUN_INSTALL_CACHE_DIR=$(mktemp -d)
# Disable post-install scripts to avoid shebang issues
bun install \
--filter=opencode \
--force \
--ignore-scripts \
--no-progress
# Remove `--frozen-lockfile` and `--production` — they erroneously report the lockfile needs updating even though `bun install` does not change it.
# Related to https://github.com/oven-sh/bun/issues/19088
# --frozen-lockfile \
# --production
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out/node_modules
cp -R ./node_modules $out
runHook postInstall
'';
# Required else we get errors that our fixed-output derivation references store paths
dontFixup = true;
outputHash =
{
x86_64-linux = "sha256-YOTuzwo0ZjqVswW3bUu3pFJcmfl0X0Se8Z5jKg8/rQs=";
}
.${stdenv.hostPlatform.system};
outputHashAlgo = "sha256";
outputHashMode = "recursive";
};
nativeBuildInputs = [
bun
makeBinaryWrapper
models-dev
];
patches = [
# Patch `packages/opencode/src/provider/models-macro.ts` to get contents of
# `_api.json` from the file bundled with `bun build`.
./local-models-dev.patch
];
configurePhase = ''
runHook preConfigure
cp -R ${finalAttrs.node_modules}/node_modules .
runHook postConfigure
'';
env.MODELS_DEV_API_JSON = "${models-dev}/dist/_api.json";
buildPhase = ''
runHook preBuild
bun build \
--define OPENCODE_TUI_PATH="'${finalAttrs.tui}/bin/tui'" \
--define OPENCODE_VERSION="'${finalAttrs.version}'" \
--compile \
--compile-exec-argv="--" \
--target=${bun-target.${stdenvNoCC.hostPlatform.system}} \
--outfile=opencode \
./packages/opencode/src/index.ts \
runHook postBuild
'';
dontStrip = true;
installPhase = ''
runHook preInstall
install -Dm755 opencode $out/bin/opencode
runHook postInstall
'';
# Execution of commands using bash-tool fail on linux with
# Error [ERR_DLOPEN_FAILED]: libstdc++.so.6: cannot open shared object file: No such
# file or directory
# Thus, we add libstdc++.so.6 manually to LD_LIBRARY_PATH
postFixup = ''
wrapProgram $out/bin/opencode \
--set LD_LIBRARY_PATH "${lib.makeLibraryPath [ stdenv.cc.cc.lib ]}"
'';
passthru = {
tests.version = testers.testVersion {
package = finalAttrs.finalPackage;
command = "HOME=$(mktemp -d) opencode --version";
inherit (finalAttrs) version;
};
updateScript = nix-update-script {
extraArgs = [
"--subpackage"
"tui"
"--subpackage"
"node_modules"
];
};
};
meta = {
description = "AI coding agent built for the terminal";
longDescription = ''
OpenCode is a terminal-based agent that can build anything.
It combines a TypeScript/JavaScript core with a Go-based TUI
to provide an interactive AI coding experience.
'';
homepage = "https://github.com/sst/opencode";
license = lib.licenses.mit;
platforms = lib.platforms.unix;
maintainers = [
{
email = "aodhan.hayter@gmail.com";
github = "AodhanHayter";
name = "Aodhan Hayter";
}
];
mainProgram = "opencode";
};
})