From 80da4b731e66c6cbbe41252d24448a9887f2cacd Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 16 Jan 2025 17:51:23 +0000 Subject: [PATCH 1/2] auto update --- bin/auto-update-nixos | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/bin/auto-update-nixos b/bin/auto-update-nixos index 05f812b..7d36b4a 100755 --- a/bin/auto-update-nixos +++ b/bin/auto-update-nixos @@ -7,6 +7,7 @@ hostname=$(/run/current-system/sw/bin/hostname) # The name of the host to build flakeDir="${FLAKE_DIR}" # Path to the flake file (and optionally the hostname). Defaults to the FLAKE_DIR environment variable. update=false # Whether to update flake.lock (false by default) user=$(/run/current-system/sw/bin/whoami) # Which user account to use for git commands (defaults to whoever called the script) +reboot=false remainingArgs="" # All remaining arguments that haven't yet been processed (will be passed to nixos-rebuild) function usage() { @@ -42,6 +43,10 @@ while [[ $# -gt 0 ]]; do update=true shift ;; + --reboot|-r) + reboot=true + shift + ;; --operation|-o) operation="$2" shift @@ -72,7 +77,11 @@ fi cd $flakeDir +current_branch=$(git branch --show-current) +[ "$current_branch" != "master" ] && echo "Not on master branch. Aborting auto-update" && exit 0 + echo "Pulling the latest version of the repository..." +/run/wrappers/bin/sudo -u $user git stash /run/wrappers/bin/sudo -u $user git pull if [ $update = true ]; then @@ -88,11 +97,14 @@ echo "Running this operation: nixos-rebuild $operation $options" /run/wrappers/bin/sudo -u root /run/current-system/sw/bin/nixos-rebuild $operation $options echo "Checking if reboot is necessary" -reboot=$(diff <(readlink /run/booted-system/{initrd,kernel,kernel-modules}) <(readlink /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})) -if [ -z "$reboot" ]; then - echo "No reboot necessary, exiting." - exit 0 -else +reboot_diff=$(diff <(readlink /run/booted-system/{initrd,kernel,kernel-modules}) <(readlink /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})) +if [ -n "$reboot_diff" ] && [ $reboot == true ]; then echo "System requires a reboot. Rebooting now..." reboot +else + echo "No reboot necessary." + echo "Update complete." + exit 0 fi +echo "Update complete." +exit 0 From 69a2c4cfc86d7faeef25fb08442f29063979cebd Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 16 Jan 2025 17:51:47 +0000 Subject: [PATCH 2/2] auto update --- hosts/citadel/default.nix | 1 + modules/nixos/nixosAutoUpgrade.nix | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/hosts/citadel/default.nix b/hosts/citadel/default.nix index b821383..302c580 100644 --- a/hosts/citadel/default.nix +++ b/hosts/citadel/default.nix @@ -91,6 +91,7 @@ in { system.services.nixosAutoUpgrade = { enable = true; persistent = true; + reboot = false; pushUpdates = true; configDir = "/etc/nixos"; onCalendar = "daily"; diff --git a/modules/nixos/nixosAutoUpgrade.nix b/modules/nixos/nixosAutoUpgrade.nix index c0a99e4..8609b7c 100644 --- a/modules/nixos/nixosAutoUpgrade.nix +++ b/modules/nixos/nixosAutoUpgrade.nix @@ -20,6 +20,11 @@ in description = "Extra flags to pass to nixos-rebuild."; default = ""; }; + reboot = lib.mkOption { + type = lib.types.bool; + description = "Automatically reboots the system if there is a kernel or systemd update."; + default = false; + }; onCalendar = lib.mkOption { default = "daily"; type = lib.types.str; @@ -70,6 +75,7 @@ in (lib.mkIf (cfg.configDir != "") "--flake ${cfg.configDir} ").content (lib.mkIf (cfg.user != "") "--user ${cfg.user} ").content (lib.mkIf (cfg.pushUpdates) "--update ").content + (lib.mkIf (cfg.reboot) "--reboot ").content (lib.mkIf (cfg.extraFlags != "") cfg.extraFlags).content ]; };