config/modules/hyprspace/default.nix

88 lines
3.1 KiB
Nix
Raw Normal View History

2022-02-05 21:42:36 +02:00
{ inputs, pkgs, lib, hosts, config, ... }:
let
inherit (config.networking) hostName;
inherit (inputs.depot.packages.${pkgs.system}) hyprspace;
2022-02-05 21:42:36 +02:00
hyprspaceCapableNodes = lib.filterAttrs (_: host: host ? hypr) hosts;
peersFormatted = builtins.mapAttrs (_: x: {
inherit (x.hypr) id;
routes = map (net: { inherit net; }) ((x.hypr.routes or []) ++ [ "${x.hypr.addr}/32" ]);
}) hyprspaceCapableNodes;
2022-02-05 21:42:36 +02:00
peersFiltered = lib.filterAttrs (name: _: name != hostName) peersFormatted;
peerList = builtins.attrValues peersFiltered;
2022-02-05 21:42:36 +02:00
myNode = hosts.${hostName};
listenPort = myNode.hypr.listenPort or 8001;
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";
listen_port = listenPort;
inherit (myNode.hypr) id;
address = "${myNode.hypr.addr}/24";
private_key = "@HYPRSPACEPRIVATEKEY@";
};
peers = peerList;
});
2022-02-05 21:42:36 +02:00
privateKeyFile = config.age.secrets.hyprspace-key.path;
runConfig = "/run/hyprspace.yml";
in {
networking.hosts = lib.mapAttrs' (k: v: lib.nameValuePair (v.hypr.addr) ([k "${k}.hypr"])) hyprspaceCapableNodes;
age.secrets.hyprspace-key = {
file = ../../secrets/hyprspace-key- + "${hostName}.age";
mode = "0400";
};
2023-01-21 00:38:43 +02:00
environment.systemPackages = [
hyprspace
];
2022-02-05 21:42:36 +02:00
systemd.services.hyprspace = {
enable = true;
wantedBy = [ "multi-user.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
preStart = ''
test -e ${runConfig} && rm ${runConfig}
2022-09-26 02:17:40 +03:00
cp ${interfaceConfig} ${runConfig}
2022-02-05 21:42:36 +02:00
chmod 0600 ${runConfig}
2022-09-26 02:17:40 +03:00
${pkgs.replace-secret}/bin/replace-secret '@HYPRSPACEPRIVATEKEY@' "${privateKeyFile}" ${runConfig}
2022-02-05 21:42:36 +02:00
chmod 0400 ${runConfig}
'';
2023-01-21 23:45:00 +02:00
postStart = ''
sleep 1
'' + lib.concatStringsSep "\n" (lib.mapAttrsToList (net: v:
"${pkgs.iproute2}/bin/ip route add ${net} via ${v.ip} dev hyprspace metric 30000"
) routes);
2022-11-13 03:12:44 +02:00
environment = lib.optionalAttrs config.services.kubo.enable {
2022-10-30 15:10:57 +02:00
HYPRSPACE_IPFS_API = config.services.kubo.settings.Addresses.API;
2022-11-13 03:12:44 +02:00
};
2022-02-05 21:42:36 +02:00
serviceConfig = {
2023-01-21 00:38:43 +02:00
Group = "wheel";
2022-06-18 00:53:36 +03:00
Restart = "on-failure";
RestartSec = "5s";
2022-02-05 21:42:36 +02:00
ExecStart = "${hyprspace}/bin/hyprspace up hyprspace -f -c ${runConfig}";
ExecStop = "${hyprspace}/bin/hyprspace down hyprspace -c ${runConfig}";
ExecReload = "${pkgs.coreutils}/bin/kill -USR1 $MAINPID";
2022-02-05 21:42:36 +02:00
};
};
networking.firewall = {
allowedTCPPorts = [ listenPort ];
allowedUDPPorts = [ listenPort ];
trustedInterfaces = [ "hyprspace" ];
};
networking.networkmanager.dispatcherScripts = [{
source = pkgs.writeShellScript "hyprspace-reconnect.sh" ''
[[ "$2" != "up" ]] && exit 0
PATH=${pkgs.systemd}/bin:$PATH
case $1 in
wl*|en*)
systemctl reload-or-restart --no-block hyprspace.service;;
2022-02-05 21:42:36 +02:00
esac
exit 0
'';
type = "basic";
}];
}