-
Notifications
You must be signed in to change notification settings - Fork 16
/
eval-config.nix
239 lines (225 loc) · 9.06 KB
/
eval-config.nix
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
239
{ nixosPath
, systemConfig
, legacyInstallDirs
, system ? builtins.currentSystem
}:
let
# A minimal module set for evaluating container configs.
# This significantly reduces extra-container evaluation overhead (total eval time - container eval time)
# Compatible with nixpkgs >= 16.09
baseModules = [
(nixosPath + "/modules/misc/assertions.nix")
(nixosPath + "/modules/misc/nixpkgs.nix")
(nixosPath + "/modules/misc/extra-arguments.nix")
(nixosPath + "/modules/system/activation/top-level.nix")
(nixosPath + "/modules/system/etc/etc.nix")
(nixosPath + "/modules/system/boot/systemd.nix")
nixosContainerModule
dummyOptions
];
nixosContainerModule = let
new = nixosPath + "/modules/virtualisation/nixos-containers.nix";
old = nixosPath + "/modules/virtualisation/containers.nix"; # For nixpkgs < 20.09)
in
if builtins.pathExists new then new else old;
dummyOptions = { lib, options, ... }: let
optionValue = default: lib.mkOption { inherit default; };
dummy = optionValue [];
in {
options = {
boot.kernel.sysctl = dummy;
boot.kernelModules = dummy;
boot.kernelPackages.kernel.version = optionValue "";
boot.kernelParams = dummy;
boot.loader.systemd-boot.bootCounting.enable = optionValue false;
environment.systemPackages = dummy;
networking.dhcpcd.denyInterfaces = dummy;
networking.extraHosts = dummy;
networking.proxy.envVars = optionValue {};
security = dummy;
services = {
dbus = dummy;
logrotate = dummy;
udev = dummy;
rsyslogd.enable = optionValue false;
syslog-ng.enable = optionValue false;
};
system.activationScripts = dummy;
system.fsPackages = dummy;
system.nssDatabases = dummy;
system.nssModules = dummy;
system.path = optionValue "";
system.requiredKernelConfig = dummy;
system.stateVersion = optionValue (if legacyInstallDirs then "21.11" else "22.05");
systemd.oomd = dummy;
systemd.user.generators = optionValue {};
ids.gids.keys = dummy;
ids.uids.systemd-coredump = dummy;
ids.gids.systemd-journal = dummy;
ids.gids.systemd-journal-gateway = dummy;
ids.uids.systemd-journal-gateway = dummy;
ids.gids.systemd-network = dummy;
ids.uids.systemd-network = dummy;
ids.uids.systemd-resolve = dummy;
ids.gids.systemd-resolve = dummy;
users.users.systemd-coredump = dummy;
users.users.systemd-network.group = dummy;
users.users.systemd-network.uid = dummy;
users.users.systemd-resolve.group = dummy;
users.users.systemd-resolve.uid = dummy;
users.users.systemd-journal-gateway.group = dummy;
users.users.systemd-journal-gateway.uid = dummy;
users.groups.systemd-coredump = dummy;
users.groups.systemd-network.gid = dummy;
users.groups.systemd-resolve.gid = dummy;
users.groups.keys.gid = dummy;
users.groups.systemd-journal.gid = dummy;
users.groups.systemd-journal-gateway.gid = dummy;
};
config = {
systemd.timers = lib.mkForce {};
systemd.targets = lib.mkForce {};
} // lib.optionalAttrs (options.systemd ? managerEnvironment) {
systemd.managerEnvironment = lib.mkForce {};
};
};
containerAssert = cond: name: msg: value:
if cond then value
else throw "container '${name}': ${msg}'";
assertNonNull = var:
containerAssert (var != null);
extraModule = { config, pkgs, lib, ... }: with lib; {
options = {
containers = mkOption {
type = types.attrsOf (types.submodule (
{ config, name, ... }: {
options = {
extra = {
addressPrefix = mkOption {
type = with types; nullOr str;
default = null;
description = ''
Enable privateNetwork and set
hostAddress = <addressPrefix>.1
localAddress = <addressPrefix>.2
'';
};
enableWAN = mkOption {
type = types.bool;
default = false;
description = ''
Enable WAN access inside the container by rewriting container traffic
to use the host's address (NAT).
Only active when privateNetwork == true.
'';
};
exposeLocalhost = mkOption {
type = types.bool;
default = false;
description = ''
Forward requests from the container's external interface
to the container's localhost.
Useful to test internal services from outside the container.
WARNING: This exposes the container's localhost to all users.
Only use in a trusted environment.
Only active when privateNetwork == true.
'';
};
firewallAllowHost = mkOption {
type = types.bool;
default = false;
description = ''
Always allow connections from the container host.
Only active when privateNetwork == true.
'';
};
enableSSH = mkOption {
type = types.bool;
default = builtins.getEnv("extraContainerSSH") == "1";
description = ''
Enable SSH access with an automatically generated key.
This enables the 'cssh' comand in extra-container shell.
Requires privateNetwork == true.
'';
};
};
};
config = mkMerge [
(
let
prefix = config.extra.addressPrefix;
in mkIf (prefix != null) {
privateNetwork = true;
hostAddress = "${prefix}.1";
localAddress = "${prefix}.2";
}
)
{
config = ({ pkgs, ... }@moduleArgs: mkMerge [
{
systemd.services.forward-to-localhost = mkIf (config.extra.exposeLocalhost && config.privateNetwork) {
wantedBy = [ "network.target" ];
script = assertNonNull config.localAddress name
''
option extra.exposeLocalhost requires localAddress to be non-null.
''
''
${pkgs.procps}/bin/sysctl -w net.ipv4.conf.all.route_localnet=1
${pkgs.iptables}/bin/iptables -w -t nat -I PREROUTING -p tcp \
-d ${config.localAddress} ! --dport 80 -j DNAT --to-destination 127.0.0.1
'';
};
networking.firewall.extraCommands = mkIf (config.extra.firewallAllowHost && config.privateNetwork) (
assertNonNull config.hostAddress name
''
option extra.exposeLocalhost requires hostAddress to be non-null.
''
''
iptables -w -A nixos-fw -s ${config.hostAddress} -j ACCEPT
''
);
# Silence system state warning
system.stateVersion = lib.mkDefault moduleArgs.config.system.nixos.release;
}
(mkIf config.extra.enableSSH {
services.openssh.enable = containerAssert config.privateNetwork name ''
option extra.enableSSH requires privateNetwork to be enabled.
'' true;
users.users.root.openssh.authorizedKeys.keyFiles = [
/tmp/extra-container-ssh/key.pub
];
})
]);
}
];
}
));
};
};
config = {
systemd.services = let
WANContainers = builtins.filter (c:
let cfg = config.containers.${c};
in cfg.privateNetwork && cfg.extra.enableWAN
) (builtins.attrNames config.containers);
iptables = "${pkgs.iptables}/bin/iptables";
serviceCfg = c: let
containerAddress = config.containers.${c}.localAddress;
in
assertNonNull containerAddress c
''
option extra.enableWAN requires localAddress to be non-null
''
{
preStart = "${iptables} -w -t nat -A POSTROUTING -s ${containerAddress} -j MASQUERADE";
postStop = "${iptables} -w -t nat -D POSTROUTING -s ${containerAddress} -j MASQUERADE || true";
};
in
listToAttrs (map (c: nameValuePair "container@${c}" (serviceCfg c)) WANContainers);
};
};
in
import (nixosPath + "/lib/eval-config.nix") {
inherit baseModules system;
modules = [ extraModule systemConfig ];
}