nix-config/modules/TLMS/net.nix

72 lines
2.2 KiB
Nix
Raw Normal View History

2022-10-02 22:14:34 +02:00
{ lib, config, utils, ... }:
let
2022-12-30 18:29:13 +01:00
cfg = config.deployment-TLMS.net;
in
{
2022-12-30 18:29:13 +01:00
options.deployment-TLMS.net = with lib; {
iface.uplink = {
name = mkOption {
2022-10-02 21:39:37 +02:00
type = types.nullOr types.str;
default = null;
};
2022-10-03 02:20:48 +02:00
mac = mkOption {
type = types.nullOr types.str;
default = null;
};
matchOn = mkOption {
type = types.enum [ "name" "mac" ];
default = "name";
};
useDHCP = mkOption {
type = types.bool;
default = true;
};
addr4 = mkOption {
2022-10-02 21:39:37 +02:00
type = types.nullOr types.str;
default = null;
description = "address with prefix in CIDR notation";
};
routes =
with utils.systemdUtils.unitOptions;
with utils.systemdUtils.lib;
with lib;
mkOption {
2022-10-02 21:39:37 +02:00
#type = with types; listOf (submodule routeOptions);
type = types.listOf (types.attrsOf unitOption);
default = [ ];
description = "default gateway";
};
dns = mkOption {
type = types.listOf types.str;
default = [ "9.9.9.9" "1.1.1.1" "8.8.8.8" ];
};
};
};
config = let
2022-10-03 02:20:48 +02:00
match = if cfg.iface.uplink.matchOn == "name" then { Name = cfg.iface.uplink.name; } else { MACAddress = cfg.iface.uplink.mac; };
upname = "30-${cfg.iface.uplink.name}";
2022-10-02 22:40:29 +02:00
upconf = if cfg.iface.uplink.useDHCP then {
2022-10-03 02:20:48 +02:00
matchConfig = match;
networkConfig = {
2022-10-02 22:40:29 +02:00
DHCP = "yes";
};
} else {
2022-10-03 02:20:48 +02:00
matchConfig = match;
networkConfig = {
2022-10-02 22:40:29 +02:00
DHCP = "no";
Address = cfg.iface.uplink.addr4;
DNS = cfg.iface.uplink.dns;
};
2022-10-02 22:40:29 +02:00
routes = cfg.iface.uplink.routes;
};
in
{
2022-10-02 21:39:37 +02:00
systemd.network.networks."${upname}" = upconf;
2022-10-03 00:10:30 +02:00
networking.interfaces.${cfg.iface.uplink.name}.useDHCP = if !cfg.iface.uplink.useDHCP then (lib.mkForce false) else (lib.mkDefault true);
networking.useDHCP = if !cfg.iface.uplink.useDHCP then (lib.mkForce false) else (lib.mkDefault true);
};
}