nixos/dhcpcd: Respect per interface dhcp options

Changed files
+16 -3
nixos
modules
services
networking
+16 -3
nixos/modules/services/networking/dhcpcd.nix
···
cfg = config.networking.dhcpcd;
+
interfaces = attrValues config.networking.interfaces;
+
+
enableDHCP = config.networking.useDHCP || any (i: i.useDHCP == true) interfaces;
+
# Don't start dhcpcd on explicitly configured interfaces or on
# interfaces that are part of a bridge, bond or sit device.
ignoredInterfaces =
-
map (i: i.name) (filter (i: if i.useDHCP != null then i.useDHCP else i.ip4 != [ ] || i.ipAddress != null) (attrValues config.networking.interfaces))
+
map (i: i.name) (filter (i: if i.useDHCP != null then i.useDHCP else i.ip4 != [ ] || i.ipAddress != null) interfaces)
++ mapAttrsToList (i: _: i) config.networking.sits
++ concatLists (attrValues (mapAttrs (n: v: v.interfaces) config.networking.bridges))
++ concatLists (attrValues (mapAttrs (n: v: v.interfaces) config.networking.bonds))
++ config.networking.dhcpcd.denyInterfaces;
+
+
arrayAppendOrNull = a1: a2: if a1 == null && a2 == null then null
+
else if a1 == null then a2 else if a2 == null then a1
+
else a1 ++ a2;
+
+
# If dhcp is disabled but explicit interfaces are enabled,
+
# we need to provide dhcp just for those interfaces.
+
allowInterfaces = arrayAppendOrNull cfg.allowInterfaces
+
(if !cfg.useDHCP && enableDHCP then map (i: i.name) (filter (i: i.useDHCP == true) interfaces) else null);
# Config file adapted from the one that ships with dhcpcd.
dhcpcdConf = pkgs.writeText "dhcpcd.conf"
···
denyinterfaces ${toString ignoredInterfaces} lo peth* vif* tap* tun* virbr* vnet* vboxnet* sit*
# Use the list of allowed interfaces if specified
-
${optionalString (cfg.allowInterfaces != null) "allowinterfaces ${toString cfg.allowInterfaces}"}
+
${optionalString (allowInterfaces != null) "allowinterfaces ${toString allowInterfaces}"}
${cfg.extraConfig}
'';
···
###### implementation
-
config = mkIf config.networking.useDHCP {
+
config = mkIf enableDHCP {
systemd.services.dhcpcd =
{ description = "DHCP Client";