nixos-module/container/dhcp-server.nix: init

This commit is contained in:
Astro 2021-03-31 02:11:19 +02:00
parent c34e2e72c2
commit df3ef74862
4 changed files with 64 additions and 1 deletions

View File

@ -16,6 +16,11 @@ let
forceVeth = interface: interface // { forceVeth = interface: interface // {
type = "veth"; type = "veth";
}; };
netHasDHCP = net:
net == "pub" ||
net == "serv" ||
builtins.match "priv[[:digit:]]+" net != null;
in in
{ {
options.salt-pillar = lib.mkOption {}; options.salt-pillar = lib.mkOption {};
@ -25,9 +30,13 @@ in
(builtins.mapAttrs (_: vlan: { vlan = vlan; }) pillar.vlans) (builtins.mapAttrs (_: vlan: { vlan = vlan; }) pillar.vlans)
(builtins.mapAttrs (_: subnet4: { inherit subnet4; }) pillar.subnets-inet) (builtins.mapAttrs (_: subnet4: { inherit subnet4; }) pillar.subnets-inet)
(builtins.mapAttrs (_: hosts4: { inherit hosts4; }) pillar.hosts-inet) (builtins.mapAttrs (_: hosts4: { inherit hosts4; }) pillar.hosts-inet)
(builtins.mapAttrs (_: dhcpData: { (builtins.mapAttrs (net: dhcpData: {
dhcp = { dhcp = {
inherit (dhcpData) start end time max-time; inherit (dhcpData) start end time max-time;
server =
if netHasDHCP net
then "${net}-gw"
else null;
router = dhcpData.host-opts.routers; router = dhcpData.host-opts.routers;
domainName = dhcpData.string-opts.domain-name; domainName = dhcpData.string-opts.domain-name;
}; };

View File

@ -19,6 +19,10 @@ let
description = "Max renew time in seconds"; description = "Max renew time in seconds";
type = types.int; type = types.int;
}; };
server = mkOption {
description = "Container that runs the DHCP server";
type = types.str;
};
router = mkOption { router = mkOption {
description = "Gateway"; description = "Gateway";
type = types.str; type = types.str;
@ -39,6 +43,17 @@ let
type = with types; nullOr str; type = with types; nullOr str;
default = null; default = null;
}; };
subnet4Net = mkOption {
type = with types; nullOr types.str;
default =
let
inherit (config.site.net.${name}) subnet4;
s = lib.splitString "/" subnet4;
in
if subnet4 != null && builtins.length s == 2
then builtins.head s
else null;
};
subnet4Len = mkOption { subnet4Len = mkOption {
type = with types; nullOr types.int; type = with types; nullOr types.int;
default = default =

View File

@ -0,0 +1,38 @@
{ hostName, config, lib, ... }:
let
dhcpNets =
lib.filterAttrs (_: { dhcp, ... }:
dhcp != null &&
dhcp.server == hostName
) config.site.net;
enabled = builtins.length (builtins.attrNames dhcpNets) > 0;
in
{
services.dhcpd4 = lib.optionalAttrs enabled {
enable = true;
interfaces = builtins.attrNames dhcpNets;
extraConfig = ''
option domain-name-servers 172.20.73.8, 9.9.9.9;
${builtins.concatStringsSep "\n" (
builtins.attrValues (
builtins.mapAttrs (net: { dhcp, subnet4Net, subnet4Len, ...}:
''
subnet ${subnet4Net} netmask ${lib.netmasks.${toString subnet4Len}} {
range ${dhcp.start} ${dhcp.end};
default-lease-time ${toString dhcp.time};
max-lease-time ${toString dhcp.max-time};
option routers ${config.site.net.${net}.hosts4.${builtins.replaceStrings [".${net}"] [""] dhcp.router}};
option domain-name "${dhcp.domainName}";
}
''
) dhcpNets
)
)}
'';
# TODO: fixed-hosts
};
}

View File

@ -18,6 +18,7 @@ in {
] ]
++ optionals (hostConfig.role == "container") [ ++ optionals (hostConfig.role == "container") [
./container/defaults.nix ./container/defaults.nix
./container/dhcp-server.nix
] ++ optionals ( ] ++ optionals (
hostConfig.role == "container" && hostConfig.role == "container" &&
lib.config.site.hosts.${hostName}.isRouter lib.config.site.hosts.${hostName}.isRouter