entities/lib.nix

158 lines
4.5 KiB
Nix
Raw Permalink Normal View History

2023-01-17 13:30:45 -08:00
{ lib, entities, ... }:
2023-01-26 13:41:09 -08:00
with lib;
2023-01-17 13:30:45 -08:00
let
getHostSite = hostname:
let site-name = entities.hosts."${hostname}".site;
in entities.sites."${site-name}";
getHostDomain = hostname:
let domain-name = entities.hosts."${hostname}".domain;
in entities.domains."${domain-name}";
2023-01-17 13:30:45 -08:00
getHostRealm = hostname: (getHostDomain hostname).gssapi-realm;
getHostFqdn = hostname:
let hostDomain = entities.hosts."${hostname}".domain;
in "${hostname}.${hostDomain}";
2023-01-17 13:30:45 -08:00
2023-01-26 13:36:10 -08:00
getHostNetworkSettings = hostname:
let
hostDomain = entities.hosts."${hostname}".domain;
hostNetwork = entities.zones."${hostDomain}";
in hostNetwork.hosts."${hostname}";
2023-01-26 13:47:43 -08:00
getIfAttr = as: a: if hasAttr as a then getAttr as a else null;
getHostIpv4 = hostname:
2023-01-26 14:03:26 -08:00
getIfAttr "ipv4-address" (getHostNetworkSettings hostname);
2023-01-26 13:47:43 -08:00
getHostIpv6 = hostname:
2023-01-26 14:03:26 -08:00
getIfAttr "ipv6-address" (getHostNetworkSettings hostname);
2023-01-26 13:42:59 -08:00
getHostIps = hostname:
2023-01-26 13:36:10 -08:00
filter (o: o != null) [ (getHostIpv4 hostname) (getHostIpv6 hostname) ];
getDomainPostgresqlServer = domain:
getHostFqdn entities.domains."${domain}".postgresql-server;
2023-03-01 10:13:31 -08:00
getSiteHosts = site:
attrNames (filterAttrs (_: hostOpts: hostOpts.site == site) entities.hosts);
getDomainHosts = domain:
attrNames
2023-11-03 11:40:46 -07:00
(filterAttrs (_: hostOpts: hostOpts.domain == domain) entities.hosts);
2023-11-13 18:09:11 -08:00
getSiteGatewayV4 = siteName:
let site = entities.sites."${siteName}";
2023-11-14 09:00:57 -08:00
in if hasAttr "local-gateway" site then
getHostIpv4 site.local-gateway
else
site.gateway-v4;
2023-11-13 18:09:11 -08:00
getHostGatewayV4 = hostname:
getSiteGatewayV4 entities.hosts."${hostname}".site;
getSiteGatewayV6 = siteName:
let site = entities.sites."${siteName}";
2023-11-14 09:00:57 -08:00
in if hasAttr "local-gateway" site then
getHostIpv6 site.local-gateway
else
site.gateway-v6;
getHostGatewayV6 = hostname:
getSiteGatewayV6 entities.hosts."${hostname}".site;
2023-11-13 18:09:11 -08:00
getSiteNetwork = siteName: entities.sites."${siteName}".network;
getSiteV4PrefixLength = siteName:
2023-11-17 11:12:20 -08:00
toInt (elemAt (splitString "/" (getSiteNetwork siteName)) 1);
getSiteV6PrefixLength = siteName:
abort "not implemented: getSiteV6PrefixLength";
2023-01-26 13:36:10 -08:00
in {
inherit getHostSite getHostDomain getHostRealm getHostFqdn getHostIpv4
2023-11-13 18:09:11 -08:00
getHostIpv6 getHostIps getDomainPostgresqlServer getSiteHosts getDomainHosts
getSiteGatewayV4 getHostGatewayV4 getSiteGatewayV6 getHostGatewayV6
getSiteV4PrefixLength getSiteV6PrefixLength;
2023-11-16 10:02:42 -08:00
2023-11-17 11:05:58 -08:00
fudo-types = with lib.types; {
networkHost.options = {
hostname = mkOption {
type = str;
description = "Hostname";
};
ipv4-address = mkOption {
type = nullOr str;
description = "The V4 IP of a given host, if any.";
default = null;
};
ipv6-address = mkOption {
type = nullOr str;
description = "The V6 IP of a given host, if any.";
default = null;
};
mac-address = mkOption {
type = nullOr str;
description =
"The MAC address of a given host, if desired for IP reservation.";
default = null;
};
description = mkOption {
type = nullOr str;
description = "Description of the host.";
default = null;
};
sshfp-records = mkOption {
type = listOf str;
description = "List of SSHFP records for this host.";
default = [ ];
};
};
networkHosts = let
# This is necessary because of the default 'name'...sigh.
networkHostOpt = { name, ... }: {
2023-11-17 11:05:58 -08:00
options = {
hostname = mkOption {
type = str;
description = "Hostname";
default = name;
};
ipv4-address = mkOption {
type = nullOr str;
description = "The V4 IP of a given host, if any.";
default = null;
};
ipv6-address = mkOption {
type = nullOr str;
description = "The V6 IP of a given host, if any.";
default = null;
};
mac-address = mkOption {
type = nullOr str;
description =
"The MAC address of a given host, if desired for IP reservation.";
default = null;
};
description = mkOption {
type = nullOr str;
description = "Description of the host.";
default = null;
};
sshfp-records = mkOption {
type = listOf str;
description = "List of SSHFP records for this host.";
default = [ ];
};
2023-11-16 16:33:14 -08:00
};
};
in attrsOf (submodule networkHostOpt);
2023-11-16 10:02:42 -08:00
};
2023-01-26 13:36:10 -08:00
}