9 cfg = config.services.libreswan;
11 libexec = "${pkgs.libreswan}/libexec/ipsec";
12 ipsec = "${pkgs.libreswan}/sbin/ipsec";
17 nonchars = lib.filter (x: !(lib.elem x.value chars)) (
21 }) (lib.stringToCharacters str)
24 lib.optionalString (nonchars != [ ]) (
25 lib.substring (lib.head nonchars).ind (lib.add 1 (
26 lib.sub (lib.last nonchars).ind (lib.head nonchars).ind
36 ]) (lib.splitString "\n" str)
38 configText = indent (toString cfg.configSetup);
39 connectionText = lib.concatStrings (
40 lib.mapAttrsToList (n: v: ''
46 configFile = pkgs.writeText "ipsec-nixos.conf" ''
53 policyFiles = lib.mapAttrs' (name: text: {
54 name = "ipsec.d/policies/${name}";
55 value.source = pkgs.writeText "ipsec-policy-${name}" text;
66 services.libreswan = {
68 enable = lib.mkEnableOption "Libreswan IPsec service";
70 configSetup = lib.mkOption {
71 type = lib.types.lines;
74 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
77 secretsfile=/root/ipsec.secrets
79 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
81 description = "Options to go in the 'config setup' section of the Libreswan IPsec configuration";
84 connections = lib.mkOption {
85 type = lib.types.attrsOf lib.types.lines;
87 example = lib.literalExpression ''
100 description = "A set of connections to define for the Libreswan IPsec service";
103 policies = lib.mkOption {
104 type = lib.types.attrsOf lib.types.lines;
106 example = lib.literalExpression ''
107 { private-or-clear = '''
108 # Attempt opportunistic IPsec for the entire Internet
115 A set of policies to apply to the IPsec connections.
118 The policy name must match the one of connection it needs to apply to.
123 disableRedirects = lib.mkOption {
124 type = lib.types.bool;
127 Whether to disable send and accept redirects for all network interfaces.
129 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.
137 ###### implementation
139 config = lib.mkIf cfg.enable {
141 # Install package, systemd units, etc.
142 environment.systemPackages = [
146 systemd.packages = [ pkgs.libreswan ];
147 systemd.tmpfiles.packages = [ pkgs.libreswan ];
149 # Install configuration files
151 "ipsec.secrets".text = ''
152 include ${pkgs.libreswan}/etc/ipsec.secrets
154 "ipsec.conf".source = "${pkgs.libreswan}/etc/ipsec.conf";
155 "ipsec.d/01-nixos.conf".source = configFile;
158 systemd.services.ipsec = {
159 description = "Internet Key Exchange (IKE) Protocol Daemon for IPsec";
160 wantedBy = [ "multi-user.target" ];
161 restartTriggers = [ configFile ] ++ lib.mapAttrsToList (n: v: v.source) policyFiles;
170 preStart = lib.optionalString cfg.disableRedirects ''
171 # Disable send/receive redirects
172 echo 0 | tee /proc/sys/net/ipv4/conf/*/send_redirects
173 echo 0 | tee /proc/sys/net/ipv{4,6}/conf/*/accept_redirects
176 StateDirectory = "ipsec/nss";
177 StateDirectoryMode = 700;