blob: 3337b96cde59905317cfc1dd98fad53b5ebab90b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
{
config,
lib,
pkgs,
...
}:
let
cfg = config.personal.system;
cfgRemote = cfg.autoUpgrade.remoteBuilding;
cfgNix = config.nix;
cfgLuks = config.boot.initrd.luks.devices;
name = config.networking.hostName;
in
{
options.personal.system = {
flake = lib.mkOption {
type = with lib.types; nullOr str;
default = null;
};
autoUpgrade = {
enable = lib.mkEnableOption "automatic system and nixpkgs upgrade";
autoUpdateInputs = lib.mkOption {
type = with lib.types; listOf str;
default = [
"nixpkgs"
"my-nixpkgs/nur"
"nixos-hardware"
];
};
checkHosts = lib.mkOption {
type = with lib.types; listOf str;
default = with builtins; concatMap (match "https://([^/]*)/?") cfgNix.settings.substituters;
};
remoteBuilding = {
enable = lib.mkEnableOption "remote building of the system configuration";
builder = {
hostName = lib.mkOption {
type = lib.types.str;
default = "hephaistos";
};
domain = lib.mkOption { type = lib.types.str; };
user = lib.mkOption {
type = lib.types.str;
default = name;
};
protocol = lib.mkOption {
type = lib.types.str;
# Nix custom ssh-variant that avoids lots of "trusted-users" settings pain
default = "ssh-ng";
};
speedFactor = lib.mkOption {
type = lib.types.int;
default = 8;
};
};
};
};
};
config =
let
hasFlake = cfg.flake != null;
hasFlakeInputs = cfg.autoUpgrade.autoUpdateInputs != [ ];
reboot = config.system.autoUpgrade.allowReboot;
nixosRebuild = "nixos-rebuild ${toString config.system.autoUpgrade.flags}";
remoteBuilder = with cfgRemote.builder; "${hostName}.${domain}";
checkNetwork = {
path = [ pkgs.unixtools.ping ];
# Check network connectivity
preStart = "(${
lib.concatMapStringsSep " && " (host: "ping -c 1 ${host}") cfg.autoUpgrade.checkHosts
}) || kill -s SIGUSR1 $$";
unitConfig = {
StartLimitIntervalSec = 300;
StartLimitBurst = 5;
};
serviceConfig = lib.mkIf (!config.personal.monitoring.enable) {
Restart = "on-abort";
RestartSec = 30;
RestartMode = "direct"; # dependent units will not fail
};
};
in
lib.mkMerge [
(lib.mkIf hasFlake {
system.autoUpgrade.flake = cfg.flake;
systemd.services.flake-update = lib.mkIf hasFlakeInputs (
lib.mkMerge [
checkNetwork
{
description = "Update flake inputs";
serviceConfig.Type = "oneshot";
script =
"nix flake update --commit-lock-file --flake ${cfg.flake} "
+ lib.concatStringsSep " " cfg.autoUpgrade.autoUpdateInputs;
before = [ "nixos-upgrade.service" ];
requiredBy = [ "nixos-upgrade.service" ];
path = [
pkgs.git
cfgNix.package
];
personal.monitor = true;
}
]
);
programs.git = lib.mkIf (lib.hasPrefix "git+file" cfg.flake) {
enable = true;
config.user = lib.mkDefault {
name = "Root user of ${name}";
email = "root@${name}";
};
};
})
(lib.mkIf (cfg.autoUpgrade.enable && cfgRemote.enable) {
assertions = [
{
assertion = hasFlake && lib.hasPrefix "git+file://" cfg.flake;
message = "Auto remote upgrade is only supported when the system is specified by a flake with path of the shape 'git+file://...'";
}
];
personal.system.autoUpgrade.checkHosts = lib.mkOptionDefault [ remoteBuilder ];
programs.ssh = {
extraConfig = ''
Host ${remoteBuilder}
IdentitiesOnly yes
IdentityFile /etc/ssh/remoteBuilder
User ${cfgRemote.builder.user}
'';
knownHosts."${remoteBuilder}".publicKey =
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHvtqi8tziBuviUV8LDK2ddQQUbHdJYB02dgWTK5Olxq";
};
})
(lib.mkIf cfg.autoUpgrade.enable {
personal.boot.unattendedReboot = lib.mkIf reboot true;
system.autoUpgrade = {
enable = true;
flags = lib.optional (!hasFlake) "--upgrade-all";
};
systemd.services.nixos-upgrade = lib.mkMerge [
checkNetwork
{
path =
lib.optional reboot pkgs.coreutils
++ [
(if cfgRemote.enable then cfgNix.package else pkgs.nixos-rebuild)
]
++ lib.optional (reboot && cfgLuks ? crypt) pkgs.cryptsetup;
personal.monitor = true;
script = lib.mkForce (
lib.concatStrings [
''
## build configuration
''
(
let
in
if cfgRemote.enable then
''
# update remote flake
pushd ${lib.removePrefix "git+file://" cfg.flake}
git push --force ${cfgRemote.builder.hostName} local:master
popd
# build remotely
config=$(ssh ${remoteBuilder} -- \
'nix build --refresh --print-out-paths \
git+file://$(pwd)/nixos-configuration#nixosConfigurations.${name}.config.system.build.toplevel')
# copy result locally
nix-copy-closure --from ${remoteBuilder} "$config"
# create new generation
nix-env --profile /nix/var/nix/profiles/system \
--set "$config"
switch="$config/bin/switch-to-configuration"
''
else
''
switch="${nixosRebuild}"
''
)
''
## check whether a reboot is necessary"
''
(
if reboot then
''
$switch boot
booted="$(readlink /run/booted-system/{initrd,kernel,kernel-modules})"
built="$(readlink /nix/var/nix/profiles/system/{initrd,kernel,kernel-modules})"
reboot="$([ "$booted" = "$built" ] || echo true)"
''
else
''
reboot=""
''
)
''
## switch to new configuration
''
(
let
ifcrypt = lib.optionalString (cfgLuks ? crypt);
crypt = cfgLuks.crypt.device;
luksKey = x: "/etc/luks/keys/" + x;
in
''
if [ "$reboot" ]
then
${ifcrypt ''
cryptsetup luksAddKey ${crypt} ${luksKey "tmp"} \
--key-file ${luksKey "master"} \
--verbose
''}
shutdown -r now ${ifcrypt ''
|| cryptsetup luksRemoveKey ${crypt} \
--key-file ${luksKey "tmp"} \
--verbose
''}
else
$switch switch
fi
''
)
]
);
}
];
})
];
}
|