2014-11-22 19:27:23 +01:00
|
|
|
|
{
|
|
|
|
|
config,
|
|
|
|
|
lib,
|
|
|
|
|
pkgs,
|
|
|
|
|
...
|
|
|
|
|
}:
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
inherit (builtins) toFile;
|
2024-09-07 17:28:25 +02:00
|
|
|
|
inherit (lib)
|
|
|
|
|
concatMapStrings
|
|
|
|
|
concatStringsSep
|
|
|
|
|
mapAttrsToList
|
2023-03-19 21:44:31 +01:00
|
|
|
|
mkIf
|
|
|
|
|
mkEnableOption
|
|
|
|
|
mkOption
|
|
|
|
|
types
|
|
|
|
|
literalExpression
|
|
|
|
|
optionalString
|
|
|
|
|
;
|
2014-11-22 19:27:23 +01:00
|
|
|
|
|
|
|
|
|
cfg = config.services.strongswan;
|
|
|
|
|
|
2024-09-07 17:28:25 +02:00
|
|
|
|
ipsecSecrets = secrets: concatMapStrings (f: "include ${f}\n") secrets;
|
2014-11-22 19:27:23 +01:00
|
|
|
|
|
|
|
|
|
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}
|
|
|
|
|
'';
|
|
|
|
|
|
2017-10-31 20:14:00 +09:00
|
|
|
|
strongswanConf =
|
|
|
|
|
{
|
|
|
|
|
setup,
|
|
|
|
|
connections,
|
|
|
|
|
ca,
|
|
|
|
|
secretsFile,
|
|
|
|
|
managePlugins,
|
|
|
|
|
enabledPlugins,
|
|
|
|
|
}:
|
|
|
|
|
toFile "strongswan.conf" ''
|
2014-11-22 19:27:23 +01:00
|
|
|
|
charon {
|
2023-03-19 21:44:31 +01:00
|
|
|
|
${optionalString managePlugins "load_modular = no"}
|
|
|
|
|
${optionalString managePlugins ("load = " + (concatStringsSep " " enabledPlugins))}
|
2014-11-22 19:27:23 +01:00
|
|
|
|
plugins {
|
|
|
|
|
stroke {
|
2017-10-31 20:14:00 +09:00
|
|
|
|
secrets_file = ${secretsFile}
|
2024-12-10 20:26:33 +01:00
|
|
|
|
}
|
2014-11-22 19:27:23 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
starter {
|
|
|
|
|
config_file = ${ipsecConf { inherit setup connections ca; }}
|
|
|
|
|
}
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
options.services.strongswan = {
|
|
|
|
|
enable = mkEnableOption "strongSwan";
|
|
|
|
|
|
|
|
|
|
secrets = mkOption {
|
2019-04-06 10:28:33 +09:00
|
|
|
|
type = types.listOf types.str;
|
2014-11-22 19:27:23 +01:00
|
|
|
|
default = [ ];
|
|
|
|
|
example = [ "/run/keys/ipsec-foo.secret" ];
|
|
|
|
|
description = ''
|
|
|
|
|
A list of paths to IPSec secret files. These
|
2014-11-25 16:01:27 +01:00
|
|
|
|
files will be included into the main ipsec.secrets file with
|
|
|
|
|
the `include` directive. It is safer if these
|
|
|
|
|
paths are absolute.
|
2014-11-22 19:27:23 +01:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setup = mkOption {
|
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
|
default = { };
|
|
|
|
|
example = {
|
|
|
|
|
cachecrls = "yes";
|
|
|
|
|
strictcrlpolicy = "yes";
|
|
|
|
|
};
|
|
|
|
|
description = ''
|
2014-11-25 16:01:27 +01:00
|
|
|
|
A set of options for the ‘config setup’ section of the
|
|
|
|
|
{file}`ipsec.conf` file. Defines general
|
|
|
|
|
configuration parameters.
|
2014-11-22 19:27:23 +01:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
connections = mkOption {
|
|
|
|
|
type = types.attrsOf (types.attrsOf types.str);
|
|
|
|
|
default = { };
|
2021-10-03 18:06:03 +02:00
|
|
|
|
example = literalExpression ''
|
2020-04-02 07:39:04 +02: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";
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
'';
|
2014-11-22 19:27:23 +01:00
|
|
|
|
description = ''
|
2014-11-25 16:01:27 +01:00
|
|
|
|
A set of connections and their options for the ‘conn xxx’
|
|
|
|
|
sections of the {file}`ipsec.conf` file.
|
2014-11-22 19:27:23 +01: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";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
description = ''
|
2014-11-25 16:01:27 +01:00
|
|
|
|
A set of CAs (certification authorities) and their options for
|
|
|
|
|
the ‘ca xxx’ sections of the {file}`ipsec.conf`
|
|
|
|
|
file.
|
2014-11-22 19:27:23 +01:00
|
|
|
|
'';
|
|
|
|
|
};
|
2017-09-22 03:39:00 -07:00
|
|
|
|
|
|
|
|
|
managePlugins = mkOption {
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
|
|
|
|
description = ''
|
|
|
|
|
If set to true, this option will disable automatic plugin loading and
|
|
|
|
|
then tell strongSwan to enable the plugins specified in the
|
|
|
|
|
{option}`enabledPlugins` option.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enabledPlugins = mkOption {
|
|
|
|
|
type = types.listOf types.str;
|
|
|
|
|
default = [ ];
|
|
|
|
|
description = ''
|
|
|
|
|
A list of additional plugins to enable if
|
|
|
|
|
{option}`managePlugins` is true.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2014-11-22 19:27:23 +01:00
|
|
|
|
};
|
|
|
|
|
|
2024-09-07 17:28:25 +02:00
|
|
|
|
config =
|
|
|
|
|
with cfg;
|
|
|
|
|
mkIf enable {
|
2024-12-10 20:26:33 +01:00
|
|
|
|
|
2017-10-31 20:14:00 +09:00
|
|
|
|
# here we should use the default strongswan ipsec.secrets and
|
|
|
|
|
# append to it (default one is empty so not a pb for now)
|
2024-09-07 17:28:25 +02:00
|
|
|
|
environment.etc."ipsec.secrets".text = ipsecSecrets cfg.secrets;
|
2024-12-10 20:26:33 +01:00
|
|
|
|
|
2014-11-22 19:27:23 +01:00
|
|
|
|
systemd.services.strongswan = {
|
2014-11-25 16:01:27 +01:00
|
|
|
|
description = "strongSwan IPSec Service";
|
2014-11-22 19:27:23 +01:00
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2021-03-14 17:05:16 +01:00
|
|
|
|
path = with pkgs; [
|
|
|
|
|
kmod
|
|
|
|
|
iproute2
|
|
|
|
|
iptables
|
|
|
|
|
util-linux
|
|
|
|
|
]; # XXX Linux
|
2023-10-03 22:21:50 -07:00
|
|
|
|
wants = [ "network-online.target" ];
|
2019-08-24 16:52:17 +02:00
|
|
|
|
after = [ "network-online.target" ];
|
2014-11-22 19:27:23 +01:00
|
|
|
|
environment = {
|
2024-09-07 17:28:25 +02:00
|
|
|
|
STRONGSWAN_CONF = strongswanConf {
|
|
|
|
|
inherit
|
|
|
|
|
setup
|
|
|
|
|
connections
|
|
|
|
|
ca
|
|
|
|
|
managePlugins
|
|
|
|
|
enabledPlugins
|
|
|
|
|
;
|
|
|
|
|
secretsFile = "/etc/ipsec.secrets";
|
2024-12-10 20:26:33 +01:00
|
|
|
|
};
|
2024-09-07 17:28:25 +02:00
|
|
|
|
};
|
2014-11-22 19:27:23 +01:00
|
|
|
|
serviceConfig = {
|
|
|
|
|
ExecStart = "${pkgs.strongswan}/sbin/ipsec start --nofork";
|
2024-12-10 20:26:33 +01:00
|
|
|
|
};
|
2017-10-31 20:14:00 +09:00
|
|
|
|
preStart = ''
|
|
|
|
|
# with 'nopeerdns' setting, ppp writes into this folder
|
|
|
|
|
mkdir -m 700 -p /etc/ppp
|
2024-12-10 20:26:33 +01:00
|
|
|
|
'';
|
2014-11-22 19:27:23 +01:00
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|