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

165 lines
4.6 KiB
Nix
Raw Normal View History

2015-08-19 09:37:20 +02:00
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.softether;
in
{
###### interface
options = {
services.softether = {
enable = mkEnableOption "SoftEther VPN services";
2017-09-25 21:03:16 +02:00
package = mkOption {
type = types.package;
default = pkgs.softether;
defaultText = "pkgs.softether";
description = ''
softether derivation to use.
'';
};
2015-08-19 09:37:20 +02: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;
2017-09-25 21:03:16 +02:00
default = "${cfg.package.dataDir}";
2015-08-19 09:37:20 +02:00
description = ''
Data directory for SoftEther VPN.
'';
};
};
};
###### implementation
config = mkIf cfg.enable (
mkMerge [{
environment.systemPackages = [
2017-09-25 21:03:16 +02:00
(pkgs.lib.overrideDerivation cfg.package (attrs: {
2015-08-19 09:37:20 +02:00
dataDir = cfg.dataDir;
}))
];
systemd.services."softether-init" = {
description = "SoftEther VPN services initial task";
2017-09-25 21:03:16 +02:00
wantedBy = [ "network.target" ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = false;
};
script = ''
2015-08-19 09:37:20 +02: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
2017-09-25 21:03:16 +02:00
install -m0600 ${cfg.package}${cfg.dataDir}/$d/hamcore.se2 ${cfg.dataDir}/$d/hamcore.se2
2015-08-19 09:37:20 +02:00
fi
done
rm -rf ${cfg.dataDir}/vpncmd/vpncmd
2017-09-25 21:03:16 +02:00
ln -s ${cfg.package}${cfg.dataDir}/vpncmd/vpncmd ${cfg.dataDir}/vpncmd/vpncmd
2015-08-19 09:37:20 +02:00
'';
};
}
(mkIf (cfg.vpnserver.enable) {
systemd.services.vpnserver = {
description = "SoftEther VPN Server";
2017-09-25 21:03:16 +02:00
after = [ "softether-init.service" ];
requires = [ "softether-init.service" ];
wantedBy = [ "network.target" ];
2015-08-19 09:37:20 +02:00
serviceConfig = {
Type = "forking";
2017-09-25 21:03:16 +02:00
ExecStart = "${cfg.package}/bin/vpnserver start";
ExecStop = "${cfg.package}/bin/vpnserver stop";
2015-08-19 09:37:20 +02:00
};
preStart = ''
rm -rf ${cfg.dataDir}/vpnserver/vpnserver
2017-09-25 21:03:16 +02:00
ln -s ${cfg.package}${cfg.dataDir}/vpnserver/vpnserver ${cfg.dataDir}/vpnserver/vpnserver
2015-08-19 09:37:20 +02:00
'';
postStop = ''
rm -rf ${cfg.dataDir}/vpnserver/vpnserver
'';
};
})
(mkIf (cfg.vpnbridge.enable) {
systemd.services.vpnbridge = {
description = "SoftEther VPN Bridge";
2017-09-25 21:03:16 +02:00
after = [ "softether-init.service" ];
requires = [ "softether-init.service" ];
wantedBy = [ "network.target" ];
2015-08-19 09:37:20 +02:00
serviceConfig = {
Type = "forking";
2017-09-25 21:03:16 +02:00
ExecStart = "${cfg.package}/bin/vpnbridge start";
ExecStop = "${cfg.package}/bin/vpnbridge stop";
2015-08-19 09:37:20 +02:00
};
preStart = ''
rm -rf ${cfg.dataDir}/vpnbridge/vpnbridge
2017-09-25 21:03:16 +02:00
ln -s ${cfg.package}${cfg.dataDir}/vpnbridge/vpnbridge ${cfg.dataDir}/vpnbridge/vpnbridge
2015-08-19 09:37:20 +02:00
'';
postStop = ''
rm -rf ${cfg.dataDir}/vpnbridge/vpnbridge
'';
};
})
(mkIf (cfg.vpnclient.enable) {
systemd.services.vpnclient = {
description = "SoftEther VPN Client";
2017-09-25 21:03:16 +02:00
after = [ "softether-init.service" ];
requires = [ "softether-init.service" ];
wantedBy = [ "network.target" ];
2015-08-19 09:37:20 +02:00
serviceConfig = {
Type = "forking";
2017-09-25 21:03:16 +02:00
ExecStart = "${cfg.package}/bin/vpnclient start";
ExecStop = "${cfg.package}/bin/vpnclient stop";
2015-08-19 09:37:20 +02:00
};
preStart = ''
rm -rf ${cfg.dataDir}/vpnclient/vpnclient
2017-09-25 21:03:16 +02:00
ln -s ${cfg.package}${cfg.dataDir}/vpnclient/vpnclient ${cfg.dataDir}/vpnclient/vpnclient
2015-08-19 09:37:20 +02:00
'';
postStart = ''
sleep 1
${cfg.vpnclient.up}
'';
postStop = ''
rm -rf ${cfg.dataDir}/vpnclient/vpnclient
sleep 1
${cfg.vpnclient.down}
'';
};
boot.kernelModules = [ "tun" ];
})
]);
}