Files
nixos-servers/docs/plans/completed/dns-automation.md
Torjus Håkestad d7d4b0846c
All checks were successful
Run nix flake check / flake-check (push) Successful in 2m17s
docs: move dns-automation plan to completed
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-04 22:13:38 +01:00

2.3 KiB

DNS Automation

Status: Completed (2026-02-04)

Goal: Automatically generate DNS entries from host configurations

Approach: Leverage Nix to generate zone file entries from flake host configurations

Since most hosts use static IPs defined in their NixOS configurations, we can extract this information and automatically generate A records. This keeps DNS in sync with the actual host configs.

Implementation

  • Add optional CNAME field to host configurations
    • Added homelab.dns.cnames option in modules/homelab/dns.nix
    • Added homelab.dns.enable to allow opting out (defaults to true)
    • Documented in CLAUDE.md
  • Create Nix function to extract DNS records from all hosts
    • Created lib/dns-zone.nix with extraction functions
    • Parses each host's networking.hostName and systemd.network.networks IP configuration
    • Collects CNAMEs from homelab.dns.cnames
    • Filters out VPN interfaces (wg*, tun*, tap*, vti*)
    • Generates complete zone file with A and CNAME records
  • Integrate auto-generated records into zone files
    • External hosts separated to services/ns/external-hosts.nix
    • Zone includes comments showing which records are auto-generated vs external
  • Update zone file serial number automatically
    • Uses self.sourceInfo.lastModified (git commit timestamp)
  • Test zone file validity after generation
    • NSD validates zone at build time via nsd-checkzone
  • Deploy process documented
    • Merge to master, run auto-upgrade on ns1/ns2

Files Created/Modified

File Purpose
modules/homelab/dns.nix Defines homelab.dns.* options
modules/homelab/default.nix Module import hub
lib/dns-zone.nix Zone generation functions
services/ns/external-hosts.nix Non-flake host records
services/ns/master-authorative.nix Uses generated zone
services/ns/secondary-authorative.nix Uses generated zone

Usage

View generated zone:

nix eval .#nixosConfigurations.ns1.config.services.nsd.zones.'"home.2rjus.net"'.data --raw

Add CNAMEs to a host:

homelab.dns.cnames = [ "alias1" "alias2" ];

Exclude a host from DNS:

homelab.dns.enable = false;

Add non-flake hosts: Edit services/ns/external-hosts.nix