entities/lib.nix

59 lines
2.0 KiB
Nix

{ lib, entities, ... }:
with lib;
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}";
getHostRealm = hostname: (getHostDomain hostname).gssapi-realm;
getHostFqdn = hostname:
let hostDomain = entities.hosts."${hostname}".domain;
in "${hostname}.${hostDomain}";
getHostNetworkSettings = hostname:
let
hostDomain = entities.hosts."${hostname}".domain;
hostNetwork = entities.zones."${hostDomain}";
in hostNetwork.hosts."${hostname}";
getIfAttr = as: a: if hasAttr as a then getAttr as a else null;
getHostIpv4 = hostname:
getIfAttr "ipv4-address" (getHostNetworkSettings hostname);
getHostIpv6 = hostname:
getIfAttr "ipv6-address" (getHostNetworkSettings hostname);
getHostIps = hostname:
filter (o: o != null) [ (getHostIpv4 hostname) (getHostIpv6 hostname) ];
getDomainPostgresqlServer = domain:
getHostFqdn entities.domains."${domain}".postgresql-server;
getSiteHosts = site:
attrNames (filterAttrs (_: hostOpts: hostOpts.site == site) entities.hosts);
getDomainHosts = domain:
attrNames
(filterAttrs (_: hostOpts: hostOpts.domain == domain) entities.hosts);
getSiteGatewayV4 = siteName:
let site = config.fudo.sites."${siteName}";
in if hasAttr "local-gateway" site then getHostIpv4 else site.gateway-v4;
getHostGatewayV4 = hostname:
getSiteGatewayV4 config.fudo.hosts."${hostname}".site;
getSiteGatewayV6 = siteName:
let site = config.fudo.sites."${siteName}";
in if hasAttr "local-gateway" site then getHostIpv6 else site.gateway-v6;
getHostGatewayV6 = hostname:
getSiteGatewayV6 config.fudo.hosts."${hostname}".site;
in {
inherit getHostSite getHostDomain getHostRealm getHostFqdn getHostIpv4
getHostIpv6 getHostIps getDomainPostgresqlServer getSiteHosts getDomainHosts
getSiteGatewayV4 getHostGatewayV4 getSiteGatewayV6 getHostGatewayV6;
}