perf: allow caching wireguard network information to significantly speed up evaluation
This commit is contained in:
parent
ba53e1a0be
commit
0de9c0751b
3 changed files with 27 additions and 8 deletions
|
@ -16,6 +16,7 @@ inputs: final: prev: let
|
||||||
nameValuePair
|
nameValuePair
|
||||||
partition
|
partition
|
||||||
removeSuffix
|
removeSuffix
|
||||||
|
warn
|
||||||
;
|
;
|
||||||
|
|
||||||
inherit
|
inherit
|
||||||
|
@ -32,8 +33,8 @@ inputs: final: prev: let
|
||||||
in {
|
in {
|
||||||
lib =
|
lib =
|
||||||
prev.lib
|
prev.lib
|
||||||
// {
|
// rec {
|
||||||
wireguard = userInputs: wgName: let
|
wireguard.evaluateNetwork = userInputs: wgName: let
|
||||||
inherit (userInputs.self) nodes;
|
inherit (userInputs.self) nodes;
|
||||||
# Returns the given node's wireguard configuration of this network
|
# Returns the given node's wireguard configuration of this network
|
||||||
wgCfgOf = node: nodes.${node}.config.wireguard.${wgName};
|
wgCfgOf = node: nodes.${node}.config.wireguard.${wgName};
|
||||||
|
@ -220,5 +221,24 @@ in {
|
||||||
wgQuickConfigScript
|
wgQuickConfigScript
|
||||||
;
|
;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
wireguard.createEvalCache = userInputs: wgNames:
|
||||||
|
genAttrs wgNames (wireguard.evaluateNetwork userInputs);
|
||||||
|
|
||||||
|
wireguard.getNetwork = userInputs: wgName:
|
||||||
|
userInputs.self.wireguardEvalCache.${wgName}
|
||||||
|
or (
|
||||||
|
warn ''
|
||||||
|
The calculated information for the wireguard network "${wgName}" is not cached!
|
||||||
|
This will siginificantly increase evaluation times. Please consider pre-evaluating
|
||||||
|
this information by exposing it in your flake:
|
||||||
|
|
||||||
|
wireguardEvalCache.${wgName} = lib.wireguard.createEvalCache inputs [
|
||||||
|
"${wgName}"
|
||||||
|
# all other networks
|
||||||
|
];
|
||||||
|
|
||||||
|
'' (wireguard.evaluateNetwork userInputs wgName)
|
||||||
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ in {
|
||||||
topology.networks = mkMerge (
|
topology.networks = mkMerge (
|
||||||
flip mapAttrsToList config.wireguard (
|
flip mapAttrsToList config.wireguard (
|
||||||
wgName: _: let
|
wgName: _: let
|
||||||
inherit (lib.wireguard inputs wgName) networkCidrs;
|
inherit (lib.wireguard.getNetwork inputs wgName) networkCidrs;
|
||||||
in {
|
in {
|
||||||
${networkId wgName} = {
|
${networkId wgName} = {
|
||||||
name = mkDefault "Wireguard network '${wgName}'";
|
name = mkDefault "Wireguard network '${wgName}'";
|
||||||
|
@ -47,7 +47,7 @@ in {
|
||||||
flip mapAttrsToList config.wireguard (
|
flip mapAttrsToList config.wireguard (
|
||||||
wgName: wgCfg: let
|
wgName: wgCfg: let
|
||||||
inherit
|
inherit
|
||||||
(lib.wireguard inputs wgName)
|
(lib.wireguard.getNetwork inputs wgName)
|
||||||
participatingServerNodes
|
participatingServerNodes
|
||||||
wgCfgOf
|
wgCfgOf
|
||||||
;
|
;
|
||||||
|
|
|
@ -28,7 +28,6 @@
|
||||||
optionals
|
optionals
|
||||||
stringLength
|
stringLength
|
||||||
types
|
types
|
||||||
wireguard
|
|
||||||
;
|
;
|
||||||
|
|
||||||
cfg = config.wireguard;
|
cfg = config.wireguard;
|
||||||
|
@ -36,7 +35,7 @@
|
||||||
|
|
||||||
configForNetwork = wgName: wgCfg: let
|
configForNetwork = wgName: wgCfg: let
|
||||||
inherit
|
inherit
|
||||||
(wireguard inputs wgName)
|
(lib.wireguard.getNetwork inputs wgName)
|
||||||
externalPeerName
|
externalPeerName
|
||||||
externalPeerNamesRaw
|
externalPeerNamesRaw
|
||||||
networkCidrs
|
networkCidrs
|
||||||
|
@ -385,7 +384,7 @@ in {
|
||||||
|
|
||||||
ipv4 = mkOption {
|
ipv4 = mkOption {
|
||||||
type = types.lazyOf types.net.ipv4;
|
type = types.lazyOf types.net.ipv4;
|
||||||
default = types.lazyValue (wireguard inputs name).assignedIpv4Addresses.${nodeName};
|
default = types.lazyValue (lib.wireguard.getNetwork inputs name).assignedIpv4Addresses.${nodeName};
|
||||||
description = ''
|
description = ''
|
||||||
The ipv4 address for this machine. If you do not set this explicitly,
|
The ipv4 address for this machine. If you do not set this explicitly,
|
||||||
a semi-stable ipv4 address will be derived automatically based on the
|
a semi-stable ipv4 address will be derived automatically based on the
|
||||||
|
@ -397,7 +396,7 @@ in {
|
||||||
|
|
||||||
ipv6 = mkOption {
|
ipv6 = mkOption {
|
||||||
type = types.lazyOf types.net.ipv6;
|
type = types.lazyOf types.net.ipv6;
|
||||||
default = types.lazyValue (wireguard inputs name).assignedIpv6Addresses.${nodeName};
|
default = types.lazyValue (lib.wireguard.getNetwork inputs name).assignedIpv6Addresses.${nodeName};
|
||||||
description = ''
|
description = ''
|
||||||
The ipv6 address for this machine. If you do not set this explicitly,
|
The ipv6 address for this machine. If you do not set this explicitly,
|
||||||
a semi-stable ipv6 address will be derived automatically based on the
|
a semi-stable ipv6 address will be derived automatically based on the
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue