blob: 763d5d14ac5a400fea94a2390ae42c8e56819eca (
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
|
{
config,
lib,
pkgs,
...
}: let
cfg = config.personal.nix;
hasFlake = cfg.flake != null;
hasFlakeInputs = cfg.autoUpgrade.autoUpdateInputs != [];
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 = {
Restart = "on-abort";
RestartSec = 30;
};
};
in {
options.personal.nix = {
enable = lib.mkEnableOption "nix configuration";
autoUpgrade = {
enable = lib.mkEnableOption "automatic system and nixpkgs upgrade";
autoUpdateInputs = lib.mkOption {
type = with lib.types; listOf str;
default = ["nixpkgs"];
};
checkHosts = lib.mkOption {
type = with lib.types; listOf str;
default = config.nix.settings.substituters;
};
};
flake = lib.mkOption {
type = with lib.types; nullOr str;
default = null;
};
gc.enable = lib.mkEnableOption "garbage collection";
remoteBuilds = {
enable = lib.mkEnableOption "remote builds";
machines.hephaistos = {
enable = lib.mkEnableOption "hephaistos remote builder";
domain = lib.mkOption {
type = lib.types.str;
};
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 = 4;
};
require = lib.mkOption {
type =
lib.types.bool;
default = true;
description = ''
Whether this remote builder is required to build the configuration.
If so, network connectivity to this remote builder will be checked prior to building.
'';
};
};
};
};
config = lib.mkIf cfg.enable (lib.mkMerge [
{
nixpkgs = {
config.allowUnfree = true;
flake = lib.mkDefault {
setNixPath = false;
setFlakeRegistry = false;
};
};
nix = {
package = pkgs.lix;
settings = {
auto-optimise-store = true;
experimental-features = ["nix-command" "flakes" "recursive-nix"];
substituters = ["https://devenv.cachix.org/" "https://nix-community.cachix.org/"];
trusted-public-keys = ["devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=" "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="];
};
extraOptions = ''
!include secrets.conf
'';
};
}
(lib.mkIf cfg.gc.enable {
nix.gc = {
automatic = true;
dates = "daily";
options = "--delete-old";
};
systemd.services = {
nix-gc = {
after = ["nixos-upgrade.service"];
personal.monitor = true;
};
nix-gc-remove-dead-roots = {
enable = cfg.gc.enable;
description = "Remove dead symlinks in /nix/var/nix/gcroots";
serviceConfig.Type = "oneshot";
script = "find /nix/var/nix/gcroots -xtype l -delete";
before = ["nix-gc.service"];
wantedBy = ["nix-gc.service"];
personal.monitor = true;
};
nix-gc-remove-old-hm-gens = let
user = config.personal.user;
in {
enable = user.enable && user.homeManager.enable;
description = "Remove old Home Manager generations for user ${user.name}";
serviceConfig.Type = "oneshot";
script = "${pkgs.nix}/bin/nix-env --profile /home/${user.name}/.local/state/nix/profiles/home-manager --delete-generations old";
before = ["nix-gc.service"];
wantedBy = ["nix-gc.service"];
personal.monitor = true;
};
};
})
(lib.mkIf cfg.autoUpgrade.enable {
personal.boot.unattendedReboot = lib.mkIf config.system.autoUpgrade.allowReboot true;
system.autoUpgrade = {
enable = true;
flake = cfg.flake;
flags = lib.optional (!hasFlake) "--upgrade-all";
};
systemd.services.nixos-upgrade = lib.mkMerge [
checkNetwork
{
preStart = lib.mkAfter ''
${config.system.build.nixos-rebuild}/bin/nixos-rebuild dry-build ${toString config.system.autoUpgrade.flags}
'';
personal.monitor = true;
}
(let
luksCfg = config.boot.initrd.luks.devices;
cryptExists = luksCfg ? crypt;
cryptCfg = luksCfg.crypt;
in
lib.mkIf (cryptExists && config.system.autoUpgrade.allowReboot) {
script = lib.mkAfter ''
cryptsetup --verbose luksAddKey --key-file /etc/luks/keys/master ${cryptCfg.device} /etc/luks/keys/tmp
'';
postStop = ''
# if a reboot due to nixos-upgrade happens, it should occur within a minute
sleep 120
# if no reboot has happened, disable any leftover keyfile
while cryptsetup --verbose luksRemoveKey ${cryptCfg.device} --key-file /etc/luks/keys/tmp
do
:
done
'';
})
];
})
(lib.mkIf hasFlake {
systemd.services.flake-update = lib.mkIf hasFlakeInputs (lib.mkMerge [
checkNetwork
{
unitConfig.Description = "Update flake inputs";
serviceConfig = {
ExecStart = "${config.nix.package}/bin/nix flake update --commit-lock-file --flake ${cfg.flake} " + lib.concatStringsSep " " cfg.autoUpgrade.autoUpdateInputs;
Type = "oneshot"; # Ensure that it finishes before starting nixos-upgrade
};
before = ["nixos-upgrade.service"];
requiredBy = ["nixos-upgrade.service"];
path = [pkgs.git];
personal.monitor = true;
}
]);
programs.git = lib.mkIf (lib.hasPrefix "git+file" cfg.flake) {
enable = true;
config.user = lib.mkDefault {
name = "Root user of ${config.networking.hostName}";
email = "root@${config.networking.hostName}";
};
};
})
(lib.mkIf cfg.remoteBuilds.enable (with cfg.remoteBuilds.machines.hephaistos; {
nix = {
distributedBuilds = true;
settings.builders-use-substitutes = true;
buildMachines = lib.optional enable {
inherit protocol speedFactor;
hostName = "hephaistos.${domain}";
system = "x86_64-linux";
maxJobs = 4;
supportedFeatures = ["nixos-test" "benchmark" "big-parallel" "kvm" "recursive-nix"];
mandatoryFeatures = [];
};
};
personal.nix.autoUpgrade.checkHosts = lib.mkOptionDefault (lib.optional require "hephaistos.${domain}");
programs.ssh = {
extraConfig = lib.optionalString enable ''
Host hephaistos.${domain}
# Prevent using ssh-agent or another keyfile, useful for testing
IdentitiesOnly yes
IdentityFile /etc/ssh/nixremote
# The weakly privileged user on the remote builder
# If not set, 'root' is used – which will hopefully fail
User nixremote
'';
knownHosts."hephaistos.${domain}".publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHvtqi8tziBuviUV8LDK2ddQQUbHdJYB02dgWTK5Olxq";
};
}))
]);
}
|