2015-08-07 05:28:17 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let cfg = config.system.autoUpgrade; in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
system.autoUpgrade = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to periodically upgrade NixOS to the latest
|
|
|
|
version. If enabled, a systemd timer will run
|
|
|
|
<literal>nixos-rebuild switch --upgrade</literal> once a
|
|
|
|
day.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
channel = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = https://nixos.org/channels/nixos-14.12-small;
|
|
|
|
description = ''
|
|
|
|
The URI of the NixOS channel to use for automatic
|
|
|
|
upgrades. By default, this is the channel set using
|
|
|
|
<command>nix-channel</command> (run <literal>nix-channel
|
|
|
|
--list</literal> to see the current value).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
flags = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "-I" "stuff=/home/alice/nixos-stuff" "--option" "extra-binary-caches" "http://my-cache.example.org/" ];
|
|
|
|
description = ''
|
|
|
|
Any additional flags passed to <command>nixos-rebuild</command>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-12-18 11:25:48 +01:00
|
|
|
dates = mkOption {
|
|
|
|
default = "04:40";
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
Specification (in the format described by
|
|
|
|
<citerefentry><refentrytitle>systemd.time</refentrytitle>
|
2017-03-21 08:27:56 +01:00
|
|
|
<manvolnum>7</manvolnum></citerefentry>) of the time at
|
2015-12-18 11:25:48 +01:00
|
|
|
which the update will occur.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-07-12 08:09:51 +02:00
|
|
|
allowReboot = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
|
|
|
description = ''
|
|
|
|
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-27 22:40:10 +01:00
|
|
|
randomizedDelaySec = mkOption {
|
|
|
|
default = "0";
|
|
|
|
type = types.str;
|
|
|
|
example = "45min";
|
|
|
|
description = ''
|
2020-03-04 04:14:31 +01:00
|
|
|
Add a randomized delay before each automatic upgrade.
|
|
|
|
The delay will be chozen between zero and this value.
|
|
|
|
This value must be a time span in the format specified by
|
|
|
|
<citerefentry><refentrytitle>systemd.time</refentrytitle>
|
|
|
|
<manvolnum>7</manvolnum></citerefentry>
|
2020-02-27 22:40:10 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
|
|
|
system.autoUpgrade.flags =
|
|
|
|
[ "--no-build-output" ]
|
|
|
|
++ (if cfg.channel == null
|
|
|
|
then [ "--upgrade" ]
|
|
|
|
else [ "-I" "nixpkgs=${cfg.channel}/nixexprs.tar.xz" ]);
|
|
|
|
|
|
|
|
systemd.services.nixos-upgrade = {
|
|
|
|
description = "NixOS Upgrade";
|
|
|
|
|
|
|
|
restartIfChanged = false;
|
|
|
|
unitConfig.X-StopOnRemoval = false;
|
|
|
|
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
|
|
|
|
environment = config.nix.envVars //
|
2016-02-01 10:18:31 +01:00
|
|
|
{ inherit (config.environment.sessionVariables) NIX_PATH;
|
2015-08-07 05:28:17 +02:00
|
|
|
HOME = "/root";
|
2017-08-22 10:32:13 +02:00
|
|
|
} // config.networking.proxy.envVars;
|
2015-08-07 05:28:17 +02:00
|
|
|
|
2019-08-27 22:24:55 +02:00
|
|
|
path = with pkgs; [ coreutils gnutar xz.bin gzip gitMinimal config.nix.package.out ];
|
2019-07-12 08:09:51 +02:00
|
|
|
|
|
|
|
script = let
|
|
|
|
nixos-rebuild = "${config.system.build.nixos-rebuild}/bin/nixos-rebuild";
|
|
|
|
in
|
|
|
|
if cfg.allowReboot then ''
|
|
|
|
${nixos-rebuild} boot ${toString cfg.flags}
|
|
|
|
booted="$(readlink /run/booted-system/{initrd,kernel,kernel-modules})"
|
|
|
|
built="$(readlink /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})"
|
|
|
|
if [ "$booted" = "$built" ]; then
|
|
|
|
${nixos-rebuild} switch ${toString cfg.flags}
|
|
|
|
else
|
|
|
|
/run/current-system/sw/bin/shutdown -r +1
|
|
|
|
fi
|
|
|
|
'' else ''
|
|
|
|
${nixos-rebuild} switch ${toString cfg.flags}
|
|
|
|
'';
|
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-02-27 22:40:10 +01:00
|
|
|
systemd.timers.nixos-upgrade.timerConfig.RandomizedDelaySec = cfg.randomizedDelaySec;
|
|
|
|
|
2015-08-07 05:28:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|