1{ config, lib, pkgs, utils, ... }:
2
3let
4 cfg = config.systemd.sysupdate;
5
6 format = pkgs.formats.ini { listToValue = toString; };
7
8 definitionsDirectory = utils.systemdUtils.lib.definitions
9 "sysupdate.d"
10 format
11 cfg.transfers;
12in
13{
14 options.systemd.sysupdate = {
15
16 enable = lib.mkEnableOption "systemd-sysupdate" // {
17 description = ''
18 Atomically update the host OS, container images, portable service
19 images or other sources.
20
21 If enabled, updates are triggered in regular intervals via a
22 `systemd.timer` unit.
23
24 Please see
25 <https://www.freedesktop.org/software/systemd/man/systemd-sysupdate.html>
26 for more details.
27 '';
28 };
29
30 timerConfig = utils.systemdUtils.unitOptions.timerOptions.options.timerConfig // {
31 default = { };
32 description = ''
33 The timer configuration for performing the update.
34
35 By default, the upstream configuration is used:
36 <https://github.com/systemd/systemd/blob/main/units/systemd-sysupdate.timer>
37 '';
38 };
39
40 reboot = {
41 enable = lib.mkEnableOption "automatically rebooting after an update" // {
42 description = ''
43 Whether to automatically reboot after an update.
44
45 If set to `true`, the system will automatically reboot via a
46 `systemd.timer` unit but only after a new version was installed.
47
48 This uses a unit completely separate from the one performing the
49 update because it is typically advisable to download updates
50 regularly while the system is up, but delay reboots until the
51 appropriate time (i.e. typically at night).
52
53 Set this to `false` if you do not want to reboot after an update. This
54 is useful when you update a container image or another source where
55 rebooting is not necessary in order to finalize the update.
56 '';
57 };
58
59 timerConfig = utils.systemdUtils.unitOptions.timerOptions.options.timerConfig // {
60 default = { };
61 description = ''
62 The timer configuration for rebooting after an update.
63
64 By default, the upstream configuration is used:
65 <https://github.com/systemd/systemd/blob/main/units/systemd-sysupdate-reboot.timer>
66 '';
67 };
68 };
69
70 transfers = lib.mkOption {
71 type = with lib.types; attrsOf format.type;
72 default = { };
73 example = {
74 "10-uki" = {
75 Transfer = {
76 ProtectVersion = "%A";
77 };
78
79 Source = {
80 Type = "url-file";
81 Path = "https://download.example.com/";
82 MatchPattern = [ "nixos_@v+@l-@d.efi" "nixos_@v+@l.efi" "nixos_@v.efi" ];
83 };
84
85 Target = {
86 Type = "regular-file";
87 Path = "/EFI/Linux";
88 PathRelativeTo = "boot";
89 MatchPattern = ''
90 nixos_@v+@l-@d.efi"; \
91 nixos_@v+@l.efi \
92 nixos_@v.efi
93 '';
94 Mode = "0444";
95 TriesLeft = 3;
96 TriesDone = 0;
97 InstancesMax = 2;
98 };
99 };
100 };
101 description = ''
102 Specify transfers as a set of the names of the transfer files as the
103 key and the configuration as its value. The configuration can use all
104 upstream options. See
105 <https://www.freedesktop.org/software/systemd/man/sysupdate.d.html>
106 for all available options.
107 '';
108 };
109
110 };
111
112 config = lib.mkIf cfg.enable {
113
114 systemd.additionalUpstreamSystemUnits = [
115 "systemd-sysupdate.service"
116 "systemd-sysupdate.timer"
117 "systemd-sysupdate-reboot.service"
118 "systemd-sysupdate-reboot.timer"
119 ];
120
121 systemd.timers = {
122 "systemd-sysupdate" = {
123 wantedBy = [ "timers.target" ];
124 timerConfig = cfg.timerConfig;
125 };
126 "systemd-sysupdate-reboot" = lib.mkIf cfg.reboot.enable {
127 wantedBy = [ "timers.target" ];
128 timerConfig = cfg.reboot.timerConfig;
129 };
130 };
131
132 environment.etc."sysupdate.d".source = definitionsDirectory;
133 };
134
135 meta.maintainers = with lib.maintainers; [ nikstur ];
136}