2009-10-12 18:36:19 +02:00
|
|
|
{ config, pkgs, ... }:
|
2009-05-01 19:57:07 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
with pkgs.lib;
|
2009-05-01 19:57:07 +02:00
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.openvpn;
|
|
|
|
|
|
|
|
inherit (pkgs) openvpn;
|
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
PATH = "${pkgs.iptables}/sbin:${pkgs.coreutils}/bin:${pkgs.iproute}/sbin:${pkgs.nettools}/sbin";
|
2009-05-01 19:57:07 +02:00
|
|
|
|
2009-10-23 13:30:54 +02:00
|
|
|
makeOpenVPNJob = cfg : name:
|
2009-05-01 19:57:07 +02:00
|
|
|
let
|
|
|
|
upScript = ''
|
|
|
|
#!/bin/sh
|
2009-10-23 13:30:54 +02:00
|
|
|
exec &> /var/log/openvpn-${name}-up
|
2009-05-01 19:57:07 +02:00
|
|
|
PATH=${PATH}
|
|
|
|
${cfg.up}
|
|
|
|
'';
|
|
|
|
downScript = ''
|
|
|
|
#!/bin/sh
|
2009-10-23 13:30:54 +02:00
|
|
|
exec &> /var/log/openvpn-${name}-down
|
2009-05-01 19:57:07 +02:00
|
|
|
PATH=${PATH}
|
|
|
|
${cfg.down}
|
|
|
|
'';
|
2009-10-23 13:30:54 +02:00
|
|
|
configFile = pkgs.writeText "openvpn-config-${name}"
|
2009-10-12 18:36:19 +02:00
|
|
|
''
|
2009-10-23 13:30:54 +02:00
|
|
|
${if cfg.up != "" || cfg.down != "" then "script-security 2" else ""}
|
2009-10-12 18:36:19 +02:00
|
|
|
${cfg.config}
|
2009-10-23 13:30:54 +02:00
|
|
|
${if cfg.up != "" then "up ${pkgs.writeScript "openvpn-${name}-up" upScript}" else "" }
|
|
|
|
${if cfg.down != "" then "down ${pkgs.writeScript "openvpn-${name}-down" downScript}" else "" }
|
2009-10-12 18:36:19 +02:00
|
|
|
'';
|
2009-05-01 19:57:07 +02:00
|
|
|
in {
|
2009-10-23 13:30:54 +02:00
|
|
|
description = "OpenVPN-${name}";
|
2009-05-01 19:57:07 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
startOn = "network-interfaces/started";
|
|
|
|
stopOn = "network-interfaces/stop";
|
2009-05-01 19:57:07 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
environment = { PATH = "${pkgs.coreutils}/bin"; };
|
2009-05-01 19:57:07 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
script =
|
|
|
|
''
|
2009-10-23 13:30:54 +02:00
|
|
|
exec &> /var/log/openvpn-${name}
|
2009-10-12 18:36:19 +02:00
|
|
|
${config.system.sbin.modprobe} tun || true
|
2009-05-01 19:57:07 +02:00
|
|
|
${openvpn}/sbin/openvpn --config ${configFile}
|
2009-10-12 18:36:19 +02:00
|
|
|
'';
|
2009-05-01 19:57:07 +02:00
|
|
|
};
|
|
|
|
|
2009-10-23 13:30:54 +02:00
|
|
|
openvpnInstanceOptions = {
|
|
|
|
|
|
|
|
config = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
description = ''
|
|
|
|
config of this openvpn instance
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
up = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.string;
|
|
|
|
description = ''
|
|
|
|
script which is run when server instance starts up succesfully.
|
|
|
|
Use it to setup firewall and routing
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
down = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.string;
|
|
|
|
description = ''
|
|
|
|
script which is run when server instance shuts down
|
|
|
|
Usually this reverts what up has done
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-05-01 19:57:07 +02:00
|
|
|
in
|
2009-10-12 18:36:19 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
services.openvpn = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = "Whether to enable OpenVPN.";
|
|
|
|
};
|
|
|
|
|
2009-10-23 13:30:54 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
servers = mkOption {
|
2009-10-23 13:30:54 +02:00
|
|
|
|
|
|
|
default = {};
|
|
|
|
|
|
|
|
example = {
|
|
|
|
mostSimple = {
|
|
|
|
config = ''
|
|
|
|
# Most simple 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 static.key
|
|
|
|
'';
|
|
|
|
up = "ip route add ..!";
|
|
|
|
down = "ip route add ..!";
|
|
|
|
};
|
|
|
|
clientMostSimple = {
|
|
|
|
config = ''
|
|
|
|
#client:
|
|
|
|
#remote myremote.mydomain
|
|
|
|
#dev tun
|
|
|
|
#ifconfig 10.8.0.2 10.8.0.1
|
|
|
|
#secret static.key
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
serverScalable = {
|
|
|
|
config = ''
|
|
|
|
multiple clienst
|
|
|
|
see example file found in http://openvpn.net/index.php/documentation/howto.html
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
# !!! clean up this description please
|
|
|
|
description = ''
|
2009-10-23 13:30:54 +02:00
|
|
|
You can define multiple openvpn instances.
|
|
|
|
|
|
|
|
The id of an instance is given by the attribute name.
|
|
|
|
|
|
|
|
Each instance will result in a new job file.
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2009-10-23 13:30:54 +02:00
|
|
|
Additionally you can specify the up/ down scripts by setting
|
|
|
|
the up down properties.
|
|
|
|
Config lines up=/nix/store/xxx-up-script down=...
|
|
|
|
will be appended to your configuration file automatically
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2009-10-23 13:30:54 +02:00
|
|
|
If you define at least one of up/down "script-security 2" will be
|
|
|
|
prepended to your config otherwise you scripts aren't run by openvpn
|
|
|
|
|
|
|
|
Don't forget to check that the all package sizes can be sent. For
|
|
|
|
examlpe if scp hangs you should set --fragment XXX --mssfix YYY.
|
2009-10-12 18:36:19 +02:00
|
|
|
'';
|
2009-10-23 13:30:54 +02:00
|
|
|
|
|
|
|
type = types.attrsOf types.optionSet;
|
|
|
|
options = [ openvpnInstanceOptions ];
|
2009-10-12 18:36:19 +02:00
|
|
|
};
|
2009-10-23 13:30:54 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-05-01 19:57:07 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
###### implementation
|
2009-05-01 19:57:07 +02:00
|
|
|
|
2009-10-12 18:36:19 +02:00
|
|
|
config = mkIf cfg.enable {
|
2009-10-23 13:30:54 +02:00
|
|
|
jobs = listToAttrs (mapAttrsFlatten (name: value: nameValuePair "openvpn-${name}" (makeOpenVPNJob value name)) cfg.servers);
|
2009-05-01 19:57:07 +02:00
|
|
|
};
|
2009-10-12 18:36:19 +02:00
|
|
|
|
2009-05-01 19:57:07 +02:00
|
|
|
}
|