refactor(dns/network): replace dnsResolvableName with hostname and restructure DNS record generation

- Replaced `dnsResolvableName` with `hostname` in device configuration options
- Updated DNS record generation logic to use `hostname` instead of domain-based naming
- Removed deprecated `dnsResolvableName` option from network module
- Restructured DNS record templates to use consistent formatting
- Simplified code structure by removing redundant whitespace and reorganizing attribute definitions
- Updated `generateDeviceHostRecords` to use new naming convention and improved template syntax
This commit is contained in:
Giulio De Pasquale 2025-06-05 16:53:35 +01:00
parent 0e513e1c69
commit 184f039e40
2 changed files with 42 additions and 46 deletions

View File

@ -122,29 +122,31 @@ in
generateDeviceHostRecords =
let
generateRecordsForInterface = ifaceName: ifaceConfig:
lib.concatStringsSep "\n" (lib.mapAttrsToList (deviceName: deviceConfig:
if deviceConfig.dnsResolvableName != null then
let
serverIP = deviceConfig.address;
interfaceNet = ifaceConfig.net;
domain = deviceConfig.dnsResolvableName;
in
''
${domain} {
view ${ifaceName} {
expr incidr(client_ip(), '${interfaceNet}')
}
lib.concatStringsSep "\n" (lib.mapAttrsToList
(deviceName: deviceConfig:
if deviceConfig.hostname!= null then
let
deviceAddress = deviceConfig.address;
deviceIface = ifaceConfig.net;
deviceHost = deviceConfig.hostname;
in
''
${deviceHost} {
view ${ifaceName} {
expr incidr(client_ip(), '${deviceIface}')
}
template IN A ${domain} {
answer "${domain}. 60 IN A ${serverIP}"
}
template IN A ${deviceHost} {
answer "${deviceHost}. 60 IN A ${deviceAddress}"
}
cache
log
}
''
else ""
) ifaceConfig.devices);
cache
log
}
''
else ""
)
ifaceConfig.devices);
in
lib.concatStringsSep "\n" (lib.mapAttrsToList generateRecordsForInterface config.pepe.core.network.interfaces);
in

View File

@ -36,18 +36,12 @@ in
type = types.str;
description = "The hostname of the device.";
};
isEndpoint = mkOption {
type = types.bool;
default = false;
description = "Whether this device serves as a DNS endpoint for this interface.";
};
dnsResolvableName = mkOption {
type = types.nullOr types.str;
default = null;
description = "The fully qualified domain name that should resolve to this device's IP address. e.g., my-device.lan.example.com";
};
};
});
default = { };
@ -58,14 +52,14 @@ in
default = { };
description = "An attribute set of networks with their configurations.";
};
interfacesByType = mkOption {
type = types.attrsOf (types.listOf types.str);
default = {};
default = { };
description = "Interfaces grouped by type (lan, wan, vpn) for easy access.";
internal = true;
};
dnsEndpoints = mkOption {
type = types.attrsOf (types.submodule {
options = {
@ -73,17 +67,17 @@ in
type = types.str;
description = "The interface this DNS endpoint belongs to.";
};
device = mkOption {
type = types.str;
description = "The device name that serves as the DNS endpoint.";
};
address = mkOption {
type = types.str;
description = "The IP address of the DNS endpoint.";
};
serverName = mkOption {
type = types.str;
default = "";
@ -91,12 +85,12 @@ in
};
};
});
default = {};
default = { };
description = "DNS endpoints for each interface.";
internal = true;
};
};
config = {
# Create lists of interfaces by type for easy access elsewhere
pepe.core.network.interfacesByType = {
@ -104,15 +98,15 @@ in
wan = lib.attrNames (lib.filterAttrs (_: iface: iface.type == "wan") cfg.interfaces);
vpn = lib.attrNames (lib.filterAttrs (_: iface: iface.type == "vpn") cfg.interfaces);
};
# Collect DNS endpoints from all interfaces
pepe.core.network.dnsEndpoints =
pepe.core.network.dnsEndpoints =
let
collectEndpoints = ifaceName: iface:
lib.mapAttrs'
(deviceName: device:
lib.nameValuePair
"${ifaceName}-${deviceName}"
lib.mapAttrs'
(deviceName: device:
lib.nameValuePair
"${ifaceName}-${deviceName}"
{
interface = ifaceName;
device = deviceName;
@ -122,11 +116,11 @@ in
)
(lib.filterAttrs (_: device: device.isDnsEndpoint) iface.devices);
in
lib.foldl
(acc: ifaceName:
lib.foldl
(acc: ifaceName:
acc // (collectEndpoints ifaceName cfg.interfaces.${ifaceName})
)
{}
)
{ }
(lib.attrNames cfg.interfaces);
};
}