infra/modules/nixos/common/update.bash
zowoq 8528527e06 modules/nixos/common/update: cancel unnecessary reboots
this is needed if the host is updated to a newer generation that doesn't require a reboot or is reverted to the current generation
2025-01-06 09:52:35 +00:00

39 lines
1.3 KiB
Bash

arch=$(uname -m)
hostname=$(uname -n)
p=$(curl -L https://buildbot.nix-community.org/nix-outputs/nix-community/infra/master/"$arch"-linux.host-"$hostname")
cancel_reboot() {
if [[ -e /run/systemd/shutdown/scheduled ]]; then
shutdown -c
kexec --unload
fi
}
if [[ "$(readlink /run/current-system)" == "$p" ]]; then
cancel_reboot
exit 0
fi
nix-store --option narinfo-cache-negative-ttl 0 --realise "$p"
nix-env --profile /nix/var/nix/profiles/system --set "$p"
booted="$(readlink /run/booted-system/{initrd,kernel,kernel-modules} && cat /run/booted-system/kernel-params)"
built="$(readlink "$p"/{initrd,kernel,kernel-modules} && cat "$p"/kernel-params)"
if [[ $booted != "$built" ]]; then
if [[ -e /run/current-system ]]; then
echo "--- diff to current-system"
nvd diff /run/current-system "$p"
echo "---"
fi
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
# don't use kexec if system is virtualized, reboots are fast enough
if ! systemd-detect-virt -q; then
kexec --load "$p"/kernel --initrd="$p"/initrd --append="$(cat "$p"/kernel-params) init=$p/init"
fi
if [[ ! -e /run/systemd/shutdown/scheduled ]]; then
shutdown -r "+$(shuf -i 5-60 -n 1)"
fi
else
cancel_reboot
/nix/var/nix/profiles/system/bin/switch-to-configuration switch
fi