2016-02-24 04:06:45 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.libreswan;
|
|
|
|
|
|
|
|
libexec = "${pkgs.libreswan}/libexec/ipsec";
|
|
|
|
ipsec = "${pkgs.libreswan}/sbin/ipsec";
|
|
|
|
|
2021-05-13 10:37:59 +00:00
|
|
|
trim = chars: str:
|
|
|
|
let
|
|
|
|
nonchars = filter (x : !(elem x.value chars))
|
|
|
|
(imap0 (i: v: {ind = i; value = v;}) (stringToCharacters str));
|
|
|
|
in
|
2023-06-24 18:19:19 +00:00
|
|
|
lib.optionalString (length nonchars != 0)
|
|
|
|
(substring (head nonchars).ind (add 1 (sub (last nonchars).ind (head nonchars).ind)) str);
|
2016-02-24 04:06:45 +00:00
|
|
|
indent = str: concatStrings (concatMap (s: [" " (trim [" " "\t"] s) "\n"]) (splitString "\n" str));
|
|
|
|
configText = indent (toString cfg.configSetup);
|
2017-10-22 07:27:26 +00:00
|
|
|
connectionText = concatStrings (mapAttrsToList (n: v:
|
2016-02-24 04:06:45 +00:00
|
|
|
''
|
|
|
|
conn ${n}
|
|
|
|
${indent v}
|
|
|
|
'') cfg.connections);
|
2021-05-13 10:37:59 +00:00
|
|
|
|
|
|
|
configFile = pkgs.writeText "ipsec-nixos.conf"
|
2016-02-24 04:06:45 +00:00
|
|
|
''
|
|
|
|
config setup
|
|
|
|
${configText}
|
2017-10-22 07:27:26 +00:00
|
|
|
|
2016-02-24 04:06:45 +00:00
|
|
|
${connectionText}
|
|
|
|
'';
|
|
|
|
|
2021-05-13 10:37:59 +00:00
|
|
|
policyFiles = mapAttrs' (name: text:
|
|
|
|
{ name = "ipsec.d/policies/${name}";
|
|
|
|
value.source = pkgs.writeText "ipsec-policy-${name}" text;
|
|
|
|
}) cfg.policies;
|
|
|
|
|
2016-02-24 04:06:45 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.libreswan = {
|
|
|
|
|
2022-08-28 19:18:44 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "Libreswan IPsec service");
|
2016-02-24 04:06:45 +00:00
|
|
|
|
|
|
|
configSetup = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = ''
|
|
|
|
protostack=netkey
|
|
|
|
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
|
|
|
|
'';
|
|
|
|
example = ''
|
|
|
|
secretsfile=/root/ipsec.secrets
|
|
|
|
protostack=netkey
|
|
|
|
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
|
|
|
|
'';
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Options to go in the 'config setup' section of the Libreswan IPsec configuration";
|
2016-02-24 04:06:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
connections = mkOption {
|
|
|
|
type = types.attrsOf types.lines;
|
|
|
|
default = {};
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2021-05-13 10:37:59 +00:00
|
|
|
{ myconnection = '''
|
|
|
|
auto=add
|
|
|
|
left=%defaultroute
|
|
|
|
leftid=@user
|
|
|
|
|
|
|
|
right=my.vpn.com
|
|
|
|
|
|
|
|
ikev2=no
|
|
|
|
ikelifetime=8h
|
|
|
|
''';
|
|
|
|
}
|
|
|
|
'';
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "A set of connections to define for the Libreswan IPsec service";
|
2021-05-13 10:37:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
policies = mkOption {
|
|
|
|
type = types.attrsOf types.lines;
|
|
|
|
default = {};
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2021-05-13 10:37:59 +00:00
|
|
|
{ private-or-clear = '''
|
|
|
|
# Attempt opportunistic IPsec for the entire Internet
|
|
|
|
0.0.0.0/0
|
|
|
|
::/0
|
|
|
|
''';
|
|
|
|
}
|
|
|
|
'';
|
2022-08-30 00:30:04 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-05-13 10:37:59 +00:00
|
|
|
A set of policies to apply to the IPsec connections.
|
|
|
|
|
2022-08-30 00:30:04 +00:00
|
|
|
::: {.note}
|
|
|
|
The policy name must match the one of connection it needs to apply to.
|
|
|
|
:::
|
2021-05-13 10:37:59 +00:00
|
|
|
'';
|
2016-02-24 04:06:45 +00:00
|
|
|
};
|
2021-05-13 10:37:59 +00:00
|
|
|
|
|
|
|
disableRedirects = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-12-18 00:31:14 +00:00
|
|
|
Whether to disable send and accept redirects for all network interfaces.
|
2022-07-28 21:19:15 +00:00
|
|
|
See the Libreswan [
|
|
|
|
FAQ](https://libreswan.org/wiki/FAQ#Why_is_it_recommended_to_disable_send_redirects_in_.2Fproc.2Fsys.2Fnet_.3F) page for why this is recommended.
|
2021-05-13 10:37:59 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-02-24 04:06:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
2021-05-13 10:37:59 +00:00
|
|
|
# Install package, systemd units, etc.
|
2021-03-14 16:05:16 +00:00
|
|
|
environment.systemPackages = [ pkgs.libreswan pkgs.iproute2 ];
|
2021-05-13 10:37:59 +00:00
|
|
|
systemd.packages = [ pkgs.libreswan ];
|
|
|
|
systemd.tmpfiles.packages = [ pkgs.libreswan ];
|
|
|
|
|
|
|
|
# Install configuration files
|
|
|
|
environment.etc = {
|
|
|
|
"ipsec.secrets".source = "${pkgs.libreswan}/etc/ipsec.secrets";
|
|
|
|
"ipsec.conf".source = "${pkgs.libreswan}/etc/ipsec.conf";
|
|
|
|
"ipsec.d/01-nixos.conf".source = configFile;
|
|
|
|
} // policyFiles;
|
|
|
|
|
|
|
|
# Create NSS database directory
|
|
|
|
systemd.tmpfiles.rules = [ "d /var/lib/ipsec/nss 755 root root -" ];
|
2016-02-24 04:06:45 +00:00
|
|
|
|
|
|
|
systemd.services.ipsec = {
|
|
|
|
description = "Internet Key Exchange (IKE) Protocol Daemon for IPsec";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2021-05-13 10:37:59 +00:00
|
|
|
restartTriggers = [ configFile ] ++ mapAttrsToList (n: v: v.source) policyFiles;
|
|
|
|
path = with pkgs; [
|
|
|
|
libreswan
|
|
|
|
iproute2
|
|
|
|
procps
|
|
|
|
nssTools
|
|
|
|
iptables
|
|
|
|
nettools
|
|
|
|
];
|
|
|
|
preStart = optionalString cfg.disableRedirects ''
|
|
|
|
# Disable send/receive redirects
|
|
|
|
echo 0 | tee /proc/sys/net/ipv4/conf/*/send_redirects
|
|
|
|
echo 0 | tee /proc/sys/net/ipv{4,6}/conf/*/accept_redirects
|
|
|
|
'';
|
2016-02-24 04:06:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|