-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebuild
executable file
·130 lines (94 loc) · 2.7 KB
/
rebuild
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
#!/usr/bin/env bash
set -euo pipefail
_SCRIPT="$(realpath ${BASH_SOURCE})"
_SCRIPT_DIR="${_SCRIPT%/*}"
# ------------------------------------------------------------------------------
action=${1:-"switch"}
if [[ $EUID -eq 0 ]]; then
echo >&2 -n "$(tput setaf 1)"
echo >&2 -n "Don't run rebuild as root. I will call sudo internally as needed"
echo >&2 "$(tput sgr0)"
exit 1
fi
sudo() {
if [[ $EUID -ne 0 ]]; then
command sudo "$@"
return $?
fi
"$@"
return $?
}
git() {
if [[ $(stat -c %u .git) -eq 0 ]]; then
sudo git "$@"
return $?
fi
command git "$@"
return $?
}
cd "$_SCRIPT_DIR"
# --- Functions ---
heading() {
local msg="$1"
echo ''
echo ''
echo "==> ${msg} <=="
echo ''
}
# --- Track local files with git (for flake) but do not stage their contents ---
while read -r line || [[ -n $line ]]; do
# Ignore comments and empty lines
if [[ $line =~ ^[[:space:]]*# ]] || [[ $line =~ ^[[:space:]]*$ ]]; then
continue
fi
git ls-files -- "$line" | while read -r file; do
git update-index --no-skip-worktree "$file"
git restore --staged -- "$file"
if ! git ls-files --error-unmatch -- "$file" >/dev/null 2>&1; then
echo "--- Skipping worktree for: $file ---"
git add --intent-to-add -- "$file"
fi
git update-index --skip-worktree "$file"
done
done <.local-files
# --- Format and git-add ---
heading 'Formatting Nix expressions...'
sudo alejandra . # Autoformat
heading 'Changes to be committed'
git diff -U0
# --- Rebuild ---
heading 'NixOS Rebuilding...'
if command -v nh >/dev/null; then
nh os "$action" .
else
sudo nixos-rebuild $action
fi
if [[ ! $action =~ ^(switch|boot)$ ]]; then
heading 'Skipping auto-commit since there is no new generation'
exit
fi
# --- Auto-commit ---
heading 'Auto-committing...'
gen_info=$(nixos-rebuild list-generations --json | jq -r 'map(select(.current == true)) | first')
nixos_version="$(echo "$gen_info" |
jq -r '.nixosVersion')"
date="$(echo "$gen_info" |
jq -r '.date | fromdate | strftime("%Y-%m-%d %H:%M")')"
generation="$(echo "$gen_info" |
jq -r '.generation')"
kernel="$(echo "$gen_info" |
jq -r '.kernelVersion')"
git add .
# Test if there are staged changes
if git diff --cached --quiet; then
echo "Nothing to commit."
exit
fi
# NOTE: v-- There's a snowflake emoji here, but `sudo vim` does not display it for some reason.
git commit -am "❄️ REBUILD: [${action}] $(hostname): Gen. ${generation} - ${nixos_version}
Date: ${date}
Host: $(hostname)
Generation: ${generation}
NixOS version: ${nixos_version}
Kernel: ${kernel}
"