84 lines
2.0 KiB
Nix
84 lines
2.0 KiB
Nix
{ config, lib, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
cfg = config.clerie.gre-tunnel;
|
|
|
|
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}
|
|
'';
|
|
};
|
|
|
|
checkOpts = { config, ... }@moduleAttrs: {
|
|
options = {
|
|
remote = mkOption {
|
|
type = types.str;
|
|
};
|
|
local = mkOption {
|
|
type = types.str;
|
|
};
|
|
address = mkOption {
|
|
type = types.str;
|
|
};
|
|
preSetup = mkOption {
|
|
type = types.str;
|
|
default = "";
|
|
};
|
|
postSetup = mkOption {
|
|
type = types.str;
|
|
default = "";
|
|
};
|
|
postShutdown = mkOption {
|
|
type = types.str;
|
|
default = "";
|
|
};
|
|
};
|
|
};
|
|
|
|
in {
|
|
options = {
|
|
clerie.gre-tunnel = {
|
|
enable = mkEnableOption "Declarative Policy-Routing";
|
|
ipv6 = mkOption {
|
|
type = with types; attrsOf (submodule checkOpts);
|
|
default = {};
|
|
};
|
|
ipv4 = mkOption {
|
|
type = with types; attrsOf (submodule checkOpts);
|
|
default = {};
|
|
};
|
|
};
|
|
};
|
|
|
|
config = mkIf cfg.enable {
|
|
systemd.services =
|
|
(mapAttrsToList (generateInterfaceUnit false) cfg.ipv4)
|
|
++ (mapAttrsToList (generateInterfaceUnit true) cfg.ipv6);
|
|
};
|
|
}
|