summary refs log tree commit diff
path: root/nixos/modules
diff options
context:
space:
mode:
authorWilliam A. Kennington III <william@wkennington.com>2014-11-24 18:11:26 -0800
committerWilliam A. Kennington III <william@wkennington.com>2014-11-26 11:22:03 -0800
commitc417012c1b1ed3793ac3bd106bfc840d9aad08c7 (patch)
treef27f946e9a2bbf6647cc77ba24cc39310469628a /nixos/modules
parent7005e289dabb387bd835fdf7d9cdfc0e94e56359 (diff)
downloadnixlib-c417012c1b1ed3793ac3bd106bfc840d9aad08c7.tar
nixlib-c417012c1b1ed3793ac3bd106bfc840d9aad08c7.tar.gz
nixlib-c417012c1b1ed3793ac3bd106bfc840d9aad08c7.tar.bz2
nixlib-c417012c1b1ed3793ac3bd106bfc840d9aad08c7.tar.lz
nixlib-c417012c1b1ed3793ac3bd106bfc840d9aad08c7.tar.xz
nixlib-c417012c1b1ed3793ac3bd106bfc840d9aad08c7.tar.zst
nixlib-c417012c1b1ed3793ac3bd106bfc840d9aad08c7.zip
nixos/dhcpcd: Respect per interface dhcp options
Diffstat (limited to 'nixos/modules')
-rw-r--r--nixos/modules/services/networking/dhcpcd.nix19
1 files changed, 16 insertions, 3 deletions
diff --git a/nixos/modules/services/networking/dhcpcd.nix b/nixos/modules/services/networking/dhcpcd.nix
index 8b5057cc4eec..115ca5ce2aba 100644
--- a/nixos/modules/services/networking/dhcpcd.nix
+++ b/nixos/modules/services/networking/dhcpcd.nix
@@ -8,15 +8,28 @@ let
 
   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"
     ''
@@ -41,7 +54,7 @@ let
       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}
     '';
@@ -133,7 +146,7 @@ in
 
   ###### implementation
 
-  config = mkIf config.networking.useDHCP {
+  config = mkIf enableDHCP {
 
     systemd.services.dhcpcd =
       { description = "DHCP Client";