From b59909b155b34f05c6392d626ab8c4cf1de420a7 Mon Sep 17 00:00:00 2001 From: clerie Date: Sun, 5 Nov 2023 18:24:37 +0100 Subject: [PATCH] pkgs/nixfiles: automatic reboot for system sync to hydra --- .../nixfiles/nixfiles-system-sync-to-hydra.sh | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/pkgs/nixfiles/nixfiles-system-sync-to-hydra.sh b/pkgs/nixfiles/nixfiles-system-sync-to-hydra.sh index c6f83a0..3111fe7 100755 --- a/pkgs/nixfiles/nixfiles-system-sync-to-hydra.sh +++ b/pkgs/nixfiles/nixfiles-system-sync-to-hydra.sh @@ -2,6 +2,21 @@ set -euo pipefail +ALLOW_REBOOT= + +while [[ $# -gt 0 ]]; do + case $1 in + --allow-reboot) + ALLOW_REBOOT=1 + shift + ;; + *) + echo "Unknown option $1" + exit 1 + ;; + esac +done + HYDRA_JOB_URL="https://hydra.clerie.de/job/nixfiles/nixfiles/nixosConfigurations.$(hostname)/latest-finished" echo "Fetching job output from ${HYDRA_JOB_URL}" @@ -13,7 +28,25 @@ nix copy --from "https://nix-cache.clerie.de" "${STORE_PATH}" echo "Add to system profile" nix-env -p "/nix/var/nix/profiles/system" --set "${STORE_PATH}" -echo "Activate system" -/nix/var/nix/profiles/system/bin/switch-to-configuration switch +echo "Set as boot target" +/nix/var/nix/profiles/system/bin/switch-to-configuration boot + +BOOTED_SYSTEM_KERNEL="$(readlink /run/booted-system/{initrd,kernel,kernel-modules})" +ACTIVATING_SYSTEM_KERNEL="$(readlink /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})" + +if [[ "$BOOTED_SYSTEM_KERNEL" != "$ACTIVATING_SYSTEM_KERNEL" ]]; then + echo "Reboot is required" + if [[ -n "$ALLOW_REBOOT" ]]; then + echo "Rebooting system now" + shutdown -r +1 "System update requires reboot" + else + echo "Automatic reboot not allowed (maybe use --allow-reboot next time)" + echo "The system upgrade is staged, please reboot manually soon" + fi +else + echo "No reboot is required" + echo "Activating system now" + /nix/var/nix/profiles/system/bin/switch-to-configuration switch +fi echo "Finished system upgrade"