blob: 89020633cb983d9978603b80a752a725b3dfffa0 (
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
|
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.customTinc;
networkModule = {
extraConfig = mkOption {
default = ''
PingTimeout = 10
'';
type = types.lines;
description = ''
Extra lines to add to the tinc service configuration file.
'';
};
name = mkOption {
default = null;
type = types.nullOr types.str;
description = ''
The name of the node which is used as an identifier when communicating
with the remote nodes in the mesh. If null then the hostname of the system
is used.
'';
};
debugLevel = mkOption {
default = 0;
type = types.addCheck types.int (l: l >= 0 && l <= 5);
description = ''
The amount of debugging information to add to the log. 0 means little
logging while 5 is the most logging. <command>man tincd</command> for
more details.
'';
};
hosts = mkOption {
default = { };
type = types.attrsOf types.lines;
description = ''
The name of the host in the network as well as the configuration for that host.
This name should only contain alphanumerics and underscores.
'';
};
interfaceType = mkOption {
default = "tun";
type = types.addCheck types.str (n: n == "tun" || n == "tap");
description = ''
The type of virtual interface used for the network connection
'';
};
interfaceConfig = mkOption {
default = { };
description = ''
Additional configuration for the generated network interface
'';
};
package = mkOption {
default = pkgs.tinc_pre;
description = ''
The package to use for the tinc daemon's binary.
'';
};
scripts = mkOption {
default = { };
type = types.attrsOf (types.nullOr types.str);
description = ''
Hook scripts
'';
};
nmDispatch = mkOption {
type = types.bool;
default = config.networking.networkmanager.enable;
description = ''
Install a network-manager dispatcher script to automatically
connect to all remotes when networking is available
'';
};
};
in
{
###### interface
options = {
services.customTinc = {
networks = mkOption {
default = { };
type = types.attrsOf (types.submodule { options = networkModule; });
description = ''
Defines the tinc networks which will be started.
Each network invokes a different daemon.
'';
};
};
};
###### implementation
config = mkIf (cfg.networks != { }) {
environment.etc = fold (a: b: a // b) { }
(flip mapAttrsToList cfg.networks (network: data:
flip mapAttrs' data.hosts (host: text: nameValuePair
("tinc/${network}/hosts/${host}")
({ inherit text; })
) // (flip mapAttrs' data.scripts (scriptName: text: nameValuePair
("tinc/${network}/${scriptName}")
({ mode = "0555"; inherit text; })
)) // {
"tinc/${network}/tinc.conf" = {
text = ''
Name = ${if data.name == null then config.networking.hostName else data.name}
DeviceType = ${data.interfaceType}
Interface = ${network}
${data.extraConfig}
'';
};
}
));
environment.systemPackages = mapAttrsToList (_: data: data.package) cfg.networks;
networking.interfaces = flip mapAttrs cfg.networks (network: data:
{
virtual = true;
virtualType = "${data.interfaceType}";
} // data.interfaceConfig
);
networking.networkmanager.dispatcherScripts = concatLists (flip mapAttrsToList cfg.networks (network: data: optional data.nmDispatch {
type = "basic";
source = pkgs.writeScript "connect-${network}.sh" ''
#!${pkgs.stdenv.shell}
shopt -s extglob
case "''${2}" in
(?(vpn-)up)
${data.package}/bin/tinc -n ${network} --pidfile /run/tinc.${network}.pid --batch retry
;;
esac
'';
}));
systemd.services = flip mapAttrs' cfg.networks (network: data: nameValuePair
("tinc@${network}")
({
description = "Tinc Daemon - ${network}";
wantedBy = [ "network.target" ];
after = [ "network-interfaces.target" ];
restartTriggers = [ config.environment.etc."tinc/${network}/tinc.conf".source ]
++ mapAttrsToList (host: _ : config.environment.etc."tinc/${network}/hosts/${host}".source) data.hosts;
path = [ data.package ];
serviceConfig = {
Type = "simple";
PIDFile = "/run/tinc.${network}.pid";
ExecStart = ''
${data.package}/bin/tincd -D -U tinc.${network} -n ${network} --pidfile /run/tinc.${network}.pid -d ${toString data.debugLevel}
'';
Restart = "on-failure";
};
preStart = ''
mkdir -p /etc/tinc/${network}/hosts
# Determine how we should generate our keys
if type tinc >/dev/null 2>&1; then
# Tinc 1.1+ uses the tinc helper application for key generation
[ -f "/etc/tinc/${network}/ed25519_key.priv" ] || tinc -n ${network} --pidfile /run/tinc.${network}.pid generate-ed25519-keys
[ -f "/etc/tinc/${network}/rsa_key.priv" ] || tinc -n ${network} --pidfile /run/tinc.${network}.pid generate-rsa-keys 4096
else
# Tinc 1.0 uses the tincd application
[ -f "/etc/tinc/${network}/rsa_key.priv" ] || tincd -n ${network} -K 4096
fi
tinc -n ${network} --pidfile /run/tinc.${network}.pid --batch fsck
'';
})
);
users.extraUsers = flip mapAttrs' cfg.networks (network: _:
nameValuePair ("tinc.${network}") ({
description = "Tinc daemon user for ${network}";
group = "tinc.${network}";
isSystemUser = true;
})
);
users.extraGroups = flip mapAttrs' cfg.networks (network: _:
nameValuePair ("tinc.${network}") ({})
);
};
}
|