1 { config, lib, pkgs, ... }:
7 cfg = config.services.libreswan;
9 libexec = "${pkgs.libreswan}/libexec/ipsec";
10 ipsec = "${pkgs.libreswan}/sbin/ipsec";
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;
19 indent = str: concatStrings (concatMap (s: [" " (trim [" " "\t"] s) "\n"]) (splitString "\n" str));
20 configText = indent (toString cfg.configSetup);
21 connectionText = concatStrings (mapAttrsToList (n: v:
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;
48 services.libreswan = {
50 enable = mkEnableOption (lib.mdDoc "Libreswan IPsec service");
52 configSetup = mkOption {
56 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 secretsfile=/root/ipsec.secrets
61 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
63 description = lib.mdDoc "Options to go in the 'config setup' section of the Libreswan IPsec configuration";
66 connections = mkOption {
67 type = types.attrsOf types.lines;
69 example = literalExpression ''
82 description = lib.mdDoc "A set of connections to define for the Libreswan IPsec service";
86 type = types.attrsOf types.lines;
88 example = literalExpression ''
89 { private-or-clear = '''
90 # Attempt opportunistic IPsec for the entire Internet
96 description = lib.mdDoc ''
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.
105 disableRedirects = mkOption {
108 description = lib.mdDoc ''
109 Whether to disable send and accept redirects for all nework interfaces.
111 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.
120 ###### implementation
122 config = mkIf cfg.enable {
124 # Install package, systemd units, etc.
125 environment.systemPackages = [ pkgs.libreswan pkgs.iproute2 ];
126 systemd.packages = [ pkgs.libreswan ];
127 systemd.tmpfiles.packages = [ pkgs.libreswan ];
129 # Install configuration files
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 -" ];
139 systemd.services.ipsec = {
140 description = "Internet Key Exchange (IKE) Protocol Daemon for IPsec";
141 wantedBy = [ "multi-user.target" ];
142 restartTriggers = [ configFile ] ++ mapAttrsToList (n: v: v.source) policyFiles;
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