Files
nixos/modules/networking/lib.nix
JakeHillion 0c6e4c0b56
commit-validation / validate-single-commit (pull_request) Successful in 8s
buildbot/nix-eval Build done. (671 warnings)
buildbot/nix-build Build done.
buildbot/nix-effects Build done.
networking: move the topology into a TOML registry
The home networks and their devices are defined as NixOS options in
topology.nix, in the same module as their schema, nested under a
location and mixed with settings that only the router uses. Devices are
keyed by the last octet of their address within each network, so a host
on two networks is two unrelated entries.

Added a network registry under /network: networks.toml holds each
network's VLAN, prefix, router and DHCP pool, and devices.toml holds one
record per device with its MAC and its address on each network it is
on. registry.nix gives them a schema and loads them into the read-only
custom.network options, rejecting an address outside its network's
prefix. The router module routes the networks that name the host as
their router, and takes the settings only it uses (trunk interface, WAN,
per-network DNS, NTP and trust, port forwards) as its own options, set
in cyclone's config. Address arithmetic moved to a small CIDR library.
topology.nix is removed, along with the per-device fqdn field and the
port forward lookup by FQDN, which nothing used, and the fc-ntp-mini
entry, which had no reservation.

Test plan:
- CI
- The router snapshot is unchanged
- Compared every host's toplevel derivation before and after: only
  cyclone's changed, and only in the order of its Kea reservations
2026-11-19 18:28:13 +00:00

38 lines
1.0 KiB
Nix

{ lib }:
# IPv4 arithmetic on dotted-quad addresses and CIDR prefixes.
let
pow2 = n: lib.lists.foldl (acc: _: acc * 2) 1 (lib.lists.range 1 n);
toInt = address:
lib.lists.foldl (acc: octet: acc * 256 + lib.strings.toInt octet) 0 (lib.strings.splitString "." address);
fromInt = n:
lib.strings.concatMapStringsSep "." (shift: toString (lib.trivial.mod (n / pow2 shift) 256)) [ 24 16 8 0 ];
parse = cidr:
let
parts = lib.strings.splitString "/" cidr;
prefixLength = lib.strings.toInt (lib.lists.elemAt parts 1);
size = pow2 (32 - prefixLength);
in
{
inherit prefixLength size;
base = (toInt (lib.lists.elemAt parts 0)) / size * size;
};
in
{
inherit toInt;
prefixLength = cidr: (parse cidr).prefixLength;
# The nth address of a prefix, counting the network address as 0.
host = cidr: n: fromInt ((parse cidr).base + n);
broadcast = cidr: let p = parse cidr; in fromInt (p.base + p.size - 1);
contains = cidr: address:
let p = parse cidr; in (toInt address) / p.size * p.size == p.base;
}