-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJustfile
More file actions
388 lines (321 loc) · 12.1 KB
/
Justfile
File metadata and controls
388 lines (321 loc) · 12.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# =============================================================================
# Configuration Variables
# =============================================================================
detected_os := os()
os := if detected_os == "macos" { "darwin" } else { "linux" }
rebuild_cmd := if os == "darwin" { "darwin-rebuild" } else { "nixos-rebuild" }
stable_channel := "nixpkgs"
unstable_channel := "nixpkgs-unstable"
combined_channels := "nixpkgs nixpkgs-unstable"
pkgs_unstable := unstable_channel
pkgs_all := combined_channels
flake_shared := "determinate home-manager agenix nix-cache-push llm-agents"
flake_darwin := flake_shared + " nix-darwin nix-homebrew"
flake_linux := flake_shared + " disko"
flakes := if os == "darwin" { flake_darwin } else { flake_linux }
flakes_all := flake_shared + " nix-darwin nix-homebrew disko"
current_system := "/run/current-system"
result_link := "./result"
system_profile := "/nix/var/nix/profiles/system"
hm_switch_diff_target := "esp-0"
# =============================================================================
# Default Recipe
# =============================================================================
# Show all public recipes.
default:
@just --list
# =============================================================================
# Core Helper Functions
# =============================================================================
# Run `nix flake update` with optional input arguments.
_nix_update *args:
@if [ -z '{{args}}' ]; then \
echo "Running: nix flake update"; \
nix flake update; \
else \
echo "Running: nix flake update {{args}}"; \
nix flake update {{args}}; \
fi
# Update Homebrew package metadata on macOS.
[macos]
_brew_update:
@echo "Running: brew update"
@brew update || (echo "Brew update failed, continuing..."; exit 0)
# No-op Homebrew update on Linux.
[linux]
_brew_update:
@echo "Skipping brew update (macOS only)"
# Build the system and print a closure diff against current state.
_build_with_diff:
@echo "Running: {{rebuild_cmd}} build --flake ."
@{{rebuild_cmd}} build --flake .
@echo "--------------------------------------------------"
@echo "Comparing..."
@nvd -- diff {{current_system}} {{result_link}}
@echo "--------------------------------------------------"
@echo "Removing {{result_link}} symlink..."
@unlink {{result_link}}
# Find the active standalone Home Manager profile, if it exists.
_hm_find_profile:
#!/usr/bin/env bash
set -euo pipefail
for profile in \
"$HOME/.local/state/nix/profiles/home-manager" \
"/nix/var/nix/profiles/per-user/$USER/home-manager"
do
if [ -L "$profile" ]; then
printf '%s\n' "$profile"
exit 0
fi
done
# Print a diff between the active Home Manager profile and the build result.
_hm_show_diff:
#!/usr/bin/env bash
set -euo pipefail
if ! command -v nvd >/dev/null 2>&1; then
echo "Skipping diff: 'nvd' is not available in PATH."
exit 0
fi
current_profile="$({{just_executable()}} _hm_find_profile || true)"
if [ -z "$current_profile" ]; then
echo "Skipping diff: no active Home Manager profile found."
exit 0
fi
if [ ! -e "{{result_link}}" ]; then
echo "Skipping diff: {{result_link}} does not exist."
exit 0
fi
echo "--------------------------------------------------"
echo "Comparing..."
nvd diff "$current_profile" "{{result_link}}"
echo "--------------------------------------------------"
# Build the standalone Home Manager configuration and show closure changes.
_hm_build_with_diff flake:
#!/usr/bin/env bash
set -euo pipefail
echo "Running: home-manager build --flake .#{{flake}}"
home-manager build --flake .#{{flake}}
{{just_executable()}} _hm_show_diff
if [ -L "{{result_link}}" ] || [ -e "{{result_link}}" ]; then
echo "Removing {{result_link}} symlink..."
unlink "{{result_link}}"
fi
# Apply the built system configuration.
_switch:
@echo "Running: sudo {{rebuild_cmd}} switch --flake ."
@sudo {{rebuild_cmd}} switch --flake .
# Validate that the rollback generation argument is present and numeric.
_validate_generation generation_id:
#!/bin/sh -e
if [ -z "{{generation_id}}" ]; then
echo "Error: Generation number argument is required."
echo "Usage: just rollback <generation_number>"
exit 1
fi
if ! echo "{{generation_id}}" | grep -qE '^[0-9]+$'; then
echo "Error: Invalid input: '{{generation_id}}' is not a valid generation."
exit 1
fi
# Ensure rebuild command and sudo access are available.
_check_rebuild_prereqs:
#!/bin/sh -e
if ! command -v {{rebuild_cmd}} > /dev/null; then
echo "Error: '{{rebuild_cmd}}' command not found in PATH."
exit 1
fi
if ! sudo -n true 2>/dev/null; then
echo "Requesting sudo privileges..."
sudo -v || { echo "Error: sudo privileges are required."; exit 1; }
fi
# =============================================================================
# Package Management
# =============================================================================
# Update unstable package channels.
update-pkgs: _brew_update
@just _nix_update "{{pkgs_unstable}}"
# Update both stable and unstable package channels.
update-pkgs-all: _brew_update
@just _nix_update "{{pkgs_all}}"
# Update only the specified flake inputs.
update-specific input:
@just _nix_update {{input}}
# Update shared flake inputs based on the current OS.
update-flakes:
@just _nix_update "{{flakes}}"
# Update all flake inputs across both macOS and Linux targets.
update-flakes-all:
@just _nix_update "{{flakes_all}}"
# =============================================================================
# Build and Switch Operations
# =============================================================================
# Build the local configuration and show closure changes.
build:
@just _build_with_diff
# Build an ISO image from a machine name or full flake target.
make-iso flake:
#!/usr/bin/env bash
set -euo pipefail
target="{{flake}}"
case "$target" in
.#nixosConfigurations.*)
target="path:$(pwd)${target#.}"
;;
.#*)
machine="${target#.#}"
target="path:$(pwd)#nixosConfigurations.${machine}.config.system.build.isoImage"
;;
esac
echo "Running: nix build $target -L"
nix build "$target" -L
# Switch this machine to the new configuration.
switch:
@just _switch
# Build a standalone Home Manager configuration and show closure changes.
hm-build flake:
@just _hm_build_with_diff {{flake}}
# Switch a standalone Home Manager configuration.
hm-switch flake:
#!/usr/bin/env bash
set -euo pipefail
if [ "{{flake}}" = "{{hm_switch_diff_target}}" ]; then
{{just_executable()}} _hm_build_with_diff "{{flake}}"
fi
echo "Running: home-manager switch --flake .#{{flake}}"
home-manager switch --flake .#{{flake}}
# =============================================================================
# Generation Management
# =============================================================================
# List available system generations for this host.
list-generations:
@just _check_rebuild_prereqs
@echo "Available {{os}} generations (requires sudo):"
@sudo {{rebuild_cmd}} {{ if os() == "macos" { "--list-generations" } else { "list-generations" } }}
# List available standalone Home Manager generations.
hm-list-generations:
@echo "Available Home Manager generations:"
@home-manager generations
# Roll back to the specified system generation.
rollback generation_id:
@just _validate_generation {{generation_id}}
@just _check_rebuild_prereqs
@just _rollback_{{os}} {{generation_id}}
# Roll back the standalone Home Manager configuration.
hm-rollback:
@echo "Running: home-manager switch --rollback"
@home-manager switch --rollback
# Perform rollback for nix-darwin systems.
[macos]
_rollback_darwin generation_id:
#!/bin/sh -e
PROFILE_PATH="{{system_profile}}-{{generation_id}}-link"
if ! sudo test -L "$PROFILE_PATH"; then
echo "Error: Generation {{generation_id}} does not exist."
exit 1
fi
echo "Rolling back to generation {{generation_id}}..."
sudo darwin-rebuild switch -G "{{generation_id}}"
echo "Rollback complete!"
# Perform rollback for NixOS systems.
[linux]
_rollback_linux generation_id:
#!/bin/sh -e
PROFILE_PATH="{{system_profile}}-{{generation_id}}-link"
if ! sudo test -L "$PROFILE_PATH"; then
echo "Error: Generation {{generation_id}} does not exist."
exit 1
fi
echo "Rolling back to generation {{generation_id}}..."
sudo nix-env --switch-generation "{{generation_id}}" -p {{system_profile}}
sudo {{system_profile}}/bin/switch-to-configuration switch
echo "Rollback complete!"
# =============================================================================
# Deployment (Remote Systems)
# =============================================================================
# Deploy configurations to one or more remote target hosts.
deploy target_hosts:
#!/bin/sh -e
if [ -z "{{target_hosts}}" ]; then
echo "Error: at least one target host is required."
echo "Usage: just deploy <host1,host2,...>"
exit 1
fi
if ! command -v nixos-deploy >/dev/null 2>&1; then \
echo "Error: 'nixos-deploy' command not found."; \
echo "Enable it with: programs.nixos-deploy.enable = true"; \
exit 1; \
fi
deploy_cmd="$(command -v nixos-deploy)"
if [ ! -t 0 ]; then
echo "Error: deploy requires an interactive terminal to read sudo password."
exit 1
fi
printf "Remote sudo password (used for all hosts): "
trap 'stty echo 2>/dev/null || true' EXIT INT TERM
stty -echo
IFS= read -r deploy_sudo_password
stty echo
trap - EXIT INT TERM
printf "\n"
host_list=$(printf '%s' "{{target_hosts}}" | tr -d '[:space:]')
if [ -z "$host_list" ]; then
echo "Error: invalid target host list."
echo "Usage: just deploy <host1,host2,...>"
exit 1
fi
OLD_IFS="$IFS"
set -f
IFS=','
set -- $host_list
IFS="$OLD_IFS"
set +f
for target_host in "$@"; do
if [ -z "$target_host" ]; then
echo "Error: host list contains an empty entry."
echo "Usage: just deploy <host1,host2,...>"
exit 1
fi
target_expr=".#nixosConfigurations.${target_host}"
set +e
target_eval_output=$(nix eval --raw "${target_expr}.config.nixpkgs.system" 2>&1)
eval_status=$?
set -e
if [ "$eval_status" -ne 0 ]; then
echo "Error: could not determine system architecture for '$target_host'."
printf '%s\n' "$target_eval_output"
exit "$eval_status"
fi
target_system=$(printf '%s\n' "$target_eval_output" | tail -n1 | tr -d '\r\n')
case "$target_system" in
x86_64-*)
build_host="builder-x86_64"
;;
aarch64-*)
build_host="builder-aarch64"
;;
*)
echo "Error: unsupported system architecture '$target_system' for '$target_host'."
exit 1
;;
esac
echo "Deploying configuration to $target_host..."
echo "Detected target system: $target_system"
echo "Selected build host: $build_host"
printf '%s\n' "$deploy_sudo_password" | "$deploy_cmd" ".#$target_host" "$build_host" "$target_host"
done
deploy_sudo_password=""
# =============================================================================
# Convenience Aliases
# =============================================================================
alias s := switch
alias b := build
alias up := update-pkgs
alias upa := update-pkgs-all
alias uf := update-flakes
alias ufa := update-flakes-all
alias us := update-specific
alias lg := list-generations
alias rb := rollback
alias hs := hm-switch
alias hb := hm-build
alias hlg := hm-list-generations
alias hrb := hm-rollback