blob: ca60ac77fa37f784b52974d443cd279347275e2d (
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
|
{
config,
lib,
pkgs,
...
}: let
cfg = config.personal.nix;
hasFlake = cfg.flake != null;
hasFlakeInputs = cfg.autoUpgrade.autoUpdateInputs != [];
checkNetwork = {
preStart = "${pkgs.host}/bin/host 9.9.9.9 || exit 100"; # Check network connectivity
unitConfig = {
StartLimitIntervalSec = 300;
StartLimitBurst = 5;
};
serviceConfig = {
RestartForceExitStatus = "100";
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"];
};
};
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;
};
};
};
};
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"];
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 {
system.autoUpgrade = {
enable = true;
flake = cfg.flake;
flags = lib.optional (!hasFlake) "--upgrade-all";
};
systemd.services.nixos-upgrade = lib.mkMerge [
checkNetwork
{
personal.monitor = true;
}
];
})
(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"];
wantedBy = ["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 {
nix = {
distributedBuilds = true;
settings.builders-use-substitutes = true;
buildMachines = with cfg.remoteBuilds.machines.hephaistos;
lib.optional enable {
inherit protocol speedFactor;
hostName = "hephaistos.${domain}";
system = "x86_64-linux";
maxJobs = 4;
supportedFeatures = ["nixos-test" "benchmark" "big-parallel" "kvm"];
mandatoryFeatures = [];
};
};
programs.ssh = with cfg.remoteBuilds.machines.hephaistos; {
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";
};
})
]);
}
|