@@ -0,0 +1,130 @@
1+{ config, lib, pkgs, ... }:
5+ inherit (builtins) toFile;
6+ inherit (lib) concatMapStringsSep concatStringsSep mapAttrsToList
7+ mkIf mkEnableOption mkOption types;
9+ cfg = config.services.strongswan;
11+ ipsecSecrets = secrets: toFile "ipsec.secrets" (
12+ concatMapStringsSep "\n" (f: "include ${f}") secrets
15+ ipsecConf = {setup, connections, ca}:
17+ # https://wiki.strongswan.org/projects/strongswan/wiki/IpsecConf
18+ makeSections = type: sections: concatStringsSep "\n\n" (
19+ mapAttrsToList (sec: attrs:
21+ (concatStringsSep "\n" ( mapAttrsToList (k: v: " ${k}=${v}") attrs ))
24+ setupConf = makeSections "config" { inherit setup; };
25+ connectionsConf = makeSections "conn" connections;
26+ caConf = makeSections "ca" ca;
29+ builtins.toFile "ipsec.conf" ''
35+ strongswanConf = {setup, connections, ca, secrets}: toFile "strongswan.conf" ''
39+ secrets_file = ${ipsecSecrets secrets}
45+ config_file = ${ipsecConf { inherit setup connections ca; }}
51+ options.services.strongswan = {
52+ enable = mkEnableOption "strongSwan";
55+ type = types.listOf types.path;
57+ example = [ "/run/keys/ipsec-foo.secret" ];
59+ A list of paths to IPSec secret files. These
60+ files will be included into the main ipsec.secrets file
61+ with the `include' directive. It is safer if these paths are absolute.
66+ type = types.attrsOf types.str;
68+ example = { cachecrls = "yes"; strictcrlpolicy = "yes"; };
70+ A set of options for the `config setup' section of
71+ the `ipsec.conf' file. Defines general configuration parameters.
75+ connections = mkOption {
76+ type = types.attrsOf (types.attrsOf types.str);
80+ keyexchange = "ikev2";
85+ leftcert = "/run/keys/moonCert.pem";
86+ leftid = "@moon.strongswan.org";
87+ leftsubnet = "10.1.0.0/16";
92+ A set of connections and their options for the `conn xxx'
93+ sections of the `ipsec.conf' file.
98+ type = types.attrsOf (types.attrsOf types.str);
103+ cacert = "/run/keys/strongswanCert.pem";
104+ crluri = "http://crl2.strongswan.org/strongswan.crl";
108+ A set of CAs (certification authorities) and their options
109+ for the `ca xxx' sections of the `ipsec.conf' file.
114+ config = with cfg; mkIf enable {
115+ systemd.services.strongswan = {
116+ description = "strongSwan IPSec service";
117+ wantedBy = [ "multi-user.target" ];
118+ path = with pkgs; [ kmod ]; # XXX Linux
119+ wants = [ "keys.target" ];
120+ after = [ "network.target" "keys.target" ];
122+ STRONGSWAN_CONF = strongswanConf { inherit setup connections ca secrets; };
125+ ExecStart = "${pkgs.strongswan}/sbin/ipsec start --nofork";