From 8528527e0673449de5c45a55014d98983334b699 Mon Sep 17 00:00:00 2001
From: zowoq <59103226+zowoq@users.noreply.github.com>
Date: Sun, 5 Jan 2025 13:56:30 +1000
Subject: [PATCH] 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
---
 modules/nixos/common/update.bash | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/modules/nixos/common/update.bash b/modules/nixos/common/update.bash
index fd823a7..817166c 100644
--- a/modules/nixos/common/update.bash
+++ b/modules/nixos/common/update.bash
@@ -2,7 +2,15 @@ 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
 
@@ -26,5 +34,6 @@ if [[ $booted != "$built" ]]; then
     shutdown -r "+$(shuf -i 5-60 -n 1)"
   fi
 else
+  cancel_reboot
   /nix/var/nix/profiles/system/bin/switch-to-configuration switch
 fi