diff --git a/modules/core/dns.nix b/modules/core/dns.nix index 3e4732f..c6d5367 100644 --- a/modules/core/dns.nix +++ b/modules/core/dns.nix @@ -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 diff --git a/modules/core/network.nix b/modules/core/network.nix index 71af459..9fac084 100644 --- a/modules/core/network.nix +++ b/modules/core/network.nix @@ -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); }; }