Merge branch 'autoupdate'

This commit is contained in:
Sam 2025-01-16 17:52:01 +00:00
commit be283658f6
3 changed files with 24 additions and 5 deletions

View File

@ -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. 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) 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) 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) remainingArgs="" # All remaining arguments that haven't yet been processed (will be passed to nixos-rebuild)
function usage() { function usage() {
@ -42,6 +43,10 @@ while [[ $# -gt 0 ]]; do
update=true update=true
shift shift
;; ;;
--reboot|-r)
reboot=true
shift
;;
--operation|-o) --operation|-o)
operation="$2" operation="$2"
shift shift
@ -72,7 +77,11 @@ fi
cd $flakeDir 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..." echo "Pulling the latest version of the repository..."
/run/wrappers/bin/sudo -u $user git stash
/run/wrappers/bin/sudo -u $user git pull /run/wrappers/bin/sudo -u $user git pull
if [ $update = true ]; then 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 /run/wrappers/bin/sudo -u root /run/current-system/sw/bin/nixos-rebuild $operation $options
echo "Checking if reboot is necessary" 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})) reboot_diff=$(diff <(readlink /run/booted-system/{initrd,kernel,kernel-modules}) <(readlink /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules}))
if [ -z "$reboot" ]; then if [ -n "$reboot_diff" ] && [ $reboot == true ]; then
echo "No reboot necessary, exiting."
exit 0
else
echo "System requires a reboot. Rebooting now..." echo "System requires a reboot. Rebooting now..."
reboot reboot
else
echo "No reboot necessary."
echo "Update complete."
exit 0
fi fi
echo "Update complete."
exit 0

View File

@ -91,6 +91,7 @@ in {
system.services.nixosAutoUpgrade = { system.services.nixosAutoUpgrade = {
enable = true; enable = true;
persistent = true; persistent = true;
reboot = false;
pushUpdates = true; pushUpdates = true;
configDir = "/etc/nixos"; configDir = "/etc/nixos";
onCalendar = "daily"; onCalendar = "daily";

View File

@ -20,6 +20,11 @@ in
description = "Extra flags to pass to nixos-rebuild."; description = "Extra flags to pass to nixos-rebuild.";
default = ""; 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 { onCalendar = lib.mkOption {
default = "daily"; default = "daily";
type = lib.types.str; type = lib.types.str;
@ -70,6 +75,7 @@ in
(lib.mkIf (cfg.configDir != "") "--flake ${cfg.configDir} ").content (lib.mkIf (cfg.configDir != "") "--flake ${cfg.configDir} ").content
(lib.mkIf (cfg.user != "") "--user ${cfg.user} ").content (lib.mkIf (cfg.user != "") "--user ${cfg.user} ").content
(lib.mkIf (cfg.pushUpdates) "--update ").content (lib.mkIf (cfg.pushUpdates) "--update ").content
(lib.mkIf (cfg.reboot) "--reboot ").content
(lib.mkIf (cfg.extraFlags != "") cfg.extraFlags).content (lib.mkIf (cfg.extraFlags != "") cfg.extraFlags).content
]; ];
}; };