2009-10-12 16:36:19 +00:00
|
|
|
|
{ config, pkgs, ... }:
|
2009-05-01 17:57:07 +00:00
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
|
with pkgs.lib;
|
2009-05-01 17:57:07 +00:00
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
|
|
cfg = config.services.openvpn;
|
|
|
|
|
|
|
|
|
|
inherit (pkgs) openvpn;
|
|
|
|
|
|
2012-02-20 20:10:07 +00:00
|
|
|
|
makeOpenVPNJob = cfg: name:
|
2009-05-01 17:57:07 +00:00
|
|
|
|
let
|
2012-02-20 20:10:07 +00:00
|
|
|
|
|
2013-01-16 11:33:18 +00:00
|
|
|
|
path = (getAttr "openvpn-${name}" config.systemd.services).path;
|
2012-08-31 01:11:36 +00:00
|
|
|
|
|
2009-05-01 17:57:07 +00:00
|
|
|
|
upScript = ''
|
2012-02-20 20:10:07 +00:00
|
|
|
|
#! /bin/sh
|
|
|
|
|
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
|
2012-08-31 01:11:36 +00:00
|
|
|
|
|
2009-05-01 17:57:07 +00:00
|
|
|
|
${cfg.up}
|
|
|
|
|
'';
|
2012-08-31 01:11:36 +00:00
|
|
|
|
|
2009-05-01 17:57:07 +00:00
|
|
|
|
downScript = ''
|
2012-02-20 20:10:07 +00:00
|
|
|
|
#! /bin/sh
|
|
|
|
|
export PATH=${path}
|
2009-05-01 17:57:07 +00:00
|
|
|
|
${cfg.down}
|
|
|
|
|
'';
|
2012-08-31 01:11:36 +00:00
|
|
|
|
|
2009-10-23 11:30:54 +00:00
|
|
|
|
configFile = pkgs.writeText "openvpn-config-${name}"
|
2009-10-12 16:36:19 +00:00
|
|
|
|
''
|
2013-05-28 12:38:13 +00:00
|
|
|
|
errors-to-stderr
|
2012-02-20 20:10:07 +00:00
|
|
|
|
${optionalString (cfg.up != "" || cfg.down != "") "script-security 2"}
|
2009-10-12 16:36:19 +00:00
|
|
|
|
${cfg.config}
|
2012-02-20 20:10:07 +00:00
|
|
|
|
${optionalString (cfg.up != "") "up ${pkgs.writeScript "openvpn-${name}-up" upScript}"}
|
|
|
|
|
${optionalString (cfg.down != "") "down ${pkgs.writeScript "openvpn-${name}-down" downScript}"}
|
2009-10-12 16:36:19 +00:00
|
|
|
|
'';
|
2012-08-31 01:11:36 +00:00
|
|
|
|
|
2009-05-01 17:57:07 +00:00
|
|
|
|
in {
|
2012-02-20 20:10:07 +00:00
|
|
|
|
description = "OpenVPN instance ‘${name}’";
|
2009-05-01 17:57:07 +00:00
|
|
|
|
|
2013-06-02 08:23:03 +00:00
|
|
|
|
wantedBy = optional cfg.autoStart "multi-user.target";
|
2013-05-28 12:38:13 +00:00
|
|
|
|
after = [ "network-interfaces.target" ];
|
2009-05-01 17:57:07 +00:00
|
|
|
|
|
2012-02-20 20:10:07 +00:00
|
|
|
|
path = [ pkgs.iptables pkgs.iproute pkgs.nettools ];
|
2009-10-23 11:30:54 +00:00
|
|
|
|
|
2013-05-28 12:38:13 +00:00
|
|
|
|
serviceConfig.ExecStart = "@${openvpn}/sbin/openvpn openvpn --config ${configFile}";
|
|
|
|
|
serviceConfig.Restart = "always";
|
2009-10-23 11:30:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
2009-05-01 17:57:07 +00:00
|
|
|
|
in
|
2011-09-14 18:20:50 +00:00
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
|
2012-02-20 20:10:07 +00:00
|
|
|
|
/* !!! Obsolete. */
|
|
|
|
|
services.openvpn.enable = mkOption {
|
|
|
|
|
default = true;
|
|
|
|
|
description = "Whether to enable OpenVPN.";
|
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
|
2012-02-20 20:10:07 +00:00
|
|
|
|
services.openvpn.servers = mkOption {
|
|
|
|
|
default = {};
|
|
|
|
|
|
|
|
|
|
example = {
|
2012-08-31 01:11:36 +00:00
|
|
|
|
|
2012-02-20 20:10:07 +00:00
|
|
|
|
server = {
|
|
|
|
|
config = ''
|
|
|
|
|
# Simplest server configuration: http://openvpn.net/index.php/documentation/miscellaneous/static-key-mini-howto.html.
|
|
|
|
|
# server :
|
|
|
|
|
dev tun
|
|
|
|
|
ifconfig 10.8.0.1 10.8.0.2
|
|
|
|
|
secret /root/static.key
|
|
|
|
|
'';
|
|
|
|
|
up = "ip route add ...";
|
|
|
|
|
down = "ip route del ...";
|
|
|
|
|
};
|
2012-08-31 01:11:36 +00:00
|
|
|
|
|
2012-02-20 20:10:07 +00:00
|
|
|
|
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";
|
|
|
|
|
};
|
2012-08-31 01:11:36 +00:00
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-02-20 20:10:07 +00:00
|
|
|
|
description = ''
|
|
|
|
|
Each attribute of this option defines an Upstart job to run an
|
|
|
|
|
OpenVPN instance. These can be OpenVPN servers or clients.
|
|
|
|
|
The name of each Upstart job is
|
|
|
|
|
<literal>openvpn-</literal><replaceable>name</replaceable>,
|
|
|
|
|
where <replaceable>name</replaceable> is the corresponding
|
|
|
|
|
attribute name.
|
|
|
|
|
'';
|
2009-10-23 11:30:54 +00:00
|
|
|
|
|
2012-02-20 20:10:07 +00:00
|
|
|
|
type = types.attrsOf types.optionSet;
|
2012-08-31 01:11:36 +00:00
|
|
|
|
|
2012-02-20 20:10:07 +00:00
|
|
|
|
options = {
|
|
|
|
|
|
|
|
|
|
config = mkOption {
|
|
|
|
|
type = types.string;
|
|
|
|
|
description = ''
|
|
|
|
|
Configuration of this OpenVPN instance. See
|
|
|
|
|
<citerefentry><refentrytitle>openvpn</refentrytitle><manvolnum>8</manvolnum></citerefentry>
|
|
|
|
|
for details.
|
|
|
|
|
'';
|
2009-10-23 11:30:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
2012-02-20 20:10:07 +00:00
|
|
|
|
up = mkOption {
|
|
|
|
|
default = "";
|
|
|
|
|
type = types.string;
|
|
|
|
|
description = ''
|
|
|
|
|
Shell commands executed when the instance is starting.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2009-10-23 11:30:54 +00:00
|
|
|
|
|
2012-02-20 20:10:07 +00:00
|
|
|
|
down = mkOption {
|
|
|
|
|
default = "";
|
|
|
|
|
type = types.string;
|
|
|
|
|
description = ''
|
|
|
|
|
Shell commands executed when the instance is shutting down.
|
|
|
|
|
'';
|
|
|
|
|
};
|
2009-10-23 11:30:54 +00:00
|
|
|
|
|
2013-05-28 12:38:13 +00:00
|
|
|
|
autoStart = mkOption {
|
|
|
|
|
default = true;
|
|
|
|
|
type = types.bool;
|
|
|
|
|
description = "Whether this OpenVPN instance should be started automatically.";
|
|
|
|
|
};
|
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
|
};
|
2009-10-23 11:30:54 +00:00
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
2009-05-01 17:57:07 +00:00
|
|
|
|
|
2009-10-12 16:36:19 +00:00
|
|
|
|
###### implementation
|
2009-05-01 17:57:07 +00:00
|
|
|
|
|
2012-02-20 20:10:07 +00:00
|
|
|
|
config = mkIf (cfg.servers != {}) {
|
2010-10-18 10:40:08 +00:00
|
|
|
|
|
2013-05-28 12:38:13 +00:00
|
|
|
|
systemd.services = listToAttrs (mapAttrsFlatten (name: value: nameValuePair "openvpn-${name}" (makeOpenVPNJob value name)) cfg.servers);
|
2010-10-18 10:40:08 +00:00
|
|
|
|
|
|
|
|
|
environment.systemPackages = [ openvpn ];
|
2012-08-31 01:11:36 +00:00
|
|
|
|
|
2012-02-20 20:10:07 +00:00
|
|
|
|
boot.kernelModules = [ "tun" ];
|
2012-08-31 01:11:36 +00:00
|
|
|
|
|
2009-05-01 17:57:07 +00:00
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
|
2009-05-01 17:57:07 +00:00
|
|
|
|
}
|