-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
340 lines (287 loc) · 11 KB
/
flake.nix
File metadata and controls
340 lines (287 loc) · 11 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
{
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";
cli.url = "path:./cli";
cli.inputs.nixpkgs.follows = "nixpkgs";
cli.inputs.flake-utils.follows = "flake-utils";
cli.inputs.rust-overlay.follows = "rust-overlay";
};
outputs =
{
self,
nixpkgs,
flake-utils,
rust-overlay,
cli,
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
};
syncOpencodeConfigApp = pkgs.writeShellApplication {
name = "sync-opencode-config";
runtimeInputs = [
pkgs.coreutils
pkgs.diffutils
pkgs.git
pkgs.pkl
pkgs.rsync
];
text = ''
set -euo pipefail
usage() {
cat <<'EOF'
Usage: nix run .#sync-opencode-config [-- --help]
Deterministic flake entrypoint for opencode config sync workflow.
Current scope:
- Regenerate generated config outputs in a staging workspace.
- Replace repository config/ only after successful staged regeneration.
- Replace repository-root .opencode/ from staged config/.opencode/.
- Exclude runtime artifacts during root sync (for example node_modules/).
EOF
}
case "''${1:-}" in
-h|--help)
usage
exit 0
;;
esac
repo_root="$(git rev-parse --show-toplevel 2>/dev/null || true)"
if [ -z "''${repo_root}" ]; then
repo_root="$(pwd)"
fi
live_config="''${repo_root}/config"
live_opencode="''${repo_root}/.opencode"
generator_path="''${live_config}/pkl/generate.pkl"
if [ ! -d "''${live_config}" ]; then
cat >&2 <<EOF
Could not locate live config directory at:
''${live_config}
EOF
exit 1
fi
if [ ! -f "''${generator_path}" ]; then
cat >&2 <<EOF
Could not locate config generator at:
''${generator_path}
Run this command from the repository (or inside a git worktree rooted there).
EOF
exit 1
fi
stage_root="$(mktemp -d "''${TMPDIR:-/tmp}/sync-opencode-config.XXXXXX")"
stage_config="''${stage_root}/config"
stage_opencode="''${stage_config}/.opencode"
stage_generator_path="''${stage_config}/pkl/generate.pkl"
backup_config="''${repo_root}/.config-pre-sync-backup"
backup_opencode="''${repo_root}/.opencode-pre-sync-backup"
config_swap_complete=0
opencode_swap_complete=0
exclude_runtime_artifacts=(
node_modules
)
cleanup() {
if [ "''${opencode_swap_complete}" -ne 1 ] && [ -d "''${backup_opencode}" ]; then
rm -rf "''${live_opencode}"
mv "''${backup_opencode}" "''${live_opencode}"
fi
if [ "''${config_swap_complete}" -ne 1 ] && [ -d "''${backup_config}" ]; then
rm -rf "''${live_config}"
mv "''${backup_config}" "''${live_config}"
fi
rm -rf "''${stage_root}" "''${backup_config}" "''${backup_opencode}"
}
trap cleanup EXIT
echo "==> Preparing staged config workspace"
cp -R "''${live_config}" "''${stage_config}"
echo "==> Regenerating generated-owned config outputs in staging"
pkl eval -m "''${stage_root}" "''${stage_generator_path}"
if [ ! -d "''${stage_config}/.opencode" ] || [ ! -d "''${stage_config}/.claude" ]; then
cat >&2 <<EOF
Staged regeneration is incomplete; refusing to replace live config/.
Missing expected generated directories under:
''${stage_config}
EOF
exit 1
fi
if [ ! -d "''${stage_opencode}" ]; then
cat >&2 <<EOF
Staged regeneration is missing config/.opencode; refusing to replace root .opencode/.
Missing directory:
''${stage_opencode}
EOF
exit 1
fi
if [ -e "''${backup_config}" ]; then
rm -rf "''${backup_config}"
fi
echo "==> Replacing live config/ from staged output"
mv "''${live_config}" "''${backup_config}"
cp -R "''${stage_config}" "''${live_config}"
rm -rf "''${backup_config}"
config_swap_complete=1
if [ -e "''${backup_opencode}" ]; then
rm -rf "''${backup_opencode}"
fi
if [ -e "''${live_opencode}" ]; then
echo "==> Replacing repository-root .opencode/ from staged config/.opencode/"
mv "''${live_opencode}" "''${backup_opencode}"
else
echo "==> Creating repository-root .opencode/ from staged config/.opencode/"
fi
rm -rf "''${live_opencode}"
mkdir -p "''${live_opencode}"
rsync_excludes=()
diff_excludes=()
for entry in "''${exclude_runtime_artifacts[@]}"; do
rsync_excludes+=(--exclude "''${entry}/")
diff_excludes+=(-x "''${entry}")
done
rsync -a "''${rsync_excludes[@]}" "''${stage_opencode}/" "''${live_opencode}/"
if ! diff -rq "''${diff_excludes[@]}" "''${stage_opencode}" "''${live_opencode}" >/dev/null; then
cat >&2 <<EOF
Root .opencode replacement verification failed.
Source and target trees differ after copy.
EOF
exit 1
fi
rm -rf "''${backup_opencode}"
opencode_swap_complete=1
cat <<'EOF'
Done: repository config/ has been regenerated in staging and replaced.
Done: repository-root .opencode/ has been replaced from staged config/.opencode/.
EOF
'';
};
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"
'';
};
tokenCountWorkflowsApp = pkgs.writeShellApplication {
name = "token-count-workflows";
runtimeInputs = [
pkgs.git
pkgs.nix
];
text = ''
set -euo pipefail
usage() {
cat <<'EOF'
Usage: nix run .#token-count-workflows [-- --help]
Deterministic flake entrypoint for workflow token counting.
Runs evals/token-count-workflows.ts through the existing evals Bun runtime.
EOF
}
case "''${1:-}" in
-h|--help)
usage
exit 0
;;
esac
repo_root="$(git rev-parse --show-toplevel 2>/dev/null || true)"
if [ -z "''${repo_root}" ]; then
repo_root="$(pwd)"
fi
evals_dir="''${repo_root}/evals"
if [ ! -d "''${evals_dir}" ]; then
cat >&2 <<EOF
Could not locate evals directory at:
''${evals_dir}
Run this command from the repository (or inside a git worktree rooted there).
EOF
exit 1
fi
exec nix develop "''${repo_root}" -c sh -c "cd \"''${evals_dir}\" && exec bun run token-count-workflows"
'';
};
agnixLspShim = pkgs.writeShellScriptBin "agnix-lsp" ''
set -euo pipefail
if [ -n "''${AGNIX_LSP_BIN:-}" ] && [ -x "''${AGNIX_LSP_BIN}" ]; then
exec "''${AGNIX_LSP_BIN}" "$@"
fi
if [ -x "$HOME/.cargo/bin/agnix-lsp" ]; then
exec "$HOME/.cargo/bin/agnix-lsp" "$@"
fi
cat >&2 <<'EOF'
agnix-lsp is not bundled in nixpkgs for this dev shell yet.
Manual fallback (non-automatic):
cargo install --locked agnix-lsp
Then either:
- ensure ~/.cargo/bin is on PATH, or
- set AGNIX_LSP_BIN to the agnix-lsp binary path.
EOF
exit 1
'';
in
{
checks.cli-setup-command-surface = cli.checks.${system}.cli-setup-command-surface;
apps.sync-opencode-config = {
type = "app";
program = "${syncOpencodeConfigApp}/bin/sync-opencode-config";
meta = {
description = "Regenerate and sync config/.opencode outputs";
};
};
apps.pkl-check-generated = {
type = "app";
program = "${pklCheckGeneratedApp}/bin/pkl-check-generated";
meta = {
description = "Run generated-output drift check in dev shell";
};
};
apps.token-count-workflows = {
type = "app";
program = "${tokenCountWorkflowsApp}/bin/token-count-workflows";
meta = {
description = "Run static workflow token counting via evals Bun runtime";
};
};
devShells.default = pkgs.mkShell {
packages = with pkgs; [
bun
jq
pkl
typescript
nodePackages.typescript-language-server
agnixLspShim
cargo
rustc
];
shellHook = ''
version_of() {
"$1" --version 2>/dev/null | awk 'match($0, /[0-9]+(\.[0-9]+)+/) { print substr($0, RSTART, RLENGTH); exit }'
}
export PATH="$HOME/.cargo/bin:$PATH"
if [ ! -x "$HOME/.cargo/bin/agnix" ]; then
echo "- agnix: installing agnix-cli via cargo"
cargo install --locked agnix-cli
fi
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 "- agnix: $(version_of agnix)"
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"
'';
};
}
);
}