blob: ed2bf9ef8cdbca94be5ccde64547e1b25b6045d8 (
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
{
lib,
config,
pkgs,
...
}:
let
utils = import ./utils.nix { inherit lib; };
inherit (lib)
mkOption
types
assertMsg
optional
optionalString
getExe
concatStringsSep
filterAttrs
listToAttrs
attrsToList
imap0
attrNames
nameValuePair
;
inherit (utils) naming;
cfg = config.foundation;
# this could be moved out into library functions, it's pretty useful.
# mapAttrsIndexed' :: (Int -> String -> AttrSet -> { name:: String; value :: Any; }) -> AttrSet -> AttrSet
mapAttrsIndexed' = f: attrs: listToAttrs (imap0 (i: v: f i v.name v.value) (attrsToList attrs));
# mapAttrsIndexed :: (Int -> String -> AttrSet -> Any) -> AttrSet -> AttrSet
mapAttrsIndexed =
f: attrs:
mapAttrsIndexed' (
i: n: v:
nameValuePair v.name (f i v.name v.value)
) attrs;
in
{
options.foundation =
let
networkSubmodule =
with types;
submodule {
options = {
enable = mkOption {
type = types.bool;
default = true;
description = "Should this network be created?";
};
default = mkOption {
type = types.bool;
default = false;
description = ''
Should this network be the default for all services
that don't have an explicit network defined?
'';
};
# we rely on docker to give us our
# ipv4 settings automatically, but
# maybe we want another module like
# this for ipv4?
ipv6 = mkOption {
type = types.submodule {
options = {
enable = mkOption {
type = types.bool;
default = true;
description = "Should this network have IPv6?";
};
subnet = mkOption {
type = types.nullOr types.str;
description = ''
IPv6 subnet for this network in CIDR notation.
Don't set to get a random subnet assigned to you within
the subnet defined in `defaultIPv6SubnetPrefix`.
'';
example = "2001:d0c:123::/64";
default = null;
};
gateway = mkOption {
type = types.nullOr types.str;
description = ''
IPv6 gateway for this network.
Should match the subnet.
'';
example = "2001:d0c:123::1";
default = null;
};
};
};
description = "IPv6 settings for network";
default = { };
};
driver = mkOption {
type = types.str;
default = "bridge";
description = "Docker network driver to use";
};
mtu = mkOption {
type = types.nullOr types.int;
default = null;
example = 1400;
description = ''
The MTU for this network.
If null, we use the Docker default.
'';
};
options = mkOption {
type = types.listOf types.str;
default = [ ];
description = "Additional options to pass to docker network create";
};
serviceGroup = mkOption {
type = types.nullOr types.str;
default = null;
description = ''
The group of the network, if it belongs to one.
This makes the network service a part of the
group target.
'';
};
};
};
in
{
networks = mkOption {
type = types.attrsOf networkSubmodule;
description = "List of service networks to create";
default = { };
};
defaultNetwork = mkOption {
type = types.nullOr types.str;
description = ''
The default network for services.
If at least one network is marked as default, you can
find it's name here.
'';
};
defaultIPv6SubnetPrefix = mkOption {
type = types.str;
description = ''
Default network subnet assigned to networks without
a set subnet.
Prefix length defined by `defaultIPv6SubnetLength`.
'';
default = "2001:d0c";
};
defaultIPv6SubnetLength = mkOption {
type = types.int;
description = ''
Default network subnet length assigned to networks without
a set subnet.
This should always be the length of the `defaultIPv6SubnetPrefix` + 16.
'';
default = 48;
};
};
config = lib.mkIf (cfg.networks != { }) {
# other modules can use this to look up which network
# they can use as the default!
foundation.defaultNetwork =
let
networksLabeledDefault = attrNames (filterAttrs (name: net: net.default) cfg.networks);
defaultsCount = builtins.length networksLabeledDefault;
in
assert assertMsg (defaultsCount <= 1) "multiple service networks labeled default";
assert assertMsg (defaultsCount != 0) "no default service network";
builtins.head networksLabeledDefault;
# we need these settings to allow our networks to be IPv6-enabled.
# we also ignore the default bridge, because it's setup
# is a lot more complicated...
virtualisation.docker.daemon.settings = {
experimental = true;
ipv6 = true;
ip6tables = true;
fixed-cidr-v6 = "${cfg.defaultIPv6SubnetPrefix}:255::/${toString cfg.defaultIPv6SubnetLength}";
};
boot.kernel.sysctl = {
"net.ipv6.conf.all.forwarding" = 1;
"net.ipv6.conf.default.forwarding" = 1;
};
networking.firewall = {
# both options should work together to let all packets coming from
# docker bridges through.
trustedInterfaces = [ "br-*" ];
extraCommands = ''
# allow inbound packets
ip6tables -A nixos-fw -i br-+ -j nixos-fw-accept 2>/dev/null || true
# allow outbound to the docker bridge
ip6tables -A nixos-fw -o br-+ -j nixos-fw-accept 2>/dev/null || true
# allow forwarding between bridges+external interfaces
ip6tables -I FORWARD -i br-+ -j ACCEPT 2>/dev/null || true
ip6tables -I FORWARD -o br-+ -j ACCEPT 2>/dev/null || true
# allow return traffic
# note: nothing works without this!
ip6tables -I FORWARD ! -i br-+ -o br-+ -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT 2>/dev/null || true
'';
};
systemd.services =
let
prefixOffset = 100;
prefixByIndex = i: "${cfg.defaultIPv6SubnetPrefix}:${toString (prefixOffset + i)}";
subnetByIndex = i: "${prefixByIndex i}::/${toString cfg.defaultIPv6SubnetLength}";
gatewayByIndex = i: "${prefixByIndex i}::1";
networkService =
index: name: network:
let
inherit (network)
ipv6
driver
mtu
options
serviceGroup
;
docker = getExe pkgs.docker;
subnet = if ipv6.subnet == null then subnetByIndex index else ipv6.subnet;
gateway = if ipv6.gateway == null then gatewayByIndex index else ipv6.gateway;
extraOptions = concatStringsSep " " options;
ipv6Options = concatStringsSep " " [
"--ipv6"
"--subnet=${subnet}"
"--gateway=${gateway}"
];
in
{
description = "Docker service network '${name}'";
after = [ "docker.service" ];
requires = [ "docker.service" ];
wantedBy = [ "multi-user.target" ];
partOf = optional (network.serviceGroup != null) "${naming.groupTarget serviceGroup}.target";
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
ExecStart = pkgs.writeShellScript "create-docker-network-${name}" ''
if ${docker} network inspect ${name} >/dev/null 2>&1; then
${docker} network rm ${name}
fi
${docker} network create \
--driver=${driver} \
${optionalString (ipv6.enable) ipv6Options} \
${optionalString (mtu != null) "--opt com.docker.network.driver.mtu=${toString network.mtu}"} \
${extraOptions} \
${name}
'';
ExecStop = pkgs.writeShellScript "delete-docker-network-${name}" ''
${docker} network rm -f ${name}
'';
};
};
mkNetwork = index: name: network: {
name = naming.networkService name;
value = networkService index name network;
};
in
mapAttrsIndexed' mkNetwork (filterAttrs (name: net: net.enable) cfg.networks);
};
}
|