dot

vizie's NixOS desktop: Hyprland, CachyOS kernel and gaming tweaks

103 lines3032 bytesexecutable
1#!/usr/bin/env bash
2# Updates NixOS from a throwaway worktree of this repo, so the checkout you edit in
3# is left alone until the update is done. Started from the control centre's
4# "Update NixOS" tile in a kitty window (installed as `nixos-update`,
5# modules/home/packages.nix, which sets DOT_REPO).
6#
7# 1. a temporary worktree of main
8# 2. nix flake update, committed there
9# 3. nixos-rebuild build, then nvd's list of what changed against the running system
10# 4. asks before going on; then pushes the commit, installs it as the next boot
11# entry (nixos-rebuild boot), brings your checkout's main up to date and reboots
12#
13# If anything fails, the window stays open until you press Enter, and nothing is
14# rebooted.
15set -E
16
17repo=${DOT_REPO:-$HOME/git/dot}
18tmp=""
19
20step() { printf '\n\e[1;34m==> %s\e[0m\n' "$*"; }
21note() { printf '\e[1;33m%s\e[0m\n' "$*"; }
22wait_close() { read -rp "Press Enter to close. " _ || true; }
23
24die() {
25 trap - ERR
26 printf '\n\e[1;31m%s\e[0m\n' "$*"
27 wait_close
28 exit 1
29}
30
31cleanup() {
32 if [ -n "$tmp" ]; then
33 git -C "$repo" worktree remove --force "$tmp" 2>/dev/null || true
34 rm -rf "$tmp"
35 tmp=""
36 fi
37}
38
39trap 'die "The update failed (see above). Nothing was rebooted."' ERR
40trap cleanup EXIT
41
42step "Fetching $repo"
43git -C "$repo" worktree prune
44git -C "$repo" fetch origin main
45git -C "$repo" merge-base --is-ancestor origin/main main ||
46 die "main in $repo is behind origin/main (or has diverged); pull first."
47base=$(git -C "$repo" rev-parse main)
48ahead=$(git -C "$repo" rev-list --count origin/main..main)
49dirty=$(git -C "$repo" status --porcelain)
50
51tmp=$(mktemp -d -t dot-update.XXXXXX)
52step "Creating a worktree of main in $tmp"
53git -C "$repo" worktree add --detach "$tmp" main
54cd "$tmp" || die "Could not enter $tmp."
55
56step "Updating flake inputs"
57nix flake update --commit-lock-file
58if [ "$(git rev-parse HEAD)" = "$base" ]; then
59 printf '\nEverything is already up to date.\n'
60 wait_close
61 exit 0
62fi
63
64step "Building"
65nixos-rebuild build --flake "$tmp"
66
67step "What changes"
68git --no-pager log -1 --format=%b
69nvd diff /run/current-system "$tmp/result"
70
71echo
72[ -z "$dirty" ] || note "Uncommitted changes in $repo are not part of this update."
73[ "$ahead" -eq 0 ] || note "main has $ahead commit(s) not on GitHub yet; they are pushed too."
74read -rp "Commit, push, install and reboot? [y/N] " answer || answer=""
75case $answer in
76 [yY]*) ;;
77 *)
78 echo "Nothing was changed."
79 sleep 1
80 exit 0
81 ;;
82esac
83
84step "Pushing"
85git push origin HEAD:main
86
87step "Installing as the next boot entry"
88nixos-rebuild boot --sudo --flake "$tmp"
89
90step "Updating main in $repo"
91commit=$(git rev-parse HEAD)
92if [ "$(git -C "$repo" symbolic-ref --quiet --short HEAD)" = main ]; then
93 git -C "$repo" merge --ff-only "$commit"
94else
95 git -C "$repo" update-ref refs/heads/main "$commit" "$base"
96fi
97cd "$repo"
98cleanup
99
100trap 'printf "\nReboot cancelled. The update boots next time.\n"; wait_close; exit 0' INT
101step "Rebooting in 5 seconds (Ctrl+C to cancel)"
102sleep 5
103systemctl reboot