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

90 lines
2.4 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
2020-12-17 23:43:52 +01:00
with lib;
let
cfg = config.clerie.gre-tunnel;
generateInterfaceUnit = isIPv6: (name: tunnel:
2021-01-02 16:52:45 +01:00
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}
2021-01-13 13:45:26 +01:00
ip${optionalString isIPv6 " -6"} tunnel add ${name} mode ${optionalString isIPv6 "ip6"}gre remote ${tunnel.remote} local ${tunnel.local}
2021-01-02 16:52:45 +01:00
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}
'';
});
2021-01-02 16:52:45 +01:00
2020-12-17 23:43:52 +01:00
checkOpts = { config, ... }@moduleAttrs: {
options = {
remote = mkOption {
type = types.str;
description = "Address of reciever.";
2020-12-17 23:43:52 +01:00
};
local = mkOption {
type = types.str;
description = "Address our packets originate from.";
2020-12-17 23:43:52 +01:00
};
address = mkOption {
type = types.str;
description = "Our address in this tunnel.";
2020-12-17 23:43:52 +01:00
};
2021-01-02 16:52:45 +01:00
preSetup = mkOption {
type = types.str;
default = "";
description = "Commands called at the start of the interface setup.";
2021-01-02 16:52:45 +01:00
};
postSetup = mkOption {
type = types.str;
default = "";
description = "Commands called at the end of the interface setup.";
2021-01-02 16:52:45 +01:00
};
postShutdown = mkOption {
type = types.str;
default = "";
description = "Commands called after shutting down the interface.";
2021-01-02 16:52:45 +01:00
};
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 =
(mapAttrs' (generateInterfaceUnit false) cfg.ipv4)
// (mapAttrs' (generateInterfaceUnit true) cfg.ipv6);
2020-12-17 23:43:52 +01:00
};
}