2021-11-13 13:33:25 +02:00
|
|
|
{ pkgs, inputs, lib, hosts, config, ... }:
|
|
|
|
let
|
|
|
|
inherit (config.networking) hostName;
|
2022-02-05 20:09:02 +02:00
|
|
|
inherit (inputs.self.packages.${pkgs.system}) hyprspace;
|
2021-11-13 13:33:25 +02:00
|
|
|
hyprspaceCapableNodes = lib.filterAttrs (_: host: host ? hypr) hosts;
|
|
|
|
peersFormatted = builtins.mapAttrs (_: x: { "${x.hypr.addr}".id = x.hypr.id; }) hyprspaceCapableNodes;
|
|
|
|
peersFiltered = lib.filterAttrs (name: _: name != hostName) peersFormatted;
|
2022-09-26 02:17:40 +03:00
|
|
|
peerList = lib.foldAttrs (n: _: n) null (builtins.attrValues peersFiltered);
|
2021-11-13 13:33:25 +02:00
|
|
|
myNode = hosts.${hostName};
|
|
|
|
listenPort = myNode.hypr.listenPort or 8001;
|
|
|
|
|
2022-10-08 13:08:34 +03:00
|
|
|
routes' = map (x: lib.genAttrs (x.hypr.routes or []) (_: { ip = x.hypr.addr; })) (builtins.attrValues hyprspaceCapableNodes);
|
|
|
|
routes = builtins.foldl' (x: y: x // y) {} (lib.flatten routes');
|
|
|
|
|
2022-09-26 02:17:40 +03:00
|
|
|
interfaceConfig = pkgs.writeText "hyprspace.yml" (builtins.toJSON {
|
|
|
|
interface = {
|
|
|
|
name = "hyprspace";
|
2022-09-26 02:27:12 +03:00
|
|
|
listen_port = listenPort;
|
2022-09-26 02:17:40 +03:00
|
|
|
inherit (myNode.hypr) id;
|
|
|
|
address = "${myNode.hypr.addr}/24";
|
|
|
|
private_key = "@HYPRSPACEPRIVATEKEY@";
|
|
|
|
};
|
|
|
|
peers = peerList;
|
2022-10-08 13:08:34 +03:00
|
|
|
inherit routes;
|
2022-09-26 02:17:40 +03:00
|
|
|
});
|
2021-11-13 13:33:25 +02:00
|
|
|
|
|
|
|
privateKeyFile = config.age.secrets.hyprspace-key.path;
|
|
|
|
runConfig = "/run/hyprspace.yml";
|
|
|
|
in {
|
2022-03-13 00:16:38 +02:00
|
|
|
networking.hosts = lib.mapAttrs' (k: v: lib.nameValuePair v.hypr.addr [k "${k}.hypr"]) hyprspaceCapableNodes;
|
2021-11-13 13:33:25 +02:00
|
|
|
age.secrets.hyprspace-key = {
|
2022-02-05 20:09:02 +02:00
|
|
|
file = ../../secrets/hyprspace-key- + "${hostName}.age";
|
2021-11-13 13:33:25 +02:00
|
|
|
mode = "0400";
|
|
|
|
};
|
|
|
|
systemd.services.hyprspace = {
|
|
|
|
enable = true;
|
2022-09-27 00:06:13 +03:00
|
|
|
after = [ "network-online.target" ];
|
2021-11-13 13:33:25 +02:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
preStart = ''
|
|
|
|
test -e ${runConfig} && rm ${runConfig}
|
2022-09-26 02:17:40 +03:00
|
|
|
cp ${interfaceConfig} ${runConfig}
|
2021-11-13 13:33:25 +02:00
|
|
|
chmod 0600 ${runConfig}
|
2022-09-26 02:17:40 +03:00
|
|
|
${pkgs.replace-secret}/bin/replace-secret '@HYPRSPACEPRIVATEKEY@' "${privateKeyFile}" ${runConfig}
|
2021-11-13 13:33:25 +02:00
|
|
|
chmod 0400 ${runConfig}
|
|
|
|
'';
|
|
|
|
serviceConfig = {
|
2022-06-18 00:53:36 +03:00
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "5s";
|
2021-11-13 13:33:25 +02:00
|
|
|
ExecStart = "${hyprspace}/bin/hyprspace up hyprspace -f -c ${runConfig}";
|
|
|
|
ExecStop = "${hyprspace}/bin/hyprspace down hyprspace";
|
|
|
|
IPAddressDeny = [
|
|
|
|
"10.0.0.0/8"
|
|
|
|
"100.64.0.0/10"
|
|
|
|
"169.254.0.0/16"
|
|
|
|
"172.16.0.0/12"
|
|
|
|
"192.0.0.0/24"
|
|
|
|
"192.0.2.0/24"
|
|
|
|
"192.168.0.0/16"
|
|
|
|
"198.18.0.0/15"
|
|
|
|
"198.51.100.0/24"
|
|
|
|
"203.0.113.0/24"
|
|
|
|
"240.0.0.0/4"
|
|
|
|
"100::/64"
|
|
|
|
"2001:2::/48"
|
|
|
|
"2001:db8::/32"
|
|
|
|
"fc00::/7"
|
|
|
|
"fe80::/10"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
networking.firewall = {
|
|
|
|
allowedTCPPorts = [ listenPort ];
|
|
|
|
allowedUDPPorts = [ listenPort ];
|
|
|
|
trustedInterfaces = [ "hyprspace" ];
|
|
|
|
};
|
|
|
|
}
|