nixpkgs/nixos/modules/services/networking/softether.nix

166 lines
4.5 KiB
Nix
Raw Normal View History

2015-08-19 07:37:20 +00:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.softether;
2018-02-22 15:37:59 +00:00
package = cfg.package.override { dataDir = cfg.dataDir; };
2015-08-19 07:37:20 +00:00
in
{
###### interface
options = {
services.softether = {
enable = mkEnableOption "SoftEther VPN services";
2017-09-25 19:03:16 +00:00
package = mkOption {
type = types.package;
default = pkgs.softether;
defaultText = "pkgs.softether";
description = ''
softether derivation to use.
'';
};
2015-08-19 07:37:20 +00:00
vpnserver.enable = mkEnableOption "SoftEther VPN Server";
vpnbridge.enable = mkEnableOption "SoftEther VPN Bridge";
vpnclient = {
enable = mkEnableOption "SoftEther VPN Client";
up = mkOption {
type = types.lines;
default = "";
description = ''
Shell commands executed when the Virtual Network Adapter(s) is/are starting.
'';
};
down = mkOption {
type = types.lines;
default = "";
description = ''
Shell commands executed when the Virtual Network Adapter(s) is/are shutting down.
'';
};
};
dataDir = mkOption {
type = types.string;
2018-02-22 15:37:59 +00:00
default = "/var/lib/softether";
2015-08-19 07:37:20 +00:00
description = ''
Data directory for SoftEther VPN.
'';
};
};
};
###### implementation
config = mkIf cfg.enable (
mkMerge [{
2018-02-22 15:37:59 +00:00
environment.systemPackages = [ package ];
systemd.services."softether-init" = {
description = "SoftEther VPN services initial task";
after = [ "keys.target" ];
wants = [ "keys.target" ];
2017-09-25 19:03:16 +00:00
wantedBy = [ "network.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = false;
};
script = ''
2015-08-19 07:37:20 +00:00
for d in vpnserver vpnbridge vpnclient vpncmd; do
if ! test -e ${cfg.dataDir}/$d; then
${pkgs.coreutils}/bin/mkdir -m0700 -p ${cfg.dataDir}/$d
2018-02-22 15:37:59 +00:00
install -m0600 ${package}${cfg.dataDir}/$d/hamcore.se2 ${cfg.dataDir}/$d/hamcore.se2
2015-08-19 07:37:20 +00:00
fi
done
rm -rf ${cfg.dataDir}/vpncmd/vpncmd
2018-02-22 15:37:59 +00:00
ln -s ${package}${cfg.dataDir}/vpncmd/vpncmd ${cfg.dataDir}/vpncmd/vpncmd
2015-08-19 07:37:20 +00:00
'';
};
}
(mkIf (cfg.vpnserver.enable) {
systemd.services.vpnserver = {
description = "SoftEther VPN Server";
2017-09-25 19:03:16 +00:00
after = [ "softether-init.service" ];
requires = [ "softether-init.service" ];
wantedBy = [ "network.target" ];
2015-08-19 07:37:20 +00:00
serviceConfig = {
Type = "forking";
2018-02-22 15:37:59 +00:00
ExecStart = "${package}/bin/vpnserver start";
ExecStop = "${package}/bin/vpnserver stop";
2015-08-19 07:37:20 +00:00
};
preStart = ''
rm -rf ${cfg.dataDir}/vpnserver/vpnserver
2018-02-22 15:37:59 +00:00
ln -s ${package}${cfg.dataDir}/vpnserver/vpnserver ${cfg.dataDir}/vpnserver/vpnserver
2015-08-19 07:37:20 +00:00
'';
postStop = ''
rm -rf ${cfg.dataDir}/vpnserver/vpnserver
'';
};
})
(mkIf (cfg.vpnbridge.enable) {
systemd.services.vpnbridge = {
description = "SoftEther VPN Bridge";
2017-09-25 19:03:16 +00:00
after = [ "softether-init.service" ];
requires = [ "softether-init.service" ];
wantedBy = [ "network.target" ];
2015-08-19 07:37:20 +00:00
serviceConfig = {
Type = "forking";
2018-02-22 15:37:59 +00:00
ExecStart = "${package}/bin/vpnbridge start";
ExecStop = "${package}/bin/vpnbridge stop";
2015-08-19 07:37:20 +00:00
};
preStart = ''
rm -rf ${cfg.dataDir}/vpnbridge/vpnbridge
2018-02-22 15:37:59 +00:00
ln -s ${package}${cfg.dataDir}/vpnbridge/vpnbridge ${cfg.dataDir}/vpnbridge/vpnbridge
2015-08-19 07:37:20 +00:00
'';
postStop = ''
rm -rf ${cfg.dataDir}/vpnbridge/vpnbridge
'';
};
})
(mkIf (cfg.vpnclient.enable) {
systemd.services.vpnclient = {
description = "SoftEther VPN Client";
2017-09-25 19:03:16 +00:00
after = [ "softether-init.service" ];
requires = [ "softether-init.service" ];
wantedBy = [ "network.target" ];
2015-08-19 07:37:20 +00:00
serviceConfig = {
Type = "forking";
2018-02-22 15:37:59 +00:00
ExecStart = "${package}/bin/vpnclient start";
ExecStop = "${package}/bin/vpnclient stop";
2015-08-19 07:37:20 +00:00
};
preStart = ''
rm -rf ${cfg.dataDir}/vpnclient/vpnclient
2018-02-22 15:37:59 +00:00
ln -s ${package}${cfg.dataDir}/vpnclient/vpnclient ${cfg.dataDir}/vpnclient/vpnclient
2015-08-19 07:37:20 +00:00
'';
postStart = ''
sleep 1
${cfg.vpnclient.up}
'';
postStop = ''
rm -rf ${cfg.dataDir}/vpnclient/vpnclient
sleep 1
${cfg.vpnclient.down}
'';
};
boot.kernelModules = [ "tun" ];
})
]);
}