2020-04-24 23:36:52 +00:00
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
cfg = config.services.openvpn;
|
|
|
|
|
|
|
|
|
|
inherit (pkgs) openvpn;
|
|
|
|
|
|
|
|
|
|
makeOpenVPNJob = cfg: name:
|
|
|
|
|
let
|
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
|
path = makeBinPath (getAttr "openvpn-${name}" config.systemd.services).path;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
upScript = ''
|
|
|
|
|
export PATH=${path}
|
|
|
|
|
|
|
|
|
|
# For convenience in client scripts, extract the remote domain
|
|
|
|
|
# name and name server.
|
|
|
|
|
for var in ''${!foreign_option_*}; do
|
|
|
|
|
x=(''${!var})
|
|
|
|
|
if [ "''${x[0]}" = dhcp-option ]; then
|
|
|
|
|
if [ "''${x[1]}" = DOMAIN ]; then domain="''${x[2]}"
|
|
|
|
|
elif [ "''${x[1]}" = DNS ]; then nameserver="''${x[2]}"
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
${cfg.up}
|
|
|
|
|
${optionalString cfg.updateResolvConf
|
|
|
|
|
"${pkgs.update-resolv-conf}/libexec/openvpn/update-resolv-conf"}
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
downScript = ''
|
|
|
|
|
export PATH=${path}
|
|
|
|
|
${optionalString cfg.updateResolvConf
|
|
|
|
|
"${pkgs.update-resolv-conf}/libexec/openvpn/update-resolv-conf"}
|
|
|
|
|
${cfg.down}
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
configFile = pkgs.writeText "openvpn-config-${name}"
|
|
|
|
|
''
|
|
|
|
|
errors-to-stderr
|
|
|
|
|
${optionalString (cfg.up != "" || cfg.down != "" || cfg.updateResolvConf) "script-security 2"}
|
|
|
|
|
${cfg.config}
|
|
|
|
|
${optionalString (cfg.up != "" || cfg.updateResolvConf)
|
2023-03-04 12:14:45 +00:00
|
|
|
|
"up ${pkgs.writeShellScript "openvpn-${name}-up" upScript}"}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
${optionalString (cfg.down != "" || cfg.updateResolvConf)
|
2023-03-04 12:14:45 +00:00
|
|
|
|
"down ${pkgs.writeShellScript "openvpn-${name}-down" downScript}"}
|
2020-04-24 23:36:52 +00:00
|
|
|
|
${optionalString (cfg.authUserPass != null)
|
|
|
|
|
"auth-user-pass ${pkgs.writeText "openvpn-credentials-${name}" ''
|
|
|
|
|
${cfg.authUserPass.username}
|
|
|
|
|
${cfg.authUserPass.password}
|
|
|
|
|
''}"}
|
|
|
|
|
'';
|
|
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
|
in
|
|
|
|
|
{
|
2020-04-24 23:36:52 +00:00
|
|
|
|
description = "OpenVPN instance ‘${name}’";
|
|
|
|
|
|
|
|
|
|
wantedBy = optional cfg.autoStart "multi-user.target";
|
|
|
|
|
after = [ "network.target" ];
|
|
|
|
|
|
2021-04-05 15:23:46 +00:00
|
|
|
|
path = [ pkgs.iptables pkgs.iproute2 pkgs.nettools ];
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
serviceConfig.ExecStart = "@${openvpn}/sbin/openvpn openvpn --suppress-timestamps --config ${configFile}";
|
|
|
|
|
serviceConfig.Restart = "always";
|
|
|
|
|
serviceConfig.Type = "notify";
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
|
restartService = optionalAttrs cfg.restartAfterSleep {
|
|
|
|
|
openvpn-restart = {
|
|
|
|
|
wantedBy = [ "sleep.target" ];
|
|
|
|
|
path = [ pkgs.procps ];
|
|
|
|
|
script = "pkill --signal SIGHUP --exact openvpn";
|
|
|
|
|
#SIGHUP makes openvpn process to self-exit and then it got restarted by systemd because of Restart=always
|
|
|
|
|
description = "Sends a signal to OpenVPN process to trigger a restart after return from sleep";
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
in
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
imports = [
|
|
|
|
|
(mkRemovedOptionModule [ "services" "openvpn" "enable" ] "")
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
|
|
services.openvpn.servers = mkOption {
|
2023-02-09 11:40:11 +00:00
|
|
|
|
default = { };
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2021-10-06 13:57:05 +00:00
|
|
|
|
example = literalExpression ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
{
|
|
|
|
|
server = {
|
|
|
|
|
config = '''
|
|
|
|
|
# Simplest server configuration: https://community.openvpn.net/openvpn/wiki/StaticKeyMiniHowto
|
|
|
|
|
# server :
|
|
|
|
|
dev tun
|
|
|
|
|
ifconfig 10.8.0.1 10.8.0.2
|
|
|
|
|
secret /root/static.key
|
|
|
|
|
''';
|
|
|
|
|
up = "ip route add ...";
|
|
|
|
|
down = "ip route del ...";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
client = {
|
|
|
|
|
config = '''
|
|
|
|
|
client
|
|
|
|
|
remote vpn.example.org
|
|
|
|
|
dev tun
|
|
|
|
|
proto tcp-client
|
|
|
|
|
port 8080
|
|
|
|
|
ca /root/.vpn/ca.crt
|
|
|
|
|
cert /root/.vpn/alice.crt
|
|
|
|
|
key /root/.vpn/alice.key
|
|
|
|
|
''';
|
|
|
|
|
up = "echo nameserver $nameserver | ''${pkgs.openresolv}/sbin/resolvconf -m 0 -a $dev";
|
|
|
|
|
down = "''${pkgs.openresolv}/sbin/resolvconf -d $dev";
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
'';
|
|
|
|
|
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Each attribute of this option defines a systemd service that
|
|
|
|
|
runs an OpenVPN instance. These can be OpenVPN servers or
|
|
|
|
|
clients. The name of each systemd service is
|
2022-08-12 12:06:08 +00:00
|
|
|
|
`openvpn-«name».service`,
|
|
|
|
|
where «name» is the corresponding
|
2020-04-24 23:36:52 +00:00
|
|
|
|
attribute name.
|
|
|
|
|
'';
|
|
|
|
|
|
|
|
|
|
type = with types; attrsOf (submodule {
|
|
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
|
|
config = mkOption {
|
|
|
|
|
type = types.lines;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Configuration of this OpenVPN instance. See
|
2022-08-12 12:06:08 +00:00
|
|
|
|
{manpage}`openvpn(8)`
|
2020-04-24 23:36:52 +00:00
|
|
|
|
for details.
|
|
|
|
|
|
|
|
|
|
To import an external config file, use the following definition:
|
2022-08-12 12:06:08 +00:00
|
|
|
|
`config = "config /path/to/config.ovpn"`
|
2020-04-24 23:36:52 +00:00
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
up = mkOption {
|
|
|
|
|
default = "";
|
|
|
|
|
type = types.lines;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Shell commands executed when the instance is starting.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
down = mkOption {
|
|
|
|
|
default = "";
|
|
|
|
|
type = types.lines;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Shell commands executed when the instance is shutting down.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
autoStart = mkOption {
|
|
|
|
|
default = true;
|
|
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Whether this OpenVPN instance should be started automatically.";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
updateResolvConf = mkOption {
|
|
|
|
|
default = false;
|
|
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
Use the script from the update-resolv-conf package to automatically
|
|
|
|
|
update resolv.conf with the DNS information provided by openvpn. The
|
|
|
|
|
script will be run after the "up" commands and before the "down" commands.
|
|
|
|
|
'';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
authUserPass = mkOption {
|
|
|
|
|
default = null;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
|
This option can be used to store the username / password credentials
|
|
|
|
|
with the "auth-user-pass" authentication method.
|
|
|
|
|
|
|
|
|
|
WARNING: Using this option will put the credentials WORLD-READABLE in the Nix store!
|
|
|
|
|
'';
|
|
|
|
|
type = types.nullOr (types.submodule {
|
|
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
username = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "The username to store inside the credentials file.";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
type = types.str;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
password = mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "The password to store inside the credentials file.";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
type = types.str;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
|
services.openvpn.restartAfterSleep = mkOption {
|
|
|
|
|
default = true;
|
|
|
|
|
type = types.bool;
|
2024-04-21 15:54:59 +00:00
|
|
|
|
description = "Whether OpenVPN client should be restarted after sleep.";
|
2023-02-09 11:40:11 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
|
config = mkIf (cfg.servers != { }) {
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
2023-02-09 11:40:11 +00:00
|
|
|
|
systemd.services = (listToAttrs (mapAttrsFlatten (name: value: nameValuePair "openvpn-${name}" (makeOpenVPNJob value name)) cfg.servers))
|
|
|
|
|
// restartService;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
|
environment.systemPackages = [ openvpn ];
|
|
|
|
|
|
|
|
|
|
boot.kernelModules = [ "tun" ];
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|