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"