Skip to content

Commit 34dd7c3

Browse files
authored
nixos/auto-upgrade: Format (#356091)
2 parents 0db4552 + b9956ce commit 34dd7c3

File tree

1 file changed

+109
-78
lines changed

1 file changed

+109
-78
lines changed

nixos/modules/tasks/auto-upgrade.nix

Lines changed: 109 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
{ config, lib, pkgs, ... }:
2-
let cfg = config.system.autoUpgrade;
3-
4-
in {
1+
{
2+
config,
3+
lib,
4+
pkgs,
5+
...
6+
}:
7+
let
8+
cfg = config.system.autoUpgrade;
9+
10+
in
11+
{
512

613
options = {
714

@@ -19,7 +26,10 @@ in {
1926
};
2027

2128
operation = lib.mkOption {
22-
type = lib.types.enum ["switch" "boot"];
29+
type = lib.types.enum [
30+
"switch"
31+
"boot"
32+
];
2333
default = "switch";
2434
example = "boot";
2535
description = ''
@@ -125,22 +135,27 @@ in {
125135
The default value of `null` means that reboots are allowed at any time.
126136
'';
127137
default = null;
128-
example = { lower = "01:00"; upper = "05:00"; };
129-
type = with lib.types; nullOr (submodule {
130-
options = {
131-
lower = lib.mkOption {
132-
description = "Lower limit of the reboot window";
133-
type = lib.types.strMatching "[[:digit:]]{2}:[[:digit:]]{2}";
134-
example = "01:00";
135-
};
136-
137-
upper = lib.mkOption {
138-
description = "Upper limit of the reboot window";
139-
type = lib.types.strMatching "[[:digit:]]{2}:[[:digit:]]{2}";
140-
example = "05:00";
138+
example = {
139+
lower = "01:00";
140+
upper = "05:00";
141+
};
142+
type =
143+
with lib.types;
144+
nullOr (submodule {
145+
options = {
146+
lower = lib.mkOption {
147+
description = "Lower limit of the reboot window";
148+
type = lib.types.strMatching "[[:digit:]]{2}:[[:digit:]]{2}";
149+
example = "01:00";
150+
};
151+
152+
upper = lib.mkOption {
153+
description = "Upper limit of the reboot window";
154+
type = lib.types.strMatching "[[:digit:]]{2}:[[:digit:]]{2}";
155+
example = "05:00";
156+
};
141157
};
142-
};
143-
});
158+
});
144159
};
145160

146161
persistent = lib.mkOption {
@@ -165,20 +180,28 @@ in {
165180

166181
config = lib.mkIf cfg.enable {
167182

168-
assertions = [{
169-
assertion = !((cfg.channel != null) && (cfg.flake != null));
170-
message = ''
171-
The options 'system.autoUpgrade.channel' and 'system.autoUpgrade.flake' cannot both be set.
172-
'';
173-
}];
183+
assertions = [
184+
{
185+
assertion = !((cfg.channel != null) && (cfg.flake != null));
186+
message = ''
187+
The options 'system.autoUpgrade.channel' and 'system.autoUpgrade.flake' cannot both be set.
188+
'';
189+
}
190+
];
174191

175-
system.autoUpgrade.flags = (if cfg.flake == null then
176-
[ "--no-build-output" ] ++ lib.optionals (cfg.channel != null) [
192+
system.autoUpgrade.flags = (
193+
if cfg.flake == null then
194+
[ "--no-build-output" ]
195+
++ lib.optionals (cfg.channel != null) [
177196
"-I"
178197
"nixpkgs=${cfg.channel}/nixexprs.tar.xz"
179198
]
180199
else
181-
[ "--refresh" "--flake ${cfg.flake}" ]);
200+
[
201+
"--refresh"
202+
"--flake ${cfg.flake}"
203+
]
204+
);
182205

183206
systemd.services.nixos-upgrade = {
184207
description = "NixOS Upgrade";
@@ -188,10 +211,13 @@ in {
188211

189212
serviceConfig.Type = "oneshot";
190213

191-
environment = config.nix.envVars // {
192-
inherit (config.environment.sessionVariables) NIX_PATH;
193-
HOME = "/root";
194-
} // config.networking.proxy.envVars;
214+
environment =
215+
config.nix.envVars
216+
// {
217+
inherit (config.environment.sessionVariables) NIX_PATH;
218+
HOME = "/root";
219+
}
220+
// config.networking.proxy.envVars;
195221

196222
path = with pkgs; [
197223
coreutils
@@ -203,54 +229,59 @@ in {
203229
config.programs.ssh.package
204230
];
205231

206-
script = let
207-
nixos-rebuild = "${config.system.build.nixos-rebuild}/bin/nixos-rebuild";
208-
date = "${pkgs.coreutils}/bin/date";
209-
readlink = "${pkgs.coreutils}/bin/readlink";
210-
shutdown = "${config.systemd.package}/bin/shutdown";
211-
upgradeFlag = lib.optional (cfg.channel == null) "--upgrade";
212-
in if cfg.allowReboot then ''
213-
${nixos-rebuild} boot ${toString (cfg.flags ++ upgradeFlag)}
214-
booted="$(${readlink} /run/booted-system/{initrd,kernel,kernel-modules})"
215-
built="$(${readlink} /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})"
216-
217-
${lib.optionalString (cfg.rebootWindow != null) ''
218-
current_time="$(${date} +%H:%M)"
219-
220-
lower="${cfg.rebootWindow.lower}"
221-
upper="${cfg.rebootWindow.upper}"
222-
223-
if [[ "''${lower}" < "''${upper}" ]]; then
224-
if [[ "''${current_time}" > "''${lower}" ]] && \
225-
[[ "''${current_time}" < "''${upper}" ]]; then
226-
do_reboot="true"
227-
else
228-
do_reboot="false"
229-
fi
230-
else
231-
# lower > upper, so we are crossing midnight (e.g. lower=23h, upper=6h)
232-
# we want to reboot if cur > 23h or cur < 6h
233-
if [[ "''${current_time}" < "''${upper}" ]] || \
234-
[[ "''${current_time}" > "''${lower}" ]]; then
235-
do_reboot="true"
232+
script =
233+
let
234+
nixos-rebuild = "${config.system.build.nixos-rebuild}/bin/nixos-rebuild";
235+
date = "${pkgs.coreutils}/bin/date";
236+
readlink = "${pkgs.coreutils}/bin/readlink";
237+
shutdown = "${config.systemd.package}/bin/shutdown";
238+
upgradeFlag = lib.optional (cfg.channel == null) "--upgrade";
239+
in
240+
if cfg.allowReboot then
241+
''
242+
${nixos-rebuild} boot ${toString (cfg.flags ++ upgradeFlag)}
243+
booted="$(${readlink} /run/booted-system/{initrd,kernel,kernel-modules})"
244+
built="$(${readlink} /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})"
245+
246+
${lib.optionalString (cfg.rebootWindow != null) ''
247+
current_time="$(${date} +%H:%M)"
248+
249+
lower="${cfg.rebootWindow.lower}"
250+
upper="${cfg.rebootWindow.upper}"
251+
252+
if [[ "''${lower}" < "''${upper}" ]]; then
253+
if [[ "''${current_time}" > "''${lower}" ]] && \
254+
[[ "''${current_time}" < "''${upper}" ]]; then
255+
do_reboot="true"
256+
else
257+
do_reboot="false"
258+
fi
259+
else
260+
# lower > upper, so we are crossing midnight (e.g. lower=23h, upper=6h)
261+
# we want to reboot if cur > 23h or cur < 6h
262+
if [[ "''${current_time}" < "''${upper}" ]] || \
263+
[[ "''${current_time}" > "''${lower}" ]]; then
264+
do_reboot="true"
265+
else
266+
do_reboot="false"
267+
fi
268+
fi
269+
''}
270+
271+
if [ "''${booted}" = "''${built}" ]; then
272+
${nixos-rebuild} ${cfg.operation} ${toString cfg.flags}
273+
${lib.optionalString (cfg.rebootWindow != null) ''
274+
elif [ "''${do_reboot}" != true ]; then
275+
echo "Outside of configured reboot window, skipping."
276+
''}
236277
else
237-
do_reboot="false"
278+
${shutdown} -r +1
238279
fi
239-
fi
240-
''}
241-
242-
if [ "''${booted}" = "''${built}" ]; then
243-
${nixos-rebuild} ${cfg.operation} ${toString cfg.flags}
244-
${lib.optionalString (cfg.rebootWindow != null) ''
245-
elif [ "''${do_reboot}" != true ]; then
246-
echo "Outside of configured reboot window, skipping."
247-
''}
280+
''
248281
else
249-
${shutdown} -r +1
250-
fi
251-
'' else ''
252-
${nixos-rebuild} ${cfg.operation} ${toString (cfg.flags ++ upgradeFlag)}
253-
'';
282+
''
283+
${nixos-rebuild} ${cfg.operation} ${toString (cfg.flags ++ upgradeFlag)}
284+
'';
254285

255286
startAt = cfg.dates;
256287

0 commit comments

Comments
 (0)