2016-02-23 23:06:45 -05:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.libreswan;
|
|
|
|
|
|
|
|
libexec = "${pkgs.libreswan}/libexec/ipsec";
|
|
|
|
ipsec = "${pkgs.libreswan}/sbin/ipsec";
|
|
|
|
|
2021-05-13 12:37:59 +02:00
|
|
|
trim =
|
|
|
|
chars: str:
|
|
|
|
let
|
2024-08-24 22:05:53 +02:00
|
|
|
nonchars = lib.filter (x: !(lib.elem x.value chars)) (
|
|
|
|
lib.imap0 (i: v: {
|
|
|
|
ind = i;
|
|
|
|
value = v;
|
|
|
|
}) (lib.stringToCharacters str)
|
|
|
|
);
|
2021-05-13 12:37:59 +02:00
|
|
|
in
|
2023-07-02 19:03:19 +02:00
|
|
|
lib.optionalString (nonchars != [ ]) (
|
2024-08-24 22:05:53 +02:00
|
|
|
lib.substring (lib.head nonchars).ind (lib.add 1 (
|
|
|
|
lib.sub (lib.last nonchars).ind (lib.head nonchars).ind
|
|
|
|
)) str
|
|
|
|
);
|
|
|
|
indent =
|
|
|
|
str:
|
|
|
|
lib.concatStrings (
|
|
|
|
lib.concatMap (s: [
|
|
|
|
" "
|
|
|
|
(trim [ " " "\t" ] s)
|
|
|
|
"\n"
|
|
|
|
]) (lib.splitString "\n" str)
|
|
|
|
);
|
2016-02-23 23:06:45 -05:00
|
|
|
configText = indent (toString cfg.configSetup);
|
2024-08-24 22:05:53 +02:00
|
|
|
connectionText = lib.concatStrings (
|
|
|
|
lib.mapAttrsToList (n: v: ''
|
2016-02-23 23:06:45 -05:00
|
|
|
conn ${n}
|
|
|
|
${indent v}
|
|
|
|
'') cfg.connections
|
|
|
|
);
|
2021-05-13 12:37:59 +02:00
|
|
|
|
|
|
|
configFile = pkgs.writeText "ipsec-nixos.conf" ''
|
2016-02-23 23:06:45 -05:00
|
|
|
config setup
|
|
|
|
${configText}
|
2017-10-22 10:27:26 +03:00
|
|
|
|
2016-02-23 23:06:45 -05:00
|
|
|
${connectionText}
|
|
|
|
'';
|
|
|
|
|
2024-08-24 22:05:53 +02:00
|
|
|
policyFiles = lib.mapAttrs' (name: text: {
|
2021-05-13 12:37:59 +02:00
|
|
|
name = "ipsec.d/policies/${name}";
|
|
|
|
value.source = pkgs.writeText "ipsec-policy-${name}" text;
|
|
|
|
}) cfg.policies;
|
|
|
|
|
2016-02-23 23:06:45 -05:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.libreswan = {
|
|
|
|
|
2024-08-24 22:05:53 +02:00
|
|
|
enable = lib.mkEnableOption "Libreswan IPsec service";
|
2016-02-23 23:06:45 -05:00
|
|
|
|
2024-08-24 22:05:53 +02:00
|
|
|
configSetup = lib.mkOption {
|
|
|
|
type = lib.types.lines;
|
2016-02-23 23:06:45 -05:00
|
|
|
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
|
|
|
|
'';
|
2021-05-13 12:37:59 +02:00
|
|
|
description = "Options to go in the 'config setup' section of the Libreswan IPsec configuration";
|
2016-02-23 23:06:45 -05:00
|
|
|
};
|
|
|
|
|
2024-08-24 22:05:53 +02:00
|
|
|
connections = lib.mkOption {
|
|
|
|
type = lib.types.attrsOf lib.types.lines;
|
2016-02-23 23:06:45 -05:00
|
|
|
default = { };
|
2024-08-24 22:05:53 +02:00
|
|
|
example = lib.literalExpression ''
|
2021-05-13 12:37:59 +02:00
|
|
|
{ myconnection = '''
|
|
|
|
auto=add
|
|
|
|
left=%defaultroute
|
|
|
|
leftid=@user
|
|
|
|
|
|
|
|
right=my.vpn.com
|
|
|
|
|
|
|
|
ikev2=no
|
|
|
|
ikelifetime=8h
|
|
|
|
''';
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = "A set of connections to define for the Libreswan IPsec service";
|
|
|
|
};
|
|
|
|
|
2024-08-24 22:05:53 +02:00
|
|
|
policies = lib.mkOption {
|
|
|
|
type = lib.types.attrsOf lib.types.lines;
|
2021-05-13 12:37:59 +02:00
|
|
|
default = { };
|
2024-08-24 22:05:53 +02:00
|
|
|
example = lib.literalExpression ''
|
2021-05-13 12:37:59 +02:00
|
|
|
{ private-or-clear = '''
|
|
|
|
# Attempt opportunistic IPsec for the entire Internet
|
|
|
|
0.0.0.0/0
|
|
|
|
::/0
|
|
|
|
''';
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
A set of policies to apply to the IPsec connections.
|
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
The policy name must match the one of connection it needs to apply to.
|
2022-08-30 02:30:04 +02:00
|
|
|
:::
|
2021-05-13 12:37:59 +02:00
|
|
|
'';
|
2016-02-23 23:06:45 -05:00
|
|
|
};
|
2021-05-13 12:37:59 +02:00
|
|
|
|
2024-08-24 22:05:53 +02:00
|
|
|
disableRedirects = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2021-05-13 12:37:59 +02:00
|
|
|
default = true;
|
|
|
|
description = ''
|
2022-12-17 19:31:14 -05:00
|
|
|
Whether to disable send and accept redirects for all network interfaces.
|
2021-05-13 12:37:59 +02: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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-02-23 23:06:45 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
2024-08-24 22:05:53 +02:00
|
|
|
config = lib.mkIf cfg.enable {
|
2016-02-23 23:06:45 -05:00
|
|
|
|
2021-05-13 12:37:59 +02:00
|
|
|
# Install package, systemd units, etc.
|
2021-03-14 17:05:16 +01:00
|
|
|
environment.systemPackages = [
|
|
|
|
pkgs.libreswan
|
|
|
|
pkgs.iproute2
|
|
|
|
];
|
2021-05-13 12:37:59 +02:00
|
|
|
systemd.packages = [ pkgs.libreswan ];
|
|
|
|
systemd.tmpfiles.packages = [ pkgs.libreswan ];
|
|
|
|
|
|
|
|
# Install configuration files
|
|
|
|
environment.etc = {
|
2024-11-20 19:59:02 +01:00
|
|
|
"ipsec.secrets".text = ''
|
|
|
|
include ${pkgs.libreswan}/etc/ipsec.secrets
|
|
|
|
'';
|
2021-05-13 12:37:59 +02:00
|
|
|
"ipsec.conf".source = "${pkgs.libreswan}/etc/ipsec.conf";
|
|
|
|
"ipsec.d/01-nixos.conf".source = configFile;
|
|
|
|
} // policyFiles;
|
|
|
|
|
2016-02-23 23:06:45 -05:00
|
|
|
systemd.services.ipsec = {
|
|
|
|
description = "Internet Key Exchange (IKE) Protocol Daemon for IPsec";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2024-08-24 22:05:53 +02:00
|
|
|
restartTriggers = [ configFile ] ++ lib.mapAttrsToList (n: v: v.source) policyFiles;
|
2021-05-13 12:37:59 +02:00
|
|
|
path = with pkgs; [
|
|
|
|
libreswan
|
|
|
|
iproute2
|
|
|
|
procps
|
|
|
|
nssTools
|
|
|
|
iptables
|
|
|
|
nettools
|
|
|
|
];
|
2024-08-24 22:05:53 +02:00
|
|
|
preStart = lib.optionalString cfg.disableRedirects ''
|
2021-05-13 12:37:59 +02:00
|
|
|
# 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
|
|
|
|
'';
|
2023-05-20 13:36:33 +02:00
|
|
|
serviceConfig = {
|
|
|
|
StateDirectory = "ipsec/nss";
|
|
|
|
StateDirectoryMode = 700;
|
|
|
|
};
|
2016-02-23 23:06:45 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|