1
0
Fork 0
nix-configuration/modules/nixos/services/autoupgrade/default.nix

89 lines
3.1 KiB
Nix
Raw Normal View History

# Run automatic updates. Replaces system.autoUpgrade.
2024-08-02 21:55:48 +00:00
{ config, lib, ... }:
let
cfg = config.aux.system.services.autoUpgrade;
in
{
options = {
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.";
};
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-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.";
};
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-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
aux.system.nixos-operations-script.enable = true;
2024-09-30 15:44:07 +00:00
# Pull and apply updates.
systemd = {
services."nixos-upgrade" = {
serviceConfig = {
Type = "oneshot";
User = "root";
};
path = config.aux.system.corePackages;
2024-09-07 17:44:14 +00:00
unitConfig.RequiresMountsFor = cfg.configDir;
script =
"/run/current-system/sw/bin/nixos-operations-script --operation ${cfg.operation} "
+ (if (cfg.configDir != "") then "--flake ${cfg.configDir} " else "")
+ (if (cfg.user != "") then "--user ${cfg.user} " else "")
+ (if (cfg.pushUpdates) then "--update " else "")
+ (if (cfg.extraFlags != "") then cfg.extraFlags else "");
};
2024-09-30 15:44:07 +00:00
timers."nixos-upgrade" = {
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;
Unit = "nixos-upgrade.service";
RandomizedDelaySec = "30m";
};
};
2024-09-30 15:44:07 +00:00
};
};
}