nixos-config/machines/shinobu/services/router/nft.nix
Simon Bruder b55cc2deaf
shinobu/router: Allow adding static hosts
This is required to have them available in nftables rules without too
much headache.
2024-09-22 11:27:52 +02:00

43 lines
1.1 KiB
Nix

# SPDX-FileCopyrightText: 2023-2024 Simon Bruder <simon@sbruder.de>
#
# SPDX-License-Identifier: AGPL-3.0-or-later
{ config, lib, pkgs, ... }:
let
cfg = pkgs.callPackage ./common.nix { };
mkKeyValue = lib.generators.mkKeyValueDefault
rec {
mkValueString = v:
if lib.isString v then ''"${v}"''
else if lib.isList v then ''{ ${lib.concatMapStringsSep ", " mkValueString v} }''
else lib.generators.mkValueStringDefault { } v;
} " = ";
passthru = {
VLANS = lib.attrNames cfg.vlan;
VLAN_BRIDGES = map (name: "br-${name}") (lib.attrNames cfg.vlan);
} // (lib.listToAttrs (lib.flatten (lib.mapAttrsToList
(name: staticHostConfig:
(map
(option: option // { name = "STATIC_HOST_${name}_${option.name}"; })
(lib.attrsToList staticHostConfig)))
cfg.staticHosts)));
defines = lib.concatStringsSep
"\n"
(lib.mapAttrsToList
(key: value: "define " + (mkKeyValue key value))
passthru);
in
{
networking.nftables = {
enable = true;
ruleset = ''
${defines}
include "${./rules.nft}"
'';
};
}