dns + keybinding consolidation

This commit is contained in:
ari committed 2026-09-21 10:52:32 -04:00
1 parent df46627173
commit f8444b1549
23 files changed
+612 -109

No files matched your search

@@ -23,6 +23,28 @@ let
# When composeFile is set, networks/volumes/extraConfig declared alongside it
# are rendered as a base overrides file merged in via an earlier -f flag, so
# the composeFile's own definitions win on any overlapping keys.
# ---- internal DNS (skyg.dns) ----------------------------------------------
# Every service with dns.names set contributes a record. The address is taken
# from dns.ip when given, otherwise derived from the service's `networks`
# mapping (the compose form `{ lan.ipv4_address = "10.0.101.2"; }`).
serviceStaticIps = svcCfg:
if builtins.isAttrs svcCfg.networks
then lib.filter (v: v != null) (lib.mapAttrsToList (_: n: n.ipv4_address or null) svcCfg.networks)
else [ ];
dnsServices = lib.flatten (lib.mapAttrsToList
(groupName: grpCfg: lib.mapAttrsToList
(svcName: svcCfg: { inherit groupName svcName svcCfg; })
(lib.filterAttrs (_: svcCfg: svcCfg.dns.names != [ ]) grpCfg.services))
enabledGroups);
dnsServiceIp = s:
if s.svcCfg.dns.ip != null
then s.svcCfg.dns.ip
else
let ips = serviceStaticIps s.svcCfg;
in if lib.length ips == 1 then lib.head ips else null;
mkOverridesFileFor = groupName: grpCfg:
if grpCfg.composeFile != null
&& (grpCfg.networks != { } || grpCfg.volumes != { } || grpCfg.extraConfig != { })
@@ -53,7 +75,28 @@ in
message =
"skyg.nixos.common.container-services.${name}: set either composeFile OR services, not both.";
})
enabledGroups;
enabledGroups
++ map
(s: {
assertion = dnsServiceIp s != null;
message = ''
skyg.nixos.common.container-services.${s.groupName}.services.${s.svcName}:
dns.names is set but no address could be determined. Found
${toString (lib.length (serviceStaticIps s.svcCfg))} ipv4_address
entries in `networks` (exactly one is required). Set dns.ip
explicitly.
'';
})
dnsServices;
# Internal DNS records for services that asked for a name.
skyg.dns.records = map
(s: {
ip = dnsServiceIp s;
inherit (s.svcCfg.dns) names wildcard;
source = "container-services/${s.groupName}/${s.svcName}";
})
(lib.filter (s: dnsServiceIp s != null) dnsServices);
# Create tmpfiles rules for state dirs and file dirs
systemd.tmpfiles.rules =
@@ -166,6 +166,51 @@
'';
};
dns = lib.mkOption {
default = { };
description = ''
Internal DNS names for this service. Folded into
skyg.dns.records, aggregated across hosts at the flake level,
and pushed to the router by `skyg openwrt`. Nothing is
configured on the host itself.
'';
type = lib.types.submodule {
options = {
names = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
example = [ "drawdb" ];
description = ''
DNS names for this service. Bare labels (no dot) are
suffixed with skyg.dns.domain, so [ "drawdb" ] becomes
drawdb.app.internal.
'';
};
ip = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = ''
Address the names resolve to. When null (the default)
it is derived from this service's `networks` block --
which requires exactly one ipv4_address to be present.
Set explicitly for host-networked services or when the
service is attached to several networks.
'';
};
wildcard = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Also resolve every subdomain of these names
(dnsmasq `address=/name/ip` instead of `host-record=`).
'';
};
};
};
};
extra_hosts = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
@@ -215,6 +215,45 @@ group starts; groups only reference it (`external: true`) and never create it.
---
## Internal DNS names
A service with a static IP can declare its own internal DNS name. The record is folded into
`skyg.dns.records`, aggregated across every host at the flake level, and pushed to the
OpenWrt router's `dnsmasq.conf` by `skyg openwrt`. **Nothing is configured on the host.**
```nix
skyg.nixos.common.container-services.drawdb.services.drawdb = {
image = "ghcr.io/drawdb-io/drawdb:latest";
networks = { lan = { ipv4_address = "10.0.101.2"; }; };
dns.names = [ "drawdb" ]; # -> drawdb.app.internal
};
```
| Option | Default | Notes |
|---|---|---|
| `dns.names` | `[ ]` | Bare labels get `skyg.dns.domain` (default `app.internal`) appended; names containing a `.` are used as-is. |
| `dns.ip` | `null` | Derived from the service's `networks` block when it contains exactly one `ipv4_address`. Required for host-networked or multi-network services. |
| `dns.wildcard` | `false` | `true` also resolves every subdomain (`address=/name/ip` instead of `host-record=`). |
Setting `dns.names` without a derivable address is an **eval error** that names the offending
service. Two hosts mapping the same name to different IPs is also an eval error.
For IPs that aren't container services, use the host-level escape hatch instead:
```nix
skyg.dns.extraRecords.nas = { ip = "10.0.0.50"; names = [ "nas" "files" ]; };
```
Verify and deploy:
```sh
nix eval .#dnsRecords --json | jq # every record, all hosts
skyg openwrt --dry-run # render dnsmasq.conf locally
skyg openwrt # diff + confirm + apply
```
---
## Limitations
- **One unit per group.** No per-container systemd units. Use `docker compose logs` for
+1
View File
@@ -3,6 +3,7 @@
imports = [
./hardware
./containers
./dns-records
./core.nix
./networking.nix
./user.nix
@@ -0,0 +1,65 @@
# modules/nixos/common/dns-records/
Host-level registry of **internal DNS records**. This module configures *nothing* on the
host — it is a pure data collection point. Records declared here are aggregated across all
hosts at the flake level (`nix eval .#dnsRecords`) and pushed to the OpenWrt router's
`dnsmasq.conf` by `skyg openwrt` (see `modules/openwrt/`).
## Why
Service IPs live in host configs (e.g. a container's static macvlan address in
`hosts/hl-minipc2.nix`), but the router config lives in an encrypted JSON secret. Without a
bridge, naming a service means writing its IP in two places that silently drift. This module
is the single canonical shape everything writes to:
```nix
{ ip = "10.0.101.2"; names = [ "drawdb.app.internal" ]; wildcard = false; source = "..."; }
```
## Options
| Option | Purpose |
|---|---|
| `skyg.dns.domain` | Default suffix for bare names. Default `app.internal`. |
| `skyg.dns.extraRecords` | Hand-written records, keyed by name so they can be overridden. **Use this for arbitrary IPs.** |
| `skyg.dns.records` | List form. Modules append here (container-services does). |
| `skyg.dns.resolved` | Read-only: the above, names fully qualified + `host` attached. What the aggregator reads. |
| `skyg.dns.writeDebugFile` | Dump `resolved` to `/etc/skyg/dns-records.json`. Default true. |
Names without a `.` get `skyg.dns.domain` appended: `"drawdb"` → `"drawdb.app.internal"`.
Names containing a `.` are treated as fully-qualified and passed through untouched.
`wildcard = false` (the default) emits dnsmasq `host-record=name,ip` — a plain A record with
reverse PTR. `wildcard = true` emits `address=/name/ip`, which also catches every subdomain.
## Examples
Arbitrary IP, no container involved:
```nix
skyg.dns.extraRecords.nas = {
ip = "10.0.0.50";
names = [ "nas" "files" ]; # -> nas.app.internal, files.app.internal
};
```
From a container service (see `containers/container-services/`, which folds into `records`):
```nix
skyg.nixos.common.container-services.drawdb.services.drawdb = {
networks = { lan.ipv4_address = "10.0.101.2"; };
dns.names = [ "drawdb" ];
};
```
## Deploying
Records are inert until pushed to the router:
```sh
nix eval .#dnsRecords --json | jq # inspect the aggregate across all hosts
skyg openwrt --dry-run # render dnsmasq.conf locally
skyg openwrt # diff + confirm + apply on the router
```
Conflicts (the same name mapped to two different IPs, on any host) fail flake evaluation.
@@ -0,0 +1,132 @@
{ config, lib, ... }:
let
cfg = config.skyg.dns;
# Bare labels get the default domain appended; anything already containing a
# dot is treated as fully-qualified and left alone.
qualify = name:
if lib.hasInfix "." name || cfg.domain == null || cfg.domain == ""
then name
else "${name}.${cfg.domain}";
recordType = lib.types.submodule ({ ... }: {
options = {
ip = lib.mkOption {
type = lib.types.str;
example = "10.0.101.2";
description = "IPv4 address the names should resolve to.";
};
names = lib.mkOption {
type = lib.types.listOf lib.types.str;
example = [ "drawdb" "drawdb.app.internal" ];
description = ''
DNS names for this address. Bare labels (no dot) are automatically
suffixed with skyg.dns.domain.
'';
};
wildcard = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
When true, the record also matches every subdomain
(dnsmasq `address=/name/ip`). When false, a plain host record is
emitted (dnsmasq `host-record=name,ip`), which also provides reverse
PTR lookups.
'';
};
source = lib.mkOption {
type = lib.types.str;
default = "manual";
description = "Free-form provenance marker, used in generated comments.";
};
};
});
# Fully-qualified, host-tagged view of every record on this machine.
resolved = map
(r: {
inherit (r) ip wildcard source;
names = map qualify r.names;
host = config.networking.hostName;
})
(cfg.records ++ (lib.attrValues cfg.extraRecords));
in
{
options.skyg.dns = {
enable = lib.mkOption {
type = lib.types.bool;
default = true;
description = ''
Collect internal DNS records declared on this host. Records are pure
data -- they are aggregated at the flake level and pushed to the router
by `skyg openwrt`; nothing is configured on the host itself beyond a
debug dump in /etc/skyg/dns-records.json.
'';
};
domain = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = "app.internal";
example = "lab.internal";
description = "Default suffix appended to bare (dot-less) record names.";
};
records = lib.mkOption {
type = lib.types.listOf recordType;
default = [ ];
description = ''
Internal DNS records contributed by this host. Modules append to this
list (see container-services' `dns.names`); prefer `extraRecords` for
hand-written entries so they can be overridden by name.
'';
};
extraRecords = lib.mkOption {
type = lib.types.attrsOf recordType;
default = { };
example = lib.literalExpression ''
{
nas = { ip = "10.0.0.50"; names = [ "nas" "files" ]; };
}
'';
description = ''
Hand-written DNS records, keyed by an arbitrary name so they can be
overridden or removed from another module. Merged into `records`.
'';
};
resolved = lib.mkOption {
type = lib.types.listOf (lib.types.attrsOf lib.types.anything);
readOnly = true;
internal = true;
description = ''
Read-only: `records` + `extraRecords` with names fully qualified and the
owning hostname attached. This is what the flake-level aggregator reads.
'';
};
writeDebugFile = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Write the resolved records to /etc/skyg/dns-records.json.";
};
};
config = lib.mkIf cfg.enable {
skyg.dns.resolved = resolved;
assertions = lib.flip map resolved (r: {
assertion = r.names != [ ];
message = "skyg.dns: record for ${r.ip} (source: ${r.source}) has no names.";
});
environment.etc."skyg/dns-records.json" = lib.mkIf (cfg.writeDebugFile && resolved != [ ]) {
text = builtins.toJSON resolved + "\n";
};
};
}