config-perso/hive.nix

92 lines
2.1 KiB
Nix
Raw Normal View History

2024-05-14 19:21:19 +02:00
let
2024-06-01 00:28:17 +02:00
sources = import ./npins;
2024-12-15 16:19:19 +01:00
pkgs = import sources.nixpkgs-unstable { };
inherit (pkgs) lib;
inherit (lib)
mapAttrs
mapAttrs'
2025-01-07 11:27:06 +01:00
filterAttrs
mapAttrsToList
2024-12-15 16:19:19 +01:00
removeSuffix
evalModules
2025-01-07 11:27:06 +01:00
flatten
2024-12-15 16:19:19 +01:00
;
2024-12-15 16:19:19 +01:00
nodes = mapAttrs' (name: _: {
name = removeSuffix ".nix" name;
value = import ./machines/${name};
}) (builtins.readDir ./machines);
2024-12-15 16:19:19 +01:00
meta' = evalModules {
modules = [
./kat/meta.nix
{ machines = mapAttrs (_: n: n.meta) nodes; }
];
specialArgs = {
inherit pkgs sources;
};
};
meta = meta'.config // {
machines = mapAttrs (_: node: node.node_meta) meta'.config.machines;
};
2025-01-07 11:27:06 +01:00
meta-lib = self: rec {
self-meta = meta.machines.${self};
other-meta = filterAttrs (name: _: name != self) meta.machines;
mkPeers =
let
hubs = filterAttrs (_: node: node.vpn-hub) other-meta;
non-hubs = filterAttrs (_: node: node.wg-key != null && !node.vpn-hub) other-meta;
all-subnets =
[ "10.42.0.0/16" ] ++ flatten
(map (mapAttrsToList (_: node: node.subnets)) [
hubs
non-hubs
]);
in
if self-meta.vpn-hub then
mapAttrsToList (_: node: {
AllowedIPs = [
"${node.vpn-ip4}/32"
] ++ node.subnets;
PublicKey = node.wg-key;
}) non-hubs
else
mapAttrsToList (_: node: {
AllowedIPs = all-subnets;
PublicKey = node.wg-key;
Endpoint = "${node.fqdn}:1194";
PersistentKeepalive = 25;
}) hubs;
};
2024-05-14 19:21:19 +02:00
in
{
2024-12-15 16:19:19 +01:00
meta = {
nixpkgs = pkgs.path;
specialArgs = {
2025-01-07 11:27:06 +01:00
inherit sources;
mods = import ./modules;
users = import ./users;
2024-12-15 16:19:19 +01:00
kat-path = ./kat;
};
2024-12-15 16:19:19 +01:00
nodeNixpkgs = mapAttrs (_: node: node.nixpkgs-paths.nixpkgs-src) meta.machines;
2025-01-07 11:27:06 +01:00
nodeSpecialArgs = mapAttrs (name: node: {
self-meta = node;
meta = meta // {
lib = meta-lib name;
};
}) meta.machines;
2024-03-13 11:11:16 +01:00
};
2024-05-14 19:21:19 +02:00
defaults =
2024-06-01 16:26:08 +02:00
{
name,
2024-12-15 16:19:19 +01:00
kat-path,
2024-06-01 16:26:08 +02:00
...
}:
2024-05-14 19:21:19 +02:00
{
2024-12-15 16:19:19 +01:00
imports = [ kat-path ];
2024-09-26 11:51:04 +02:00
networking.hostName = name;
2024-05-14 19:21:19 +02:00
};
2024-03-12 17:26:16 +01:00
}
2024-12-15 16:19:19 +01:00
// mapAttrs (_: n: n.config) nodes