nixos-configuration/custom-utils/ports.nix
2025-04-16 22:54:29 +02:00

33 lines
864 B
Nix

{ lib, ... }:
let
filterPort = pm: port: (
lib.attrsets.catAttrs port (
lib.attrsets.attrValues (
lib.attrsets.filterAttrs (_: v: v ? "${port}") pm
)
)
);
onlyUniqueItemsInList = x: lib.lists.length x == lib.lists.length (lib.lists.unique x);
mkRange = { from, to }: (lib.lists.range from to);
in
portmap:
if builtins.all
(
proto:
if onlyUniqueItemsInList
(
lib.flatten (
map
(x:
if lib.isInt x then x
else if lib.isList x then x
else if lib.isAttrs x then mkRange x
else builtins.abort "found invalid entry in portmap"
)
(filterPort portmap proto)
)
) then true else builtins.abort "Found duplicate ${proto} ports."
) [ "tcp" "udp" ]
then portmap
else builtins.abort "Found duplicate ports."