-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnix.sh
More file actions
executable file
·533 lines (455 loc) · 18.3 KB
/
Copy pathnix.sh
File metadata and controls
executable file
·533 lines (455 loc) · 18.3 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/usr/bin/env bash
set -euo pipefail
# ── Colors ────────────────────────────────────────────────────────────────────
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
BLUE='\033[0;34m'; CYAN='\033[0;36m'; BOLD='\033[1m'; DIM='\033[2m'; ITALIC='\033[3m'; NC='\033[0m'
# ── Print helpers ─────────────────────────────────────────────────────────────
print_info() { echo -e " ${BLUE}➜${NC} $1"; }
print_success() { echo -e " ${GREEN}✔${NC} $1"; }
print_warning() { echo -e " ${YELLOW}⚠${NC} $1"; }
print_error() { echo -e " ${RED}✖${NC} $1"; }
print_step() { echo -e "\n${BOLD}${CYAN}▸${NC} ${BOLD}$1${NC}"; }
print_dim() { echo -e " ${DIM}$1${NC}"; }
print_divider() { echo -e " ${DIM}────────────────────────────────────────────${NC}"; }
# ── Spinner ───────────────────────────────────────────────────────────────────
SPINNER_PID=""
_spinner_loop() {
local chars='⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏' msg="$1"
printf '\033[?25l'
while true; do
for ((i=0; i<${#chars}; i++)); do
printf "\r ${CYAN}%s${NC} %s" "${chars:$i:1}" "$msg"
sleep 0.08
done
done
}
start_spinner() { _spinner_loop "$1" & SPINNER_PID=$!; }
stop_spinner() {
[[ -z "$SPINNER_PID" ]] && return
kill "$SPINNER_PID" 2>/dev/null || true
wait "$SPINNER_PID" 2>/dev/null || true
SPINNER_PID=""
printf '\033[?25h'
printf "\r%*s\r" 80 ""
}
# ── Cleanup on interrupt ──────────────────────────────────────────────────────
_BACKGROUND_PID=""
_cleanup() {
stop_spinner
[[ -n "$_BACKGROUND_PID" ]] && kill "$_BACKGROUND_PID" 2>/dev/null || true
echo ""
print_warning "Interrupted"
exit 130
}
trap _cleanup INT TERM
# ── Elapsed time ──────────────────────────────────────────────────────────────
_fmt_elapsed() {
local mins=$(( $1 / 60 )) secs=$(( $1 % 60 ))
[[ $mins -gt 0 ]] && echo "${mins}m ${secs}s" || echo "${secs}s"
}
# ── Command runner ────────────────────────────────────────────────────────────
_CMD_LOG=""
LOG_FILE=""
run_cmd() {
local title="$1"; shift
_CMD_LOG=$(mktemp)
local exit_code=0
"$@" >"$_CMD_LOG" 2>&1 &
_BACKGROUND_PID=$!
local cmd_pid=$_BACKGROUND_PID
start_spinner "$title"
local shown=0
while kill -0 "$cmd_pid" 2>/dev/null; do
local total
total=$(wc -l <"$_CMD_LOG" 2>/dev/null | awk '{print $1}')
if (( total > shown )); then
while IFS= read -r line; do
if [[ "$line" =~ (building|copying|fetching|activating|error:|warning:) ]]; then
stop_spinner
printf " ${DIM}%s${NC}\n" "$line"
start_spinner "$title"
fi
done < <(sed -n "$((shown+1)),${total}p" "$_CMD_LOG" 2>/dev/null || true)
shown=$total
fi
sleep 0.3
done
wait "$cmd_pid" || exit_code=$?
stop_spinner
_BACKGROUND_PID=""
if [[ -n "$LOG_FILE" ]]; then
{ printf "\n=== %s | %s ===\n" "$(date '+%Y-%m-%d %H:%M:%S')" "$title"
cat "$_CMD_LOG"
} >> "$LOG_FILE"
fi
return "$exit_code"
}
# ── Error display ─────────────────────────────────────────────────────────────
show_errors() {
local logfile="$1" label="$2"
echo ""
echo -e " ${RED}${BOLD}✖ $label failed${NC}"
echo ""
local errors
errors=$(grep -E "^(error:| …| at )" "$logfile" 2>/dev/null | head -25 || true)
if [[ -n "$errors" ]]; then
echo -e " ${BOLD}Error details:${NC}"
while IFS= read -r line; do
[[ "$line" =~ ^error: ]] && echo -e " ${RED}$line${NC}" || echo -e " ${DIM}$line${NC}"
done <<< "$errors"
else
echo -e " ${BOLD}Last output:${NC}"
tail -20 "$logfile" | sed 's/^/ /'
fi
if [[ -n "$LOG_FILE" ]]; then
echo ""
echo -e " ${DIM}Full output: ${BOLD}$LOG_FILE${NC}"
fi
echo ""
}
# ── Banner ────────────────────────────────────────────────────────────────────
print_banner() {
echo ""
echo -e " ${CYAN}╭────────────────────────────╮${NC}"
echo -e " ${CYAN}│${NC} ${BOLD}${CYAN}❄${NC} ${BOLD}nix · darwin · deploy${NC} ${CYAN}│${NC}"
echo -e " ${CYAN}╰────────────────────────────╯${NC}"
echo ""
}
# ── Available configs ─────────────────────────────────────────────────────────
AVAILABLE_CONFIGS="macbook work"
get_target_user() {
case "$1" in
macbook) echo "tutods" ;;
work) echo "daniel.a.sousa" ;;
*) echo "${USER:-$(id -un)}" ;;
esac
}
validate_config() {
for c in $AVAILABLE_CONFIGS; do
[[ "$c" == "$1" ]] && return 0
done
return 1
}
# ── Pure-bash arrow-key menu ──────────────────────────────────────────────────
# _tui_draw / _tui_select use these globals so _tui_draw can be top-level.
_TUI_SELECTED=""
_TUI_ITEMS=()
_TUI_SEL=0
_tui_draw() {
local i count=${#_TUI_ITEMS[@]}
for ((i = 0; i < count; i++)); do
if ((i == _TUI_SEL)); then
printf " \033[96m▸ %s\033[0m\n" "${_TUI_ITEMS[$i]}"
else
printf " \033[2m%s\033[0m\n" "${_TUI_ITEMS[$i]}"
fi
done
}
# Returns 1 if user pressed q/ESC; result written to _TUI_SELECTED.
_tui_select() {
_TUI_ITEMS=("$@")
_TUI_SEL=0
local count=$# k1 k2 k3 quit=0
local stty_save
stty_save=$(stty -g 2>/dev/null) || stty_save=""
[[ -n "$stty_save" ]] && stty -echo 2>/dev/null || true
printf '\033[?25l\033[?7l'
_tui_draw
while true; do
IFS= read -rsn1 k1
if [[ $k1 == $'\x1b' ]]; then
IFS= read -rsn1 -t 0.1 k2 || true
IFS= read -rsn1 -t 0.1 k3 || true
if [[ $k2 == '[' ]]; then
case $k3 in
A) ((_TUI_SEL > 0)) && ((_TUI_SEL--)) ;;
B) ((_TUI_SEL < count - 1)) && ((_TUI_SEL++)) ;;
esac
else
quit=1; break
fi
elif [[ $k1 == '' ]]; then
break
elif [[ $k1 == q || $k1 == Q ]]; then
quit=1; break
fi
printf "\033[%dA" "$count"
_tui_draw
done
[[ -n "$stty_save" ]] && stty "$stty_save" 2>/dev/null || true
printf '\033[?25h\033[?7h'
printf "\033[%dA\033[J" "$count"
((quit)) && return 1
_TUI_SELECTED="${_TUI_ITEMS[$_TUI_SEL]}"
}
# ── Interactive config picker ─────────────────────────────────────────────────
# Result written to SELECTED_CONFIG (not stdout) so terminal output stays intact.
SELECTED_CONFIG=""
select_config() {
print_banner
echo -e " ${BOLD}Select a machine:${NC}"
echo ""
if command -v fzf &>/dev/null; then
SELECTED_CONFIG=$(printf '%s\n' $AVAILABLE_CONFIGS | fzf \
--height=~40% \
--layout=reverse \
--border=rounded \
--prompt=" Machine > " \
--color="border:cyan,pointer:cyan,header:cyan" \
--header="↑/↓ navigate ENTER select ESC quit") || true
[[ -z "$SELECTED_CONFIG" ]] && { echo ""; exit 0; }
else
echo -e " ${DIM}${ITALIC}↑/↓ navigate ENTER select q quit${NC}"
echo ""
# shellcheck disable=SC2086
_tui_select $AVAILABLE_CONFIGS || exit 0
SELECTED_CONFIG="$_TUI_SELECTED"
fi
}
# ── Usage ─────────────────────────────────────────────────────────────────────
show_usage() {
print_banner
echo -e " ${BOLD}Usage:${NC} $0 [configuration] [options]"
echo ""
echo -e " ${BOLD}Configurations${NC} ${DIM}(defined in flake.nix)${NC}"
echo " macbook Personal MacBook"
echo " work Work laptop"
echo -e " ${DIM} Omit to get an interactive picker.${NC}"
echo ""
echo -e " ${BOLD}Options${NC}"
echo " --build-only Build without applying"
echo " --dry-run Preview what would change (no apply)"
echo " --force Force rebuild even if no changes"
echo " --log <file> Save full command output to file"
echo " --help, -h Show this help message"
echo ""
echo -e " ${BOLD}Examples${NC}"
echo " $0 Interactive machine picker"
echo " $0 macbook Build and apply"
echo " $0 macbook --build-only Build only"
echo " $0 macbook --dry-run Preview changes"
echo " $0 macbook --log out.log Save full log"
echo " $0 work --force Force rebuild"
print_divider
exit 0
}
# ── Checks ────────────────────────────────────────────────────────────────────
check_directory() {
if [[ ! -f "flake.nix" ]]; then
print_error "Must be run from the .dotfiles directory (flake.nix not found)"
exit 1
fi
[[ -d ".hooks" ]] && git config core.hooksPath .hooks 2>/dev/null || true
}
# ── AI provider key setup ────────────────────────────────────────────────────
# ── Git identity setup ────────────────────────────────────────────────────────
setup_private_git() {
local config="$1"
local target_user
target_user=$(get_target_user "$config")
local target_home="/Users/$target_user"
[[ "$target_user" == "${USER:-}" ]] && target_home="$HOME"
local private_file="$target_home/.config/git/private"
[[ "$target_user" != "${USER:-}" ]] && print_info "Using git identity for ${target_user}"
if [[ -f "$private_file" ]]; then
print_dim "Private git config found ✓"
return 0
fi
print_warning "Private git config not found at $private_file"
echo -e " ${YELLOW}Git user.name and user.email are required.${NC}"
echo -e " ${YELLOW}See docs/secrets.md for details.${NC}"
echo ""
local name email
read -rp " Enter your git user.name: " name
read -rp " Enter your git user.email: " email
if [[ -z "$name" || -z "$email" ]]; then
print_error "Both name and email are required. Skipping private git config."
return 1
fi
if [[ "$target_user" == "${USER:-}" ]]; then
mkdir -p "$(dirname "$private_file")"
git config --file "$private_file" user.name "$name"
git config --file "$private_file" user.email "$email"
else
sudo -u "$target_user" mkdir -p "$(dirname "$private_file")"
sudo -u "$target_user" git config --file "$private_file" user.name "$name"
sudo -u "$target_user" git config --file "$private_file" user.email "$email"
fi
print_success "Private git config created at $private_file"
}
# ── Build ─────────────────────────────────────────────────────────────────────
build_config() {
local config="$1" force="$2" step="$3"
print_step "$step Building"
print_dim "Config: $config"
local build_cmd=(nix --extra-experimental-features 'nix-command flakes' build ".#darwinConfigurations.$config.system")
[[ "$force" == "true" ]] && build_cmd+=(--rebuild)
local t0=$SECONDS exit_code=0
run_cmd "Building ${config}…" "${build_cmd[@]}" || exit_code=$?
if [[ $exit_code -eq 0 ]]; then
print_success "Built in $(_fmt_elapsed $(( SECONDS - t0 )))"
rm -f "$_CMD_LOG"
else
show_errors "$_CMD_LOG" "Build"
rm -f "$_CMD_LOG"
exit 1
fi
}
# ── Dry run ───────────────────────────────────────────────────────────────────
dry_run_config() {
local config="$1" step="$2"
print_step "$step Previewing changes"
print_dim "Showing what would be built or fetched (no changes applied)"
local dry_cmd=(nix --extra-experimental-features 'nix-command flakes' build --dry-run ".#darwinConfigurations.$config.system")
local t0=$SECONDS exit_code=0
run_cmd "Previewing ${config}…" "${dry_cmd[@]}" || exit_code=$?
if [[ $exit_code -eq 0 ]]; then
print_success "Preview completed in $(_fmt_elapsed $(( SECONDS - t0 )))"
echo ""
local changes
changes=$(grep -E "^(these [0-9]+ derivations?|these [0-9]+ paths?| /nix/store)" "$_CMD_LOG" 2>/dev/null | head -30 || true)
if [[ -n "$changes" ]]; then
echo -e " ${BOLD}Changes:${NC}"
while IFS= read -r line; do
[[ "$line" =~ ^these ]] && echo -e " ${CYAN}$line${NC}" || echo -e " ${DIM}$line${NC}"
done <<< "$changes"
else
print_dim "Nothing to build — already up to date."
fi
rm -f "$_CMD_LOG"
else
show_errors "$_CMD_LOG" "Preview"
rm -f "$_CMD_LOG"
exit 1
fi
}
# ── Apply ─────────────────────────────────────────────────────────────────────
apply_config() {
local config="$1" force="$2" step="$3"
print_step "$step Applying"
print_dim "Config: $config"
local switch_cmd=(switch --flake ".#$config")
[[ "$force" == "true" ]] && switch_cmd+=(--rebuild)
local rebuild_cmd
if command -v darwin-rebuild &>/dev/null; then
print_dim "Using local darwin-rebuild"
rebuild_cmd=(sudo darwin-rebuild "${switch_cmd[@]}")
else
print_dim "Using remote darwin-rebuild"
rebuild_cmd=(sudo nix --extra-experimental-features 'nix-command flakes' run nix-darwin/nix-darwin-25.11#darwin-rebuild -- "${switch_cmd[@]}")
fi
echo ""
print_dim "Authentication required — enter sudo password if prompted"
if ! sudo -v; then
print_error "Sudo authentication failed"
exit 1
fi
echo ""
local t0=$SECONDS exit_code=0
run_cmd "Applying ${config}…" "${rebuild_cmd[@]}" || exit_code=$?
if [[ $exit_code -eq 0 ]]; then
print_success "Applied in $(_fmt_elapsed $(( SECONDS - t0 )))"
rm -f "$_CMD_LOG"
else
show_errors "$_CMD_LOG" "Apply"
rm -f "$_CMD_LOG"
exit 1
fi
}
# ── Summary ───────────────────────────────────────────────────────────────────
print_summary() {
local config="$1" mode="$2" total_secs="$3"
echo ""
print_divider
echo ""
echo -e " ${GREEN}${BOLD}✔ All done${NC}"
echo ""
printf " ${DIM}%-9s${NC} ${BOLD}${CYAN}%s${NC}\n" "Config" "$config"
printf " ${DIM}%-9s${NC} %s\n" "Mode" "$mode"
printf " ${DIM}%-9s${NC} %s\n" "Time" "$(_fmt_elapsed "$total_secs")"
[[ -n "$LOG_FILE" ]] && printf " ${DIM}%-9s${NC} %s\n" "Log" "$LOG_FILE"
echo ""
}
# ── Main ──────────────────────────────────────────────────────────────────────
main() {
local config=""
local build_only=false dry_run=false force=false
# No args — interactive picker
if [[ $# -eq 0 ]]; then
check_directory
select_config
config="$SELECTED_CONFIG"
else
case "$1" in
--help|-h) show_usage ;;
esac
if validate_config "$1"; then
config="$1"
shift
fi
fi
# Parse remaining flags
while [[ $# -gt 0 ]]; do
case "$1" in
--build-only) build_only=true ;;
--dry-run) dry_run=true ;;
--force) force=true ;;
--log)
shift
if [[ $# -eq 0 || "$1" == --* ]]; then
print_error "--log requires a file path"
exit 1
fi
LOG_FILE="$1"
printf "# nix.sh log — %s\n\n" "$(date '+%Y-%m-%d %H:%M:%S')" > "$LOG_FILE"
;;
--help|-h) show_usage ;;
*)
if [[ -z "$config" ]] && validate_config "$1"; then
config="$1"
else
print_error "Unknown option: $1"
show_usage
fi
;;
esac
shift
done
if [[ -z "$config" ]]; then
check_directory
select_config
config="$SELECTED_CONFIG"
fi
if ! validate_config "$config"; then
print_error "Unknown configuration: $config"
echo ""
echo -e " Available: ${CYAN}$AVAILABLE_CONFIGS${NC}"
exit 1
fi
check_directory
print_banner
printf " ${BOLD}%-9s${NC} ${CYAN}%s${NC}\n" "Config" "$config"
[[ "$build_only" == "true" ]] && print_dim "Mode: build only"
[[ "$dry_run" == "true" ]] && print_dim "Mode: dry run (preview only)"
[[ "$force" == "true" ]] && print_dim "Mode: force rebuild"
[[ -n "$LOG_FILE" ]] && print_dim "Log: $LOG_FILE"
print_divider
local total_start=$SECONDS
if [[ "$dry_run" == "true" ]]; then
dry_run_config "$config" "[1/1]"
print_summary "$config" "dry run" $(( SECONDS - total_start ))
elif [[ "$build_only" == "true" ]]; then
build_config "$config" "$force" "[1/1]"
print_summary "$config" "build only" $(( SECONDS - total_start ))
print_dim "Run $0 $config to apply."
echo ""
else
setup_private_git "$config"
build_config "$config" "$force" "[1/2]"
apply_config "$config" "$force" "[2/2]"
print_summary "$config" "build + apply" $(( SECONDS - total_start ))
print_dim "Tip: run brew doctor if you encounter Homebrew issues."
echo ""
fi
}
main "$@"