Personal Nix setup

Consolidate network configuration

+3 -22
modules/router/default.nix
···
-
{ lib, config, helpers, ... }:
+
{ lib, helpers, ... }:
-
with lib;
-
let
-
cfg = config.modules.router;
-
in {
+
with lib; {
options.modules.router = {
enable = mkOption {
default = false;
···
description = "Whether to enable Router options.";
type = types.bool;
};
-
-
interfaces = {
-
external = mkOption {
-
default = "extern0";
-
type = types.str;
-
};
-
internal = mkOption {
-
default = "intern0";
-
type = types.str;
-
};
-
};
};
config.modules.router = {
···
};
} // helpers.linuxAttrs {
imports = [
+
./network.nix
./timeserver.nix
./dnsOverTLS.nix
./dnsmasq.nix
···
./mdns.nix
./kernel.nix
];
-
-
config = mkIf cfg.enable {
-
networking.firewall.trustedInterfaces = [
-
cfg.interfaces.internal
-
];
-
};
}
+2 -2
modules/router/mdns.nix
···
config = mkIf cfg.mdns.enable {
services.avahi = {
enable = true;
-
allowInterfaces = [ cfg.interfaces.internal ];
-
denyInterfaces = [ cfg.interfaces.external ];
+
allowInterfaces = [ cfg.interfaces.internal.name ];
+
denyInterfaces = [ cfg.interfaces.external.name ];
};
};
}
+84
modules/router/network.nix
···
+
{ lib, config, ... }:
+
+
with lib;
+
let
+
cfg = config.modules.router;
+
+
interfaceType = types.submodule {
+
options = {
+
name = mkOption {
+
type = types.str;
+
example = "eth0";
+
};
+
macAddress = mkOption {
+
type = types.str;
+
example = "00:00:00:00:00:00";
+
};
+
};
+
};
+
+
extern0 = cfg.interfaces.external.name;
+
extern0MAC = cfg.interfaces.external.macAddress;
+
intern0 = cfg.interfaces.internal.name;
+
intern0MAC = cfg.interfaces.internal.macAddress;
+
in {
+
options.modules.router = {
+
interfaces = {
+
external = interfaceType;
+
internal = interfaceType;
+
};
+
};
+
+
config = mkIf cfg.enable {
+
services.irqbalance.enable = true;
+
+
networking.firewall.trustedInterfaces = [ "lo" intern0 ];
+
+
systemd.network = {
+
enable = true;
+
+
links."10-${extern0}" = {
+
matchConfig.PermanentMACAddress = extern0MAC;
+
linkConfig = {
+
Description = "External Network Interface";
+
Name = extern0;
+
# MACAddress = "64:20:9f:16:70:a6";
+
MTUBytes = "1500";
+
};
+
};
+
+
links."11-${intern0}" = {
+
matchConfig.PermanentMACAddress = intern0MAC;
+
linkConfig = {
+
Description = "Internal Network Interface";
+
Name = intern0;
+
MTUBytes = "1500";
+
};
+
};
+
+
networks."10-${extern0}" = {
+
name = extern0;
+
networkConfig = {
+
DHCP = "ipv4";
+
DNS = if cfg.dnsmasq.enable then "127.0.0.1" else "1.1.1.1";
+
IPForward = true;
+
};
+
dhcpV4Config = {
+
UseDNS = false;
+
UseDomains = false;
+
UseNTP = !cfg.timeserver.enable;
+
};
+
};
+
+
networks."11-${intern0}" = {
+
name = intern0;
+
networkConfig = {
+
Address = "10.0.0.1/24";
+
DHCPServer = false;
+
IPForward = true;
+
ConfigureWithoutCarrier = true;
+
};
+
};
+
};
+
};
+
}
+6 -3
modules/router/nftables.nix
···
let
cfg = config.modules.router;
+
intern0 = cfg.interfaces.internal.name;
+
extern0 = cfg.interfaces.external.name;
+
trustedInterfaces =
strings.concatMapStringsSep ", " strings.escapeNixIdentifier config.networking.firewall.trustedInterfaces;
···
blockForwardRules =
string.concatMapStringsSep "\n"
-
(builtins.map (mac: " iifname ${cfg.interfaces.internal} oifname != ${cfg.interfaces.internal} ether saddr = ${mac} drop"));
+
(builtins.map (mac: " iifname ${intern0} oifname != ${intern0} ether saddr = ${mac} drop"));
in {
options.modules.router = {
nftables = {
···
family = "netdev";
content = ''
chain lan {
-
type filter hook ingress device ${cfg.interfaces.internal} priority -150; policy accept;
+
type filter hook ingress device ${intern0} priority -150; policy accept;
jump tags
}
chain wan {
-
type filter hook ingress device ${cfg.interfaces.external} priority -149; policy accept;
+
type filter hook ingress device ${extern0} priority -149; policy accept;
jump tags
}
+6 -3
modules/router/timeserver.nix
···
let
cfg = config.modules.router;
+
listenInterfaces =
+
strings.concatMapStringsSep "\n"
+
(builtins.map (ifname: "interface listen ${ifname}") config.networking.firewall.trustedInterfaces);
+
ntpExtraConfig = ''
-
interface listen lo
-
interface listen ${cfg.interfaces.internal}
-
interface ignore ${cfg.interfaces.external}
+
${listenInterfaces}
+
interface ignore ${cfg.interfaces.external.name}
'';
in {
options.modules.router = {
+2 -2
modules/router/upnp.nix
···
enable = true;
upnp = true;
natpmp = true;
-
internalIPs = [ cfg.interfaces.internal ];
-
externalInterface = cfg.interfaces.external;
+
internalIPs = [ cfg.interfaces.internal.name ];
+
externalInterface = cfg.interfaces.external.name;
appendConfig = ''
secure_mode=yes
notify_interval=60