1
0
nixfiles/modules/gre-tunnel/default.nix

84 lines
2.0 KiB
Nix
Raw Normal View History

2020-12-17 23:43:52 +01:00
{ config, lib, ... }:
with lib;
let
cfg = config.clerie.gre-tunnel;
2021-01-02 16:52:45 +01:00
generateInterfaceUnit = isIPv6: name: tunnel:
nameValuePair "gre-tunnel-${name}" {
description = "GRE Tunnel - ${name}";
requires = [ "network-online.target" ];
after = [ "network.target" "network-online.target" ];
wantedBy = [ "multi-user.target" ];
environment.DEVICE = name;
path = with pkgs; [ iproute ];
serviceConfig = {
Type = "oneshot";
RemainAfterExit = true;
};
script = ''
${tunnel.preSetup}
ip${optionalString isIPv6 " -6"} tunnel add ${name} mode gre remote ${tunnel.remote} local ${tunnel.local}
ip link set ${name} up
ip${optionalString isIPv6 " -6"} a add ${tunnel.address} dev ${name}
${tunnel.postSetup}
'';
postStop = ''
ip link set ${name} down
ip tunnel del ${name}
${tunnel.postShutdown}
'';
};
2020-12-17 23:43:52 +01:00
checkOpts = { config, ... }@moduleAttrs: {
options = {
remote = mkOption {
type = types.str;
};
local = mkOption {
type = types.str;
};
address = mkOption {
type = types.str;
};
2021-01-02 16:52:45 +01:00
preSetup = mkOption {
type = types.str;
default = "";
};
postSetup = mkOption {
type = types.str;
default = "";
};
postShutdown = mkOption {
type = types.str;
default = "";
};
2020-12-17 23:43:52 +01:00
};
};
in {
options = {
clerie.gre-tunnel = {
enable = mkEnableOption "Declarative Policy-Routing";
ipv6 = mkOption {
type = with types; attrsOf (submodule checkOpts);
2021-01-02 16:52:45 +01:00
default = {};
2020-12-17 23:43:52 +01:00
};
ipv4 = mkOption {
type = with types; attrsOf (submodule checkOpts);
2021-01-02 16:52:45 +01:00
default = {};
2020-12-17 23:43:52 +01:00
};
};
};
config = mkIf cfg.enable {
2021-01-02 16:52:45 +01:00
systemd.services =
(mapAttrsToList (generateInterfaceUnit false) cfg.ipv4)
++ (mapAttrsToList (generateInterfaceUnit true) cfg.ipv6);
2020-12-17 23:43:52 +01:00
};
}