dot
vizie's NixOS desktop: Hyprland, CachyOS kernel and gaming tweaks
| 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 "Desktop" half of the |
| 4 | # control centre's update 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. |
| 15 | set -E |
| 16 | |
| 17 | repo=${DOT_REPO:-$HOME/git/dot} |
| 18 | tmp="" |
| 19 | |
| 20 | step() { printf '\n\e[1;34m==> %s\e[0m\n' "$*"; } |
| 21 | note() { printf '\e[1;33m%s\e[0m\n' "$*"; } |
| 22 | wait_close() { read -rp "Press Enter to close. " _ || true; } |
| 23 | |
| 24 | die() { |
| 25 | trap - ERR |
| 26 | printf '\n\e[1;31m%s\e[0m\n' "$*" |
| 27 | wait_close |
| 28 | exit 1 |
| 29 | } |
| 30 | |
| 31 | cleanup() { |
| 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 | |
| 39 | trap 'die "The update failed (see above). Nothing was rebooted."' ERR |
| 40 | trap cleanup EXIT |
| 41 | |
| 42 | step "Fetching $repo" |
| 43 | git -C "$repo" worktree prune |
| 44 | git -C "$repo" fetch origin main |
| 45 | git -C "$repo" merge-base --is-ancestor origin/main main || |
| 46 | die "main in $repo is behind origin/main (or has diverged); pull first." |
| 47 | base=$(git -C "$repo" rev-parse main) |
| 48 | ahead=$(git -C "$repo" rev-list --count origin/main..main) |
| 49 | dirty=$(git -C "$repo" status --porcelain) |
| 50 | |
| 51 | tmp=$(mktemp -d -t dot-update.XXXXXX) |
| 52 | step "Creating a worktree of main in $tmp" |
| 53 | git -C "$repo" worktree add --detach "$tmp" main |
| 54 | cd "$tmp" || die "Could not enter $tmp." |
| 55 | |
| 56 | step "Updating flake inputs" |
| 57 | nix flake update --commit-lock-file |
| 58 | if [ "$(git rev-parse HEAD)" = "$base" ]; then |
| 59 | printf '\nEverything is already up to date.\n' |
| 60 | wait_close |
| 61 | exit 0 |
| 62 | fi |
| 63 | |
| 64 | step "Building" |
| 65 | nixos-rebuild build --flake "$tmp" |
| 66 | |
| 67 | step "What changes" |
| 68 | git --no-pager log -1 --format=%b |
| 69 | nvd diff /run/current-system "$tmp/result" |
| 70 | |
| 71 | echo |
| 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." |
| 74 | read -rp "Commit, push, install and reboot? [y/N] " answer || answer="" |
| 75 | case $answer in |
| 76 | [yY]*) ;; |
| 77 | *) |
| 78 | echo "Nothing was changed." |
| 79 | sleep 1 |
| 80 | exit 0 |
| 81 | ;; |
| 82 | esac |
| 83 | |
| 84 | step "Pushing" |
| 85 | git push origin HEAD:main |
| 86 | |
| 87 | step "Installing as the next boot entry" |
| 88 | nixos-rebuild boot --sudo --flake "$tmp" |
| 89 | |
| 90 | step "Updating main in $repo" |
| 91 | commit=$(git rev-parse HEAD) |
| 92 | if [ "$(git -C "$repo" symbolic-ref --quiet --short HEAD)" = main ]; then |
| 93 | git -C "$repo" merge --ff-only "$commit" |
| 94 | else |
| 95 | git -C "$repo" update-ref refs/heads/main "$commit" "$base" |
| 96 | fi |
| 97 | cd "$repo" |
| 98 | cleanup |
| 99 | |
| 100 | trap 'printf "\nReboot cancelled. The update boots next time.\n"; wait_close; exit 0' INT |
| 101 | step "Rebooting in 5 seconds (Ctrl+C to cancel)" |
| 102 | sleep 5 |
| 103 | systemctl reboot |