2024-05-30 18:08:19 +00:00
|
|
|
# Run automatic updates. Replaces system.autoUpgrade.
|
2024-08-02 21:55:48 +00:00
|
|
|
{ config, lib, ... }:
|
2024-05-30 18:08:19 +00:00
|
|
|
|
|
|
|
let
|
2024-06-24 15:38:28 +00:00
|
|
|
cfg = config.aux.system.services.autoUpgrade;
|
2024-05-30 18:08:19 +00:00
|
|
|
in
|
|
|
|
{
|
|
|
|
options = {
|
2024-06-24 15:38:28 +00:00
|
|
|
aux.system.services.autoUpgrade = {
|
2024-09-08 15:58:56 +00:00
|
|
|
enable = lib.mkEnableOption "Enables automatic system updates.";
|
2024-06-01 16:56:37 +00:00
|
|
|
configDir = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "Path where your NixOS configuration files are stored.";
|
|
|
|
};
|
2024-10-01 13:57:42 +00:00
|
|
|
extraFlags = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "Extra flags to pass to nixos-rebuild.";
|
2024-10-01 14:04:28 +00:00
|
|
|
default = "";
|
2024-10-01 13:57:42 +00:00
|
|
|
};
|
2024-06-01 16:56:37 +00:00
|
|
|
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.";
|
|
|
|
};
|
2024-10-02 18:02:58 +00:00
|
|
|
operation = lib.mkOption {
|
|
|
|
type = lib.types.enum [
|
|
|
|
"boot"
|
|
|
|
"switch"
|
|
|
|
"test"
|
|
|
|
];
|
|
|
|
default = "switch";
|
|
|
|
description = "Which `nixos-rebuild` operation to perform. Defaults to `switch`.";
|
|
|
|
};
|
2024-06-01 16:56:37 +00:00
|
|
|
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-09-08 15:58:56 +00:00
|
|
|
pushUpdates = lib.mkEnableOption "Updates the flake.lock file and pushes it back to the repo.";
|
2024-06-01 16:56:37 +00:00
|
|
|
user = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
description = "The user who owns the configDir.";
|
|
|
|
};
|
2024-05-30 18:08:19 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-09-30 15:44:07 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
# Assert that system.autoUpgrade is not also enabled
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = !config.system.autoUpgrade.enable;
|
|
|
|
message = "The system.autoUpgrade option conflicts with this module.";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
# Deploy update script
|
2024-10-06 16:13:36 +00:00
|
|
|
aux.system.nixos-operations-script.enable = true;
|
2024-06-03 15:33:25 +00:00
|
|
|
|
2024-09-30 15:44:07 +00:00
|
|
|
# Pull and apply updates.
|
|
|
|
systemd = {
|
|
|
|
services."nixos-upgrade" = {
|
2024-05-30 18:08:19 +00:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
User = "root";
|
|
|
|
};
|
2024-07-25 18:33:47 +00:00
|
|
|
path = config.aux.system.corePackages;
|
2024-09-07 17:44:14 +00:00
|
|
|
unitConfig.RequiresMountsFor = cfg.configDir;
|
2024-09-30 15:44:07 +00:00
|
|
|
script = lib.strings.concatStrings [
|
2024-10-06 16:13:36 +00:00
|
|
|
"/run/current-system/sw/bin/nixos-operations-script --operation ${cfg.operation} "
|
2024-10-01 14:04:28 +00:00
|
|
|
(lib.mkIf (cfg.configDir != "") "--flake ${cfg.configDir} ").content
|
|
|
|
(lib.mkIf (cfg.user != "") "--user ${cfg.user} ").content
|
2024-10-02 18:02:58 +00:00
|
|
|
(lib.mkIf (cfg.pushUpdates) "--update ").content
|
2024-10-01 14:04:28 +00:00
|
|
|
(lib.mkIf (cfg.extraFlags != "") cfg.extraFlags).content
|
2024-09-30 15:44:07 +00:00
|
|
|
];
|
2024-05-30 18:08:19 +00:00
|
|
|
};
|
2024-09-30 15:44:07 +00:00
|
|
|
timers."nixos-upgrade" = {
|
2024-05-30 18:08:19 +00:00
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
wantedBy = [ "timers.target" ];
|
|
|
|
timerConfig = {
|
2024-06-01 16:56:37 +00:00
|
|
|
OnCalendar = cfg.onCalendar;
|
|
|
|
Persistent = cfg.persistent;
|
2024-05-31 16:45:42 +00:00
|
|
|
Unit = "nixos-upgrade.service";
|
2024-09-28 04:47:38 +00:00
|
|
|
RandomizedDelaySec = "30m";
|
2024-05-30 18:08:19 +00:00
|
|
|
};
|
|
|
|
};
|
2024-09-30 15:44:07 +00:00
|
|
|
};
|
|
|
|
};
|
2024-05-30 18:08:19 +00:00
|
|
|
}
|