-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
342 lines (298 loc) · 10.1 KB
/
flake.nix
File metadata and controls
342 lines (298 loc) · 10.1 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
{
description = "Development shell for Bun + TypeScript + Pkl";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay.url = "github:oxalica/rust-overlay";
rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
crane.url = "github:ipetkov/crane";
};
outputs =
{
self,
nixpkgs,
flake-utils,
rust-overlay,
crane,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ rust-overlay.overlays.default ];
};
rustVersion = "1.93.1";
rustToolchain = pkgs.rust-bin.stable.${rustVersion}.default.override {
extensions = [
"rustfmt"
"clippy"
];
};
craneLib = (crane.mkLib pkgs).overrideToolchain (_: rustToolchain);
workspaceRoot = ./.;
workspaceSrc = pkgs.lib.fileset.toSource {
root = workspaceRoot;
fileset = pkgs.lib.fileset.unions [
(craneLib.fileset.commonCargoSources workspaceRoot)
(pkgs.lib.fileset.maybeMissing ./config/.opencode)
(pkgs.lib.fileset.maybeMissing ./config/.claude)
(pkgs.lib.fileset.maybeMissing ./config/schema/sce-config.schema.json)
(pkgs.lib.fileset.maybeMissing ./cli/assets/hooks)
];
};
configLibBashPolicySrc = pkgs.lib.fileset.toSource {
root = ./config/lib/bash-policy-plugin;
fileset = pkgs.lib.fileset.unions [
./config/lib/bash-policy-plugin/package.json
./config/lib/bash-policy-plugin/bun.lock
./config/lib/bash-policy-plugin/bash-policy/runtime.ts
./config/lib/bash-policy-plugin/bash-policy-runtime.test.ts
./config/lib/bash-policy-plugin/opencode-bash-policy-plugin.ts
];
};
# Fixed-output derivation to fetch Bun dependencies
# The output hash must be updated when package.json or bun.lock changes
configLibBashPolicyDeps = pkgs.stdenv.mkDerivation {
pname = "config-lib-bash-policy-deps";
version = "0.1.0";
src = pkgs.lib.fileset.toSource {
root = ./config/lib/bash-policy-plugin;
fileset = pkgs.lib.fileset.unions [
./config/lib/bash-policy-plugin/package.json
./config/lib/bash-policy-plugin/bun.lock
];
};
nativeBuildInputs = [ pkgs.bun ];
dontBuild = true;
installPhase = ''
bun install --frozen-lockfile --no-progress
# Remove Bun's cache symlinks that point to build directory
rm -rf node_modules/.cache
mkdir -p $out
cp -r node_modules $out/
'';
outputHashMode = "recursive";
outputHashAlgo = "sha256";
outputHash = "sha256-4OPHPfR0vHIRsAflr/EFddsMkR5ZnRaN9MLrdJDqTJY=";
};
version = pkgs.lib.strings.trim (builtins.readFile ./.version);
gitCommit =
if self ? rev then
self.rev
else if self ? dirtyRev then
self.dirtyRev
else
"unknown";
shortGitCommit = builtins.substring 0 12 gitCommit;
commonCargoArgs = {
pname = "sce";
inherit version;
src = workspaceSrc;
cargoToml = ./cli/Cargo.toml;
cargoLock = ./cli/Cargo.lock;
strictDeps = true;
doCheck = false;
SCE_GIT_COMMIT = shortGitCommit;
nativeBuildInputs = [
rustToolchain
];
postUnpack = ''
cd "$sourceRoot/cli"
sourceRoot="."
'';
};
cargoArtifacts = craneLib.buildDepsOnly (
commonCargoArgs
// {
pname = "sce-deps";
}
);
scePackage = craneLib.buildPackage (
commonCargoArgs
// {
inherit cargoArtifacts;
}
);
syncOpencodeConfigApp = pkgs.writeShellApplication {
name = "sync-opencode-config";
runtimeInputs = [
pkgs.coreutils
pkgs.diffutils
pkgs.git
pkgs.pkl
pkgs.rsync
];
text = builtins.readFile ./scripts/sync-opencode-config.sh;
};
pklCheckGeneratedApp = pkgs.writeShellApplication {
name = "pkl-check-generated";
runtimeInputs = [
pkgs.git
pkgs.nix
];
text = ''
set -euo pipefail
repo_root="$(git rev-parse --show-toplevel 2>/dev/null || true)"
if [ -z "''${repo_root}" ]; then
repo_root="$(pwd)"
fi
exec nix develop "''${repo_root}" -c "''${repo_root}/config/pkl/check-generated.sh"
'';
};
pklParityCheck =
pkgs.runCommand "pkl-parity-check"
{
nativeBuildInputs = [
pkgs.git
pkgs.pkl
];
}
''
set -euo pipefail
# Copy source files
cp -r "${./.}" ./repo
chmod -R u+w ./repo
cd ./repo
tmp_dir="$(mktemp -d)"
cleanup() {
rm -rf "$tmp_dir"
}
trap cleanup EXIT
pkl eval -m "$tmp_dir" config/pkl/generate.pkl >/dev/null
paths=(
"config/.opencode/agent"
"config/.opencode/command"
"config/.opencode/skills"
"config/.opencode/lib/drift-collectors.js"
"config/automated/.opencode/agent"
"config/automated/.opencode/command"
"config/automated/.opencode/skills"
"config/automated/.opencode/lib/drift-collectors.js"
"config/.claude/agents"
"config/.claude/commands"
"config/.claude/skills"
"config/.claude/lib/drift-collectors.js"
"config/schema/sce-config.schema.json"
)
stale=0
for path in "''${paths[@]}"; do
if [ -e "$tmp_dir/$path" ] || [ -e "$path" ]; then
if ! git diff --no-index --exit-code -- "$tmp_dir/$path" "$path" >/dev/null 2>&1; then
stale=1
printf 'Generated output drift detected at %s\n' "$path"
git diff --no-index -- "$tmp_dir/$path" "$path" || true
fi
fi
done
if [[ "$stale" -ne 0 ]]; then
cat <<'EOF'
Generated files are stale.
Regenerate with:
nix develop -c pkl eval -m . config/pkl/generate.pkl
EOF
exit 1
fi
printf 'Generated outputs are up to date.\n'
mkdir -p "$out"
'';
configLibTests =
pkgs.runCommand "config-lib-tests"
{
nativeBuildInputs = [ pkgs.bun ];
}
''
set -euo pipefail
# Copy source files
cp -r "${configLibBashPolicySrc}" ./bash-policy
chmod -R u+w ./bash-policy
cd ./bash-policy
# Use pre-fetched dependencies from FOD
cp -r "${configLibBashPolicyDeps}/node_modules" ./
# Run tests
bun test
mkdir -p "$out"
'';
in
{
packages = {
sce = scePackage;
default = scePackage;
};
checks = {
cli-tests = craneLib.cargoTest (
commonCargoArgs
// {
pname = "sce-cli-tests";
inherit cargoArtifacts;
nativeCheckInputs = [ pkgs.git ];
}
);
cli-clippy = craneLib.cargoClippy (
commonCargoArgs
// {
pname = "sce-cli-clippy";
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets --all-features";
}
);
cli-fmt = craneLib.cargoFmt (
commonCargoArgs
// {
pname = "sce-cli-fmt";
}
);
pkl-parity = pklParityCheck;
config-lib-tests = configLibTests;
};
apps.sce = {
type = "app";
program = "${scePackage}/bin/sce";
meta = {
description = "Run the packaged sce CLI";
};
};
apps.sync-opencode-config = {
type = "app";
program = "${syncOpencodeConfigApp}/bin/sync-opencode-config";
meta = {
description = "Regenerate config and sync root .opencode";
};
};
apps.pkl-check-generated = {
type = "app";
program = "${pklCheckGeneratedApp}/bin/pkl-check-generated";
meta = {
description = "Run generated-output drift check in dev shell";
};
};
devShells.default = pkgs.mkShell {
packages =
with pkgs;
[
bun
jq
pkl
typescript
nodePackages.typescript-language-server
scePackage
]
++ [ rustToolchain ];
shellHook = ''
version_of() {
"$1" --version 2>/dev/null | awk 'match($0, /[0-9]+(\.[0-9]+)+/) { print substr($0, RSTART, RLENGTH); exit }'
}
echo "- bun: $(version_of bun)"
echo "- pkl: $(version_of pkl)"
echo "- tsc: $(version_of tsc)"
echo "- tsserver-lsp: $(version_of typescript-language-server)"
echo "- rust: $(version_of rustc)"
echo "- sce: $(version_of sce)"
echo "- sync-opencode-config: nix run .#sync-opencode-config"
echo "- sync-opencode-config help: nix run .#sync-opencode-config -- --help"
echo "- pkl-check-generated: nix run .#pkl-check-generated"
'';
};
}
);
}