···
libexec = "${pkgs.libreswan}/libexec/ipsec";
ipsec = "${pkgs.libreswan}/sbin/ipsec";
12
-
trim = chars: str: let
13
-
nonchars = filter (x : !(elem x.value chars))
14
-
(imap0 (i: v: {ind = i; value = v;}) (stringToCharacters str));
16
-
if length nonchars == 0 then ""
17
-
else substring (head nonchars).ind (add 1 (sub (last nonchars).ind (head nonchars).ind)) str;
14
+
nonchars = filter (x : !(elem x.value chars))
15
+
(imap0 (i: v: {ind = i; value = v;}) (stringToCharacters str));
17
+
if length nonchars == 0 then ""
18
+
else substring (head nonchars).ind (add 1 (sub (last nonchars).ind (head nonchars).ind)) str;
indent = str: concatStrings (concatMap (s: [" " (trim [" " "\t"] s) "\n"]) (splitString "\n" str));
configText = indent (toString cfg.configSetup);
connectionText = concatStrings (mapAttrsToList (n: v:
25
+
'') cfg.connections);
25
-
'') cfg.connections);
26
-
configFile = pkgs.writeText "ipsec.conf"
27
+
configFile = pkgs.writeText "ipsec-nixos.conf"
···
35
+
policyFiles = mapAttrs' (name: text:
36
+
{ name = "ipsec.d/policies/${name}";
37
+
value.source = pkgs.writeText "ipsec-policy-${name}" text;
···
44
-
enable = mkEnableOption "libreswan ipsec service";
50
+
enable = mkEnableOption "Libreswan IPsec service";
virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v4:25.0.0.0/8,%v4:100.64.0.0/10,%v6:fd00::/8,%v6:fe80::/10
secretsfile=/root/ipsec.secrets
virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v4:25.0.0.0/8,%v4:100.64.0.0/10,%v6:fd00::/8,%v6:fe80::/10
59
-
description = "Options to go in the 'config setup' section of the libreswan ipsec configuration";
63
+
description = "Options to go in the 'config setup' section of the Libreswan IPsec configuration";
type = types.attrsOf types.lines;
69
+
example = literalExample ''
70
+
{ myconnection = '''
82
+
description = "A set of connections to define for the Libreswan IPsec service";
85
+
policies = mkOption {
86
+
type = types.attrsOf types.lines;
88
+
example = literalExample ''
89
+
{ private-or-clear = '''
90
+
# Attempt opportunistic IPsec for the entire Internet
97
+
A set of policies to apply to the IPsec connections.
100
+
The policy name must match the one of connection it needs to apply to.
77
-
description = "A set of connections to define for the libreswan ipsec service";
105
+
disableRedirects = mkOption {
109
+
Whether to disable send and accept redirects for all nework interfaces.
110
+
See the Libreswan <link xlink:href="https://libreswan.org/wiki/FAQ#Why_is_it_recommended_to_disable_send_redirects_in_.2Fproc.2Fsys.2Fnet_.3F">
111
+
FAQ</link> page for why this is recommended.
···
config = mkIf cfg.enable {
124
+
# Install package, systemd units, etc.
environment.systemPackages = [ pkgs.libreswan pkgs.iproute2 ];
126
+
systemd.packages = [ pkgs.libreswan ];
127
+
systemd.tmpfiles.packages = [ pkgs.libreswan ];
129
+
# Install configuration files
130
+
environment.etc = {
131
+
"ipsec.secrets".source = "${pkgs.libreswan}/etc/ipsec.secrets";
132
+
"ipsec.conf".source = "${pkgs.libreswan}/etc/ipsec.conf";
133
+
"ipsec.d/01-nixos.conf".source = configFile;
136
+
# Create NSS database directory
137
+
systemd.tmpfiles.rules = [ "d /var/lib/ipsec/nss 755 root root -" ];
systemd.services.ipsec = {
description = "Internet Key Exchange (IKE) Protocol Daemon for IPsec";
101
-
wants = [ "network-online.target" ];
102
-
after = [ "network-online.target" ];
wantedBy = [ "multi-user.target" ];
107
-
Restart = "always";
108
-
EnvironmentFile = "-${pkgs.libreswan}/etc/sysconfig/pluto";
110
-
"${libexec}/addconn --config ${configFile} --checkconfig"
111
-
"${libexec}/_stackmanager start"
112
-
"${ipsec} --checknss"
113
-
"${ipsec} --checknflog"
115
-
ExecStart = "${libexec}/pluto --config ${configFile} --nofork \$PLUTO_OPTIONS";
116
-
ExecStop = "${libexec}/whack --shutdown";
118
-
"${pkgs.iproute2}/bin/ip xfrm policy flush"
119
-
"${pkgs.iproute2}/bin/ip xfrm state flush"
120
-
"${ipsec} --stopnflog"
122
-
ExecReload = "${libexec}/whack --listen";
142
+
restartTriggers = [ configFile ] ++ mapAttrsToList (n: v: v.source) policyFiles;
143
+
path = with pkgs; [
151
+
preStart = optionalString cfg.disableRedirects ''
152
+
# Disable send/receive redirects
153
+
echo 0 | tee /proc/sys/net/ipv4/conf/*/send_redirects
154
+
echo 0 | tee /proc/sys/net/ipv{4,6}/conf/*/accept_redirects