Cleanup the configurer by moving helpers into lib

release
Matthew Salerno 1 year ago
parent f45752f5d7
commit 18b08d06ec

@ -10,14 +10,8 @@ let
# these aren't really important, I just wanted to reverse the argument order # these aren't really important, I just wanted to reverse the argument order
forEachAttr' = flip mapAttrs'; forEachAttr' = flip mapAttrs';
forEachAttrToList = flip mapAttrsToList; forEachAttrToList = flip mapAttrsToList;
keyProvidersInit = map (x: x inputs intermediateConfig peerName) keyProviders;
getPeerPubKey = otherPeerName: findFirst (x: x != null) (throw "Wirenix: Could not find public key for " + otherPeerName)
(map (provider: provider.getPeerPubKey otherPeerName) keyProvidersInit);
getPrivKeyFile = findFirst (x: x != null) (throw "Wirenix: Could not find private key file for " + peerName)
(map (provider: provider.getPrivKeyFile) keyProvidersInit);
getSubnetPSKFile = subnetName: findFirst (x: x != null) (null)
(map (provider: provider.getSubnetPSKFile subnetName) keyProvidersInit);
in in
with getKeyProviderFuncs keyProviders inputs intermediateConfig peerName;
{ {
networking.wireguard = { networking.wireguard = {
interfaces = forEachAttr' thisPeer.subnetConnections (subnetName: subnetConnection: { name = "wn-${subnetName}"; interfaces = forEachAttr' thisPeer.subnetConnections (subnetName: subnetConnection: { name = "wn-${subnetName}";

@ -2,6 +2,17 @@ with builtins;
/** /**
ACL independent functions that can be used in parsers. ACL independent functions that can be used in parsers.
*/ */
let
# stubbornly not passing lib and reimplementing everything since 2023
findFirst = pred: default: list:
if length list == 0
then default
else
if pred (head list)
then head list
else findFirst pred default (tail list);
in
rec { rec {
/** Builtin Parsers */ /** Builtin Parsers */
defaultParsers = { defaultParsers = {
@ -98,5 +109,18 @@ rec {
name: value: (lib.attrsets.attrByPath ["config" "modules" "wirenix" "peerName"] null value) == peerName name: value: (lib.attrsets.attrByPath ["config" "modules" "wirenix" "peerName"] null value) == peerName
) nixosConfigurations)); ) nixosConfigurations));
getKeyProviderFuncs = keyProvidersUninitialized: inputs: intermediateConfig: peerName:
let
keyProviders = map (x: x inputs intermediateConfig peerName) keyProvidersUninitialized;
in
{
getPeerPubKey = otherPeerName: findFirst (x: x != null) (throw ("Wirenix: Could not find public key for " + otherPeerName))
(map (provider: provider.getPeerPubKey otherPeerName) keyProviders);
getPrivKeyFile = findFirst (x: x != null) (throw ("Wirenix: Could not find private key file for " + peerName))
(map (provider: provider.getPrivKeyFile) keyProviders);
getSubnetPSKFile = subnetName: findFirst (x: x != null) (null)
(map (provider: provider.getSubnetPSKFile subnetName) keyProviders);
};
mergeIf = attr: key: if builtins.hasAttr key attr then {"${key}" = attr."${key}";} else {}; mergeIf = attr: key: if builtins.hasAttr key attr then {"${key}" = attr."${key}";} else {};
} }
Loading…
Cancel
Save