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 dates = mkOption {
46 default = "04:40";
47 type = types.str;
48 description = ''
49 Specification (in the format described by
50 <citerefentry><refentrytitle>systemd.time</refentrytitle>
51 <manvolnum>7</manvolnum></citerefentry>) of the time at
52 which the update will occur.
53 '';
54 };
55
56 };
57
58 };
59
60 config = {
61
62 system.autoUpgrade.flags =
63 [ "--no-build-output" ]
64 ++ (if cfg.channel == null
65 then [ "--upgrade" ]
66 else [ "-I" "nixpkgs=${cfg.channel}/nixexprs.tar.xz" ]);
67
68 systemd.services.nixos-upgrade = {
69 description = "NixOS Upgrade";
70
71 restartIfChanged = false;
72 unitConfig.X-StopOnRemoval = false;
73
74 serviceConfig.Type = "oneshot";
75
76 environment = config.nix.envVars //
77 { inherit (config.environment.sessionVariables) NIX_PATH;
78 HOME = "/root";
79 } // config.networking.proxy.envVars;
80
81 path = [ pkgs.gnutar pkgs.xz.bin config.nix.package.out ];
82
83 script = ''
84 ${config.system.build.nixos-rebuild}/bin/nixos-rebuild switch ${toString cfg.flags}
85 '';
86
87 startAt = optional cfg.enable cfg.dates;
88 };
89
90 };
91
92}