network/nix/nixos-module/container/upstream.nix

25 lines
669 B
Nix

{ hostName, config, lib, ... }:
let
upstreamInterfaces =
lib.filterAttrs (_: { upstream, ... }: upstream != null)
config.site.hosts.${hostName}.interfaces;
firstUpstreamInterface =
if builtins.length (builtins.attrNames upstreamInterfaces) > 0
then builtins.head (
builtins.attrNames upstreamInterfaces
)
else null;
in
{
systemd.network.networks = builtins.mapAttrs (_: _: {
DHCP = "yes";
}) upstreamInterfaces;
networking.nat = lib.optionalAttrs (firstUpstreamInterface != null) {
enable = true;
externalInterface = firstUpstreamInterface;
forwardPorts = config.site.hosts.${hostName}.forwardedPorts;
};
}