openvpn.nix: Use systemd.*

Also add an option ‘autoStart’ to configure whether an OpenVPN
instance should be started automatically.  And don't log to
/var/log/openvpn-* anymore.
This commit is contained in:
Eelco Dolstra 2013-05-28 14:38:13 +02:00
parent f60393975f
commit 2ec6759f5f
1 changed files with 12 additions and 6 deletions

View File

@ -15,7 +15,6 @@ let
upScript = '' upScript = ''
#! /bin/sh #! /bin/sh
exec > /var/log/openvpn-${name}-up 2>&1
export PATH=${path} export PATH=${path}
# For convenience in client scripts, extract the remote domain # For convenience in client scripts, extract the remote domain
@ -34,13 +33,13 @@ let
downScript = '' downScript = ''
#! /bin/sh #! /bin/sh
exec > /var/log/openvpn-${name}-down 2>&1
export PATH=${path} export PATH=${path}
${cfg.down} ${cfg.down}
''; '';
configFile = pkgs.writeText "openvpn-config-${name}" configFile = pkgs.writeText "openvpn-config-${name}"
'' ''
errors-to-stderr
${optionalString (cfg.up != "" || cfg.down != "") "script-security 2"} ${optionalString (cfg.up != "" || cfg.down != "") "script-security 2"}
${cfg.config} ${cfg.config}
${optionalString (cfg.up != "") "up ${pkgs.writeScript "openvpn-${name}-up" upScript}"} ${optionalString (cfg.up != "") "up ${pkgs.writeScript "openvpn-${name}-up" upScript}"}
@ -50,12 +49,13 @@ let
in { in {
description = "OpenVPN instance ${name}"; description = "OpenVPN instance ${name}";
startOn = mkDefault "started network-interfaces"; wantedBy = optional cfg.autoStart [ "multi-user.target" ];
stopOn = mkDefault "stopping network-interfaces"; after = [ "network-interfaces.target" ];
path = [ pkgs.iptables pkgs.iproute pkgs.nettools ]; path = [ pkgs.iptables pkgs.iproute pkgs.nettools ];
exec = "${openvpn}/sbin/openvpn --config ${configFile}"; serviceConfig.ExecStart = "@${openvpn}/sbin/openvpn openvpn --config ${configFile}";
serviceConfig.Restart = "always";
}; };
in in
@ -144,6 +144,12 @@ in
''; '';
}; };
autoStart = mkOption {
default = true;
type = types.bool;
description = "Whether this OpenVPN instance should be started automatically.";
};
}; };
}; };
@ -155,7 +161,7 @@ in
config = mkIf (cfg.servers != {}) { config = mkIf (cfg.servers != {}) {
jobs = listToAttrs (mapAttrsFlatten (name: value: nameValuePair "openvpn-${name}" (makeOpenVPNJob value name)) cfg.servers); systemd.services = listToAttrs (mapAttrsFlatten (name: value: nameValuePair "openvpn-${name}" (makeOpenVPNJob value name)) cfg.servers);
environment.systemPackages = [ openvpn ]; environment.systemPackages = [ openvpn ];