2015-08-07 05:28:17 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
2020-06-03 16:33:32 +02:00
|
|
|
let cfg = config.system.autoUpgrade;
|
2015-08-07 05:28:17 +02:00
|
|
|
|
2020-06-03 16:33:32 +02:00
|
|
|
in {
|
2015-08-07 05:28:17 +02:00
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
system.autoUpgrade = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to periodically upgrade NixOS to the latest
|
|
|
|
version. If enabled, a systemd timer will run
|
|
|
|
`nixos-rebuild switch --upgrade` once a
|
|
|
|
day.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-07-28 14:23:52 +02:00
|
|
|
operation = mkOption {
|
|
|
|
type = types.enum ["switch" "boot"];
|
|
|
|
default = "switch";
|
|
|
|
example = "boot";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Whether to run
|
|
|
|
`nixos-rebuild switch --upgrade` or run
|
|
|
|
`nixos-rebuild boot --upgrade`
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-06-03 16:33:32 +02:00
|
|
|
flake = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "github:kloenk/nix";
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
The Flake URI of the NixOS configuration to build.
|
|
|
|
Disables the option {option}`system.autoUpgrade.channel`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-08-07 05:28:17 +02:00
|
|
|
channel = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2020-04-01 03:11:51 +02:00
|
|
|
example = "https://nixos.org/channels/nixos-14.12-small";
|
2015-08-07 05:28:17 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
The URI of the NixOS channel to use for automatic
|
|
|
|
upgrades. By default, this is the channel set using
|
2022-08-13 11:35:46 +02:00
|
|
|
{command}`nix-channel` (run `nix-channel --list`
|
|
|
|
to see the current value).
|
2015-08-07 05:28:17 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
flags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
2020-06-03 16:33:32 +02:00
|
|
|
default = [ ];
|
|
|
|
example = [
|
|
|
|
"-I"
|
|
|
|
"stuff=/home/alice/nixos-stuff"
|
|
|
|
"--option"
|
|
|
|
"extra-binary-caches"
|
|
|
|
"http://my-cache.example.org/"
|
|
|
|
];
|
2015-08-07 05:28:17 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Any additional flags passed to {command}`nixos-rebuild`.
|
2020-06-03 16:33:32 +02:00
|
|
|
|
|
|
|
If you are using flakes and use a local repo you can add
|
|
|
|
{command}`[ "--update-input" "nixpkgs" "--commit-lock-file" ]`
|
|
|
|
to update nixpkgs.
|
2015-08-07 05:28:17 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-12-18 11:25:48 +01:00
|
|
|
dates = mkOption {
|
|
|
|
type = types.str;
|
2020-12-30 12:00:25 +01:00
|
|
|
default = "04:40";
|
|
|
|
example = "daily";
|
2015-12-18 11:25:48 +01:00
|
|
|
description = lib.mdDoc ''
|
2020-12-30 12:00:25 +01:00
|
|
|
How often or when upgrade occurs. For most desktop and server systems
|
|
|
|
a sufficient upgrade frequency is once a day.
|
|
|
|
|
|
|
|
The format is described in
|
2015-12-18 11:25:48 +01:00
|
|
|
{manpage}`systemd.time(7)`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-07-12 08:09:51 +02:00
|
|
|
allowReboot = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Reboot the system into the new generation instead of a switch
|
|
|
|
if the new generation uses a different kernel, kernel modules
|
|
|
|
or initrd than the booted system.
|
2020-02-11 17:00:26 +01:00
|
|
|
See {option}`rebootWindow` for configuring the times at which a reboot is allowed.
|
2019-07-12 08:09:51 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-02-27 22:40:10 +01:00
|
|
|
randomizedDelaySec = mkOption {
|
|
|
|
default = "0";
|
|
|
|
type = types.str;
|
|
|
|
example = "45min";
|
|
|
|
description = lib.mdDoc ''
|
2020-03-04 04:14:31 +01:00
|
|
|
Add a randomized delay before each automatic upgrade.
|
2022-03-31 06:21:21 +02:00
|
|
|
The delay will be chosen between zero and this value.
|
2020-03-04 04:14:31 +01:00
|
|
|
This value must be a time span in the format specified by
|
|
|
|
{manpage}`systemd.time(7)`
|
2020-02-27 22:40:10 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-02-11 17:00:26 +01:00
|
|
|
rebootWindow = mkOption {
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Define a lower and upper time value (in HH:MM format) which
|
|
|
|
constitute a time window during which reboots are allowed after an upgrade.
|
|
|
|
This option only has an effect when {option}`allowReboot` is enabled.
|
|
|
|
The default value of `null` means that reboots are allowed at any time.
|
|
|
|
'';
|
|
|
|
default = null;
|
|
|
|
example = { lower = "01:00"; upper = "05:00"; };
|
|
|
|
type = with types; nullOr (submodule {
|
|
|
|
options = {
|
|
|
|
lower = mkOption {
|
|
|
|
description = lib.mdDoc "Lower limit of the reboot window";
|
|
|
|
type = types.strMatching "[[:digit:]]{2}:[[:digit:]]{2}";
|
|
|
|
example = "01:00";
|
|
|
|
};
|
|
|
|
|
|
|
|
upper = mkOption {
|
|
|
|
description = lib.mdDoc "Upper limit of the reboot window";
|
|
|
|
type = types.strMatching "[[:digit:]]{2}:[[:digit:]]{2}";
|
|
|
|
example = "05:00";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-12-30 12:00:25 +01:00
|
|
|
persistent = mkOption {
|
|
|
|
default = true;
|
|
|
|
type = types.bool;
|
|
|
|
example = false;
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Takes a boolean argument. 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. Such triggering is nonetheless
|
|
|
|
subject to the delay imposed by RandomizedDelaySec=. This is
|
|
|
|
useful to catch up on missed runs of the service when the
|
|
|
|
system was powered down.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-08-07 05:28:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-03-07 01:09:02 +01:00
|
|
|
config = lib.mkIf cfg.enable {
|
2015-08-07 05:28:17 +02:00
|
|
|
|
2020-06-03 16:33:32 +02:00
|
|
|
assertions = [{
|
|
|
|
assertion = !((cfg.channel != null) && (cfg.flake != null));
|
|
|
|
message = ''
|
|
|
|
The options 'system.autoUpgrade.channels' and 'system.autoUpgrade.flake' cannot both be set.
|
|
|
|
'';
|
|
|
|
}];
|
|
|
|
|
2020-11-25 08:17:30 +01:00
|
|
|
system.autoUpgrade.flags = (if cfg.flake == null then
|
2020-02-11 17:00:26 +01:00
|
|
|
[ "--no-build-output" ] ++ optionals (cfg.channel != null) [
|
2020-06-03 16:33:32 +02:00
|
|
|
"-I"
|
|
|
|
"nixpkgs=${cfg.channel}/nixexprs.tar.xz"
|
2020-02-11 17:00:26 +01:00
|
|
|
]
|
2020-06-03 16:33:32 +02:00
|
|
|
else
|
|
|
|
[ "--flake ${cfg.flake}" ]);
|
2015-08-07 05:28:17 +02:00
|
|
|
|
|
|
|
systemd.services.nixos-upgrade = {
|
|
|
|
description = "NixOS Upgrade";
|
|
|
|
|
|
|
|
restartIfChanged = false;
|
|
|
|
unitConfig.X-StopOnRemoval = false;
|
|
|
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
2020-06-03 16:33:32 +02:00
|
|
|
environment = config.nix.envVars // {
|
|
|
|
inherit (config.environment.sessionVariables) NIX_PATH;
|
|
|
|
HOME = "/root";
|
|
|
|
} // config.networking.proxy.envVars;
|
2015-08-07 05:28:17 +02:00
|
|
|
|
2020-06-03 16:33:32 +02:00
|
|
|
path = with pkgs; [
|
|
|
|
coreutils
|
|
|
|
gnutar
|
|
|
|
xz.bin
|
|
|
|
gzip
|
|
|
|
gitMinimal
|
|
|
|
config.nix.package.out
|
2021-11-19 13:52:36 +01:00
|
|
|
config.programs.ssh.package
|
2020-06-03 16:33:32 +02:00
|
|
|
];
|
2019-07-12 08:09:51 +02:00
|
|
|
|
|
|
|
script = let
|
2020-02-11 17:00:26 +01:00
|
|
|
nixos-rebuild = "${config.system.build.nixos-rebuild}/bin/nixos-rebuild";
|
|
|
|
date = "${pkgs.coreutils}/bin/date";
|
|
|
|
readlink = "${pkgs.coreutils}/bin/readlink";
|
2022-05-05 13:23:32 +02:00
|
|
|
shutdown = "${config.systemd.package}/bin/shutdown";
|
2020-02-11 17:00:26 +01:00
|
|
|
upgradeFlag = optional (cfg.channel == null) "--upgrade";
|
2020-06-03 16:33:32 +02:00
|
|
|
in if cfg.allowReboot then ''
|
2020-02-11 17:00:26 +01:00
|
|
|
${nixos-rebuild} boot ${toString (cfg.flags ++ upgradeFlag)}
|
|
|
|
booted="$(${readlink} /run/booted-system/{initrd,kernel,kernel-modules})"
|
|
|
|
built="$(${readlink} /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})"
|
|
|
|
|
|
|
|
${optionalString (cfg.rebootWindow != null) ''
|
|
|
|
current_time="$(${date} +%H:%M)"
|
|
|
|
|
|
|
|
lower="${cfg.rebootWindow.lower}"
|
|
|
|
upper="${cfg.rebootWindow.upper}"
|
|
|
|
|
|
|
|
if [[ "''${lower}" < "''${upper}" ]]; then
|
|
|
|
if [[ "''${current_time}" > "''${lower}" ]] && \
|
|
|
|
[[ "''${current_time}" < "''${upper}" ]]; then
|
|
|
|
do_reboot="true"
|
|
|
|
else
|
|
|
|
do_reboot="false"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# lower > upper, so we are crossing midnight (e.g. lower=23h, upper=6h)
|
|
|
|
# we want to reboot if cur > 23h or cur < 6h
|
|
|
|
if [[ "''${current_time}" < "''${upper}" ]] || \
|
|
|
|
[[ "''${current_time}" > "''${lower}" ]]; then
|
|
|
|
do_reboot="true"
|
|
|
|
else
|
|
|
|
do_reboot="false"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
''}
|
|
|
|
|
|
|
|
if [ "''${booted}" = "''${built}" ]; then
|
2022-07-28 14:23:52 +02:00
|
|
|
${nixos-rebuild} ${cfg.operation} ${toString cfg.flags}
|
2020-02-11 17:00:26 +01:00
|
|
|
${optionalString (cfg.rebootWindow != null) ''
|
|
|
|
elif [ "''${do_reboot}" != true ]; then
|
|
|
|
echo "Outside of configured reboot window, skipping."
|
|
|
|
''}
|
2020-06-03 16:33:32 +02:00
|
|
|
else
|
2020-02-11 17:00:26 +01:00
|
|
|
${shutdown} -r +1
|
2020-06-03 16:33:32 +02:00
|
|
|
fi
|
|
|
|
'' else ''
|
2022-07-28 14:23:52 +02:00
|
|
|
${nixos-rebuild} ${cfg.operation} ${toString (cfg.flags ++ upgradeFlag)}
|
2020-06-03 16:33:32 +02:00
|
|
|
'';
|
2015-08-07 05:28:17 +02:00
|
|
|
|
2019-03-07 01:09:02 +01:00
|
|
|
startAt = cfg.dates;
|
2015-08-07 05:28:17 +02:00
|
|
|
|
2020-12-30 12:00:25 +01:00
|
|
|
after = [ "network-online.target" ];
|
|
|
|
wants = [ "network-online.target" ];
|
|
|
|
};
|
2020-02-27 22:40:10 +01:00
|
|
|
|
2020-12-30 12:00:25 +01:00
|
|
|
systemd.timers.nixos-upgrade = {
|
|
|
|
timerConfig = {
|
|
|
|
RandomizedDelaySec = cfg.randomizedDelaySec;
|
|
|
|
Persistent = cfg.persistent;
|
|
|
|
};
|
|
|
|
};
|
2015-08-07 05:28:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2020-02-11 17:00:26 +01:00
|
|
|
|