-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebuild
executable file
·87 lines (73 loc) · 2.58 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
#! /usr/bin/env -S nix shell '.?submodules=1' --extra-experimental-features "nix-command flakes" --command bash
SRC="${SRC:-$HOME/.config/nixos}"
MACHINE="${MACHINE:-machine.toml}"
INTERACTIVE=${INTERACTIVE:-true}
COMMIT=${COMMIT:-true}
UPDATE=${UPDATE:-false}
# Should probably fallback to "su -c" instead
SUDO="${SUDO:-$(which doas &>/dev/null && echo doas || echo sudo)}"
cd "$SRC" || exit 1
if ugrep --quiet 'serial ?= ?""' "$MACHINE"; then
echo Serial number is missing! Grabbing from system..
SERIAL=$($SUDO cat /sys/devices/virtual/dmi/id/board_serial || exit)
sed -i -E "s:serial ?= ?\"\":serial = \"$SERIAL\":g" "$MACHINE"
HW_CONF="hardware/$SERIAL.nix"
if ! [ -f "$HW_CONF" ]; then
printf "# %s (%s)\n{ }\n" \
"$(cat /sys/devices/virtual/dmi/id/product_name)" \
"$(cat /sys/devices/virtual/dmi/id/product_version)" > "$HW_CONF"
echo "Hardware config is missing! The build will fail."
exit 1
fi
"${EDITOR:-nano}" "$MACHINE"
fi
build() {
$SUDO nixos-rebuild "$@" --flake "git+file://$SRC?submodules=1#default" \
--log-format internal-json 2>&1 | nom --json
}
machine() { awk -F'"' "/$1 ?=/{print \$2}" "$MACHINE"; }
track() {
if [ "$1" == "add" ]; then
git update-index --really-refresh "${@:2}"
elif [ "$1" == "rm" ]; then
git restore --staged "${@:2}"
git update-index --assume-unchanged --skip-worktree "${@:2}"
fi
}
update-repo() {(
cd "$1" || exit 1
git add -A; git update-index --refresh >/dev/null
git diff-index --quiet HEAD -- ||
git commit -am "AUTO: Configuration updated"
)}
# shellcheck disable=SC3010
if ! git diff-index --quiet HEAD -- &&
$COMMIT && $INTERACTIVE && ! git diff --color-words |
awk '!/--- a|+++ b|index [0-9a-z]{6}/ {print $0}' | less -RK;
then
echo "Cancelled configuration update";
exit 1
fi
$UPDATE && nix flake update
IFS=: read -ra SKIP <<< "$SKIP"
track add "$MACHINE" "${SKIP[@]}"
if [ "$1" != "copy" ]; then
BUILD=$(build "$@" && echo 1)
else
SYMLINK=${SYMLINK:-false}
"${EDITOR:-nano}" "$MACHINE"
nix copy --to "ssh://${TARGET:-$2}" \
"$SRC?submodules=1#nixosConfigurations.default.config.system.build.toplevel" \
--log-format internal-json 2>&1 | nom --json && BUILD=1
fi
track rm "$MACHINE" "${SKIP[@]}"
if $COMMIT && [ -n "$BUILD" ] && [ "$1" != "test" ]; then
update-repo .
fi
MACHINE_USER="$(machine user)"
MACHINE_PROFILE="$(machine profile)"; [ -z "$MACHINE_PROFILE" ] &&
MACHINE_PROFILE="$(machine hostname)"
${SYMLINK:-true} && "$SRC/user/homes/symlink" \
"$MACHINE_USER/$MACHINE_PROFILE" "$MACHINE_USER/all" \
"global/$MACHINE_PROFILE" "global/all" 2> /dev/null
exit 0