2020-04-24 23:36:52 +00:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
inherit (builtins) toFile;
|
|
|
|
|
inherit (lib) concatMapStringsSep concatStringsSep mapAttrsToList
|
2021-10-06 13:57:05 +00:00
|
|
|
|
mkIf mkEnableOption mkOption types literalExpression;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
cfg = config.services.strongswan;
|
|
|
|
|
|
|
|
|
|
ipsecSecrets = secrets: toFile "ipsec.secrets" (
|
|
|
|
|
concatMapStringsSep "\n" (f: "include ${f}") secrets
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ipsecConf = {setup, connections, ca}:
|
|
|
|
|
let
|
|
|
|
|
# https://wiki.strongswan.org/projects/strongswan/wiki/IpsecConf
|
|
|
|
|
makeSections = type: sections: concatStringsSep "\n\n" (
|
|
|
|
|
mapAttrsToList (sec: attrs:
|
|
|
|
|
"${type} ${sec}\n" +
|
|
|
|
|
(concatStringsSep "\n" ( mapAttrsToList (k: v: " ${k}=${v}") attrs ))
|
|
|
|
|
) sections
|
|
|
|
|
);
|
|
|
|
|
setupConf = makeSections "config" { inherit setup; };
|
|
|
|
|
connectionsConf = makeSections "conn" connections;
|
|
|
|
|
caConf = makeSections "ca" ca;
|
|
|
|
|
|
|
|
|
|
in
|
|
|
|
|
builtins.toFile "ipsec.conf" ''
|
|
|
|
|
${setupConf}
|
|
|
|
|
${connectionsConf}
|
|
|
|
|
${caConf}
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
strongswanConf = {setup, connections, ca, secretsFile, managePlugins, enabledPlugins}: toFile "strongswan.conf" ''
|
|
|
|
|
charon {
|
|
|
|
|
${if managePlugins then "load_modular = no" else ""}
|
|
|
|
|
${if managePlugins then ("load = " + (concatStringsSep " " enabledPlugins)) else ""}
|
|
|
|
|
plugins {
|
|
|
|
|
stroke {
|
|
|
|
|
secrets_file = ${secretsFile}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
starter {
|
|
|
|
|
config_file = ${ipsecConf { inherit setup connections ca; }}
|
|
|
|
|
}
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
options.services.strongswan = {
|
2022-09-09 14:08:57 +00:00
|
|
|
|
enable = mkEnableOption (lib.mdDoc "strongSwan");
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
secrets = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
default = [];
|
|
|
|
|
example = [ "/run/keys/ipsec-foo.secret" ];
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
A list of paths to IPSec secret files. These
|
|
|
|
|
files will be included into the main ipsec.secrets file with
|
2022-08-12 12:06:08 +00:00
|
|
|
|
the `include` directive. It is safer if these
|
2020-04-24 23:36:52 +00:00
|
|
|
|
paths are absolute.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setup = mkOption {
|
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
|
default = {};
|
|
|
|
|
example = { cachecrls = "yes"; strictcrlpolicy = "yes"; };
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
A set of options for the ‘config setup’ section of the
|
2022-08-12 12:06:08 +00:00
|
|
|
|
{file}`ipsec.conf` file. Defines general
|
2020-04-24 23:36:52 +00:00
|
|
|
|
configuration parameters.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
connections = mkOption {
|
|
|
|
|
type = types.attrsOf (types.attrsOf types.str);
|
|
|
|
|
default = {};
|
2021-10-06 13:57:05 +00:00
|
|
|
|
example = literalExpression ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
{
|
|
|
|
|
"%default" = {
|
|
|
|
|
keyexchange = "ikev2";
|
|
|
|
|
keyingtries = "1";
|
|
|
|
|
};
|
|
|
|
|
roadwarrior = {
|
|
|
|
|
auto = "add";
|
|
|
|
|
leftcert = "/run/keys/moonCert.pem";
|
|
|
|
|
leftid = "@moon.strongswan.org";
|
|
|
|
|
leftsubnet = "10.1.0.0/16";
|
|
|
|
|
right = "%any";
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
'';
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
A set of connections and their options for the ‘conn xxx’
|
2022-08-12 12:06:08 +00:00
|
|
|
|
sections of the {file}`ipsec.conf` file.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ca = mkOption {
|
|
|
|
|
type = types.attrsOf (types.attrsOf types.str);
|
|
|
|
|
default = {};
|
|
|
|
|
example = {
|
|
|
|
|
strongswan = {
|
|
|
|
|
auto = "add";
|
|
|
|
|
cacert = "/run/keys/strongswanCert.pem";
|
|
|
|
|
crluri = "http://crl2.strongswan.org/strongswan.crl";
|
|
|
|
|
};
|
|
|
|
|
};
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
A set of CAs (certification authorities) and their options for
|
2022-08-12 12:06:08 +00:00
|
|
|
|
the ‘ca xxx’ sections of the {file}`ipsec.conf`
|
2020-04-24 23:36:52 +00:00
|
|
|
|
file.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
managePlugins = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
If set to true, this option will disable automatic plugin loading and
|
|
|
|
|
then tell strongSwan to enable the plugins specified in the
|
2022-08-12 12:06:08 +00:00
|
|
|
|
{option}`enabledPlugins` option.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enabledPlugins = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
default = [];
|
2022-08-12 12:06:08 +00:00
|
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
A list of additional plugins to enable if
|
2022-08-12 12:06:08 +00:00
|
|
|
|
{option}`managePlugins` is true.
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
config = with cfg;
|
|
|
|
|
let
|
|
|
|
|
secretsFile = ipsecSecrets cfg.secrets;
|
|
|
|
|
in
|
|
|
|
|
mkIf enable
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
# here we should use the default strongswan ipsec.secrets and
|
|
|
|
|
# append to it (default one is empty so not a pb for now)
|
|
|
|
|
environment.etc."ipsec.secrets".source = secretsFile;
|
|
|
|
|
|
|
|
|
|
systemd.services.strongswan = {
|
|
|
|
|
description = "strongSwan IPSec Service";
|
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2021-04-05 15:23:46 +00:00
|
|
|
|
path = with pkgs; [ kmod iproute2 iptables util-linux ]; # XXX Linux
|
2020-04-24 23:36:52 +00:00
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
|
environment = {
|
|
|
|
|
STRONGSWAN_CONF = strongswanConf { inherit setup connections ca secretsFile managePlugins enabledPlugins; };
|
|
|
|
|
};
|
|
|
|
|
serviceConfig = {
|
|
|
|
|
ExecStart = "${pkgs.strongswan}/sbin/ipsec start --nofork";
|
|
|
|
|
};
|
|
|
|
|
preStart = ''
|
|
|
|
|
# with 'nopeerdns' setting, ppp writes into this folder
|
|
|
|
|
mkdir -m 700 -p /etc/ppp
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|