1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let cfg = config.system.autoUpgrade; in
6
7{
8
9 options = {
10
11 system.autoUpgrade = {
12
13 enable = mkOption {
14 type = types.bool;
15 default = false;
16 description = ''
17 Whether to periodically upgrade NixOS to the latest
18 version. If enabled, a systemd timer will run
19 <literal>nixos-rebuild switch --upgrade</literal> once a
20 day.
21 '';
22 };
23
24 channel = mkOption {
25 type = types.nullOr types.str;
26 default = null;
27 example = https://nixos.org/channels/nixos-14.12-small;
28 description = ''
29 The URI of the NixOS channel to use for automatic
30 upgrades. By default, this is the channel set using
31 <command>nix-channel</command> (run <literal>nix-channel
32 --list</literal> to see the current value).
33 '';
34 };
35
36 flags = mkOption {
37 type = types.listOf types.str;
38 default = [];
39 example = [ "-I" "stuff=/home/alice/nixos-stuff" "--option" "extra-binary-caches" "http://my-cache.example.org/" ];
40 description = ''
41 Any additional flags passed to <command>nixos-rebuild</command>.
42 '';
43 };
44
45 };
46
47 };
48
49 config = {
50
51 system.autoUpgrade.flags =
52 [ "--no-build-output" ]
53 ++ (if cfg.channel == null
54 then [ "--upgrade" ]
55 else [ "-I" "nixpkgs=${cfg.channel}/nixexprs.tar.xz" ]);
56
57 systemd.services.nixos-upgrade = {
58 description = "NixOS Upgrade";
59
60 restartIfChanged = false;
61 unitConfig.X-StopOnRemoval = false;
62
63 serviceConfig.Type = "oneshot";
64
65 environment = config.nix.envVars //
66 { inherit (config.environment.sessionVariables) NIX_PATH SSL_CERT_FILE;
67 HOME = "/root";
68 };
69
70 path = [ pkgs.gnutar pkgs.xz config.nix.package ];
71
72 script = ''
73 ${config.system.build.nixos-rebuild}/bin/nixos-rebuild test ${toString cfg.flags}
74 '';
75
76 startAt = mkIf cfg.enable "04:40";
77 };
78
79 };
80
81}