2024-05-30 14:08:19 -04:00
|
|
|
# Run automatic updates. Replaces system.autoUpgrade.
|
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
|
|
|
|
let
|
2024-06-24 11:38:28 -04:00
|
|
|
cfg = config.aux.system.services.autoUpgrade;
|
2024-05-30 14:08:19 -04:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
2024-06-24 11:38:28 -04:00
|
|
|
aux.system.services.autoUpgrade = {
|
2024-06-24 14:26:41 -04:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc "Enables automatic system updates.");
|
2024-06-02 13:41:18 -04:00
|
|
|
branches = lib.mkOption {
|
|
|
|
type = lib.types.attrs;
|
|
|
|
description = "Which local and remote branches to compare.";
|
|
|
|
default = {
|
|
|
|
local = "main";
|
|
|
|
remote = "main";
|
|
|
|
remoteName = "origin";
|
|
|
|
};
|
|
|
|
};
|
2024-06-01 12:56:37 -04:00
|
|
|
configDir = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "Path where your NixOS configuration files are stored.";
|
|
|
|
};
|
|
|
|
onCalendar = lib.mkOption {
|
|
|
|
default = "daily";
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "How frequently to run updates. See systemd.timer(5) and systemd.time(7) for configuration details.";
|
|
|
|
};
|
|
|
|
persistent = lib.mkOption {
|
|
|
|
default = true;
|
|
|
|
type = lib.types.bool;
|
|
|
|
description = "If true, the time when the service unit was last triggered is stored on disk. When the timer is activated, the service unit is triggered immediately if it would have been triggered at least once during the time when the timer was inactive. This is useful to catch up on missed runs of the service when the system was powered down.";
|
|
|
|
};
|
2024-05-30 14:08:19 -04:00
|
|
|
pushUpdates = lib.mkEnableOption (
|
|
|
|
lib.mdDoc "Updates the flake.lock file and pushes it back to the repo."
|
|
|
|
);
|
2024-06-01 12:56:37 -04:00
|
|
|
user = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "The user who owns the configDir.";
|
|
|
|
};
|
2024-05-30 14:08:19 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkMerge [
|
|
|
|
(lib.mkIf cfg.enable {
|
2024-06-03 11:33:25 -04:00
|
|
|
# Assert that system.autoUpgrade is not also enabled
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = !config.system.autoUpgrade.enable;
|
|
|
|
message = "The system.autoUpgrade option conflicts with this module.";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2024-05-30 14:08:19 -04:00
|
|
|
# Pull and apply updates.
|
2024-05-31 12:45:42 -04:00
|
|
|
systemd.services."nixos-upgrade" = {
|
2024-05-30 14:08:19 -04:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
User = "root";
|
|
|
|
};
|
2024-07-25 14:33:47 -04:00
|
|
|
path = config.aux.system.corePackages;
|
2024-06-02 11:30:09 -04:00
|
|
|
# Git diffing strategy courtesy of https://stackoverflow.com/a/40255467
|
2024-05-30 14:08:19 -04:00
|
|
|
script = ''
|
2024-06-01 12:56:37 -04:00
|
|
|
cd ${cfg.configDir}
|
2024-05-30 15:38:48 -04:00
|
|
|
# Check if there are changes from Git.
|
2024-05-31 12:45:42 -04:00
|
|
|
echo "Pulling latest version..."
|
2024-06-01 12:56:37 -04:00
|
|
|
sudo -u ${cfg.user} git fetch
|
2024-06-02 13:41:18 -04:00
|
|
|
sudo -u ${cfg.user} git diff --quiet --exit-code ${cfg.branches.local} ${cfg.branches.remoteName}/${cfg.branches.remote} || true
|
2024-05-30 15:38:48 -04:00
|
|
|
# If we have changes (git diff returns 1), pull changes and run the update
|
2024-05-30 14:08:19 -04:00
|
|
|
if [ $? -eq 1 ]; then
|
2024-05-31 12:45:42 -04:00
|
|
|
echo "Updates found, running nixos-rebuild..."
|
2024-06-01 12:56:37 -04:00
|
|
|
sudo -u ${cfg.user} git pull --recurse-submodules
|
|
|
|
nixos-rebuild switch --flake .
|
2024-05-31 12:45:42 -04:00
|
|
|
else
|
|
|
|
echo "No updates found. Exiting."
|
2024-05-30 14:08:19 -04:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
2024-06-01 12:56:37 -04:00
|
|
|
systemd.timers."nixos-upgrade" = {
|
2024-05-30 14:08:19 -04:00
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
2024-06-01 12:56:37 -04:00
|
|
|
OnCalendar = cfg.onCalendar;
|
|
|
|
Persistent = cfg.persistent;
|
2024-05-31 12:45:42 -04:00
|
|
|
Unit = "nixos-upgrade.service";
|
2024-05-30 14:08:19 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
(lib.mkIf cfg.pushUpdates {
|
|
|
|
# Automatically update Flake configuration for other hosts to use
|
2024-05-31 12:45:42 -04:00
|
|
|
systemd.services."nixos-upgrade-flake" = {
|
2024-05-30 14:08:19 -04:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
2024-06-01 12:56:37 -04:00
|
|
|
User = cfg.user;
|
2024-05-30 14:08:19 -04:00
|
|
|
};
|
2024-07-25 14:33:47 -04:00
|
|
|
path = config.aux.system.corePackages;
|
2024-05-30 14:08:19 -04:00
|
|
|
script = ''
|
|
|
|
set -eu
|
2024-06-01 12:56:37 -04:00
|
|
|
cd ${cfg.configDir}
|
2024-05-30 15:38:48 -04:00
|
|
|
# Make sure we're up-to-date
|
2024-05-31 12:45:42 -04:00
|
|
|
echo "Pulling the latest version..."
|
2024-05-30 14:08:19 -04:00
|
|
|
git pull --recurse-submodules
|
2024-05-30 23:22:49 -04:00
|
|
|
nix flake update --commit-lock-file
|
|
|
|
git push
|
2024-05-30 14:08:19 -04:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-06-01 12:56:37 -04:00
|
|
|
systemd.timers."nixos-upgrade-flake" = {
|
2024-05-30 14:08:19 -04:00
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
2024-06-01 12:56:37 -04:00
|
|
|
OnCalendar = cfg.onCalendar;
|
|
|
|
Persistent = cfg.persistent;
|
2024-05-31 12:45:42 -04:00
|
|
|
Unit = "nixos-upgrade-flake.service";
|
2024-05-30 14:08:19 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
|
|
|
}
|