summary refs log tree commit diff
path: root/nixos/modules/config/networking.nix
diff options
context:
space:
mode:
authorVladimír Čunát <vcunat@gmail.com>2017-07-30 09:57:41 +0200
committerVladimír Čunát <vcunat@gmail.com>2017-07-30 09:57:41 +0200
commit8177561e8f248942afea06d096cb72ab0af23524 (patch)
tree7cb4d670a21a24fde4831bfaead720281eb1391d /nixos/modules/config/networking.nix
parent25c762f2bccab9d6c6d0d4cc11916f605cfeabcc (diff)
parent635ecd802fd1928db0b33b396744b620c7b82ffe (diff)
downloadnixlib-8177561e8f248942afea06d096cb72ab0af23524.tar
nixlib-8177561e8f248942afea06d096cb72ab0af23524.tar.gz
nixlib-8177561e8f248942afea06d096cb72ab0af23524.tar.bz2
nixlib-8177561e8f248942afea06d096cb72ab0af23524.tar.lz
nixlib-8177561e8f248942afea06d096cb72ab0af23524.tar.xz
nixlib-8177561e8f248942afea06d096cb72ab0af23524.tar.zst
nixlib-8177561e8f248942afea06d096cb72ab0af23524.zip
Merge #27105: more correct form of /etc/hosts
Diffstat (limited to 'nixos/modules/config/networking.nix')
-rw-r--r--nixos/modules/config/networking.nix44
1 files changed, 41 insertions, 3 deletions
diff --git a/nixos/modules/config/networking.nix b/nixos/modules/config/networking.nix
index cc967947dffb..616f76f6f47c 100644
--- a/nixos/modules/config/networking.nix
+++ b/nixos/modules/config/networking.nix
@@ -20,12 +20,35 @@ in
 
   options = {
 
+    networking.fqdn = lib.mkOption {
+      type = types.nullOr types.str;
+      default = null;
+      example = "foo.example.com";
+      description = ''
+        Fully qualified domain name, if any.
+      '';
+    };
+
+    networking.hosts = lib.mkOption {
+      type = types.attrsOf ( types.listOf types.str );
+      default = {};
+      example = literalExample ''
+        {
+          "127.0.0.1" = [ "foo.bar.baz" ];
+          "192.168.0.2" = [ "fileserver.local" "nameserver.local" ];
+        };
+      '';
+      description = ''
+        Locally defined maps of hostnames to IP addresses.
+      '';
+    };
+
     networking.extraHosts = lib.mkOption {
       type = types.lines;
       default = "";
       example = "192.168.0.1 lanlocalhost";
       description = ''
-        Additional entries to be appended to <filename>/etc/hosts</filename>.
+        Additional verbatim entries to be appended to <filename>/etc/hosts</filename>.
       '';
     };
 
@@ -176,6 +199,9 @@ in
 
   config = {
 
+    warnings = optional (cfg.extraHosts != "")
+      "networking.extraHosts is deprecated, please use networking.hosts instead";
+
     environment.etc =
       { # /etc/services: TCP/UDP port assignments.
         "services".source = pkgs.iana-etc + "/etc/services";
@@ -188,11 +214,23 @@ in
 
         # /etc/hosts: Hostname-to-IP mappings.
         "hosts".text =
+          let oneToString = set : ip : ip + " " + concatStringsSep " " ( getAttr ip set );
+              allToString = set : concatMapStringsSep "\n" ( oneToString set ) ( attrNames set );
+              userLocalHosts = optionalString
+                ( builtins.hasAttr "127.0.0.1" cfg.hosts )
+                ( concatStringsSep " " ( remove "localhost" cfg.hosts."127.0.0.1" ));
+              userLocalHosts6 = optionalString
+                ( builtins.hasAttr "::1" cfg.hosts )
+                ( concatStringsSep " " ( remove "localhost" cfg.hosts."::1" ));
+              otherHosts = allToString ( removeAttrs cfg.hosts [ "127.0.0.1" "::1" ]);
+              maybeFQDN = optionalString ( cfg.fqdn != null ) cfg.fqdn;
+          in
           ''
-            127.0.0.1 localhost
+            127.0.0.1 ${maybeFQDN} ${userLocalHosts} localhost
             ${optionalString cfg.enableIPv6 ''
-              ::1 localhost
+              ::1 ${maybeFQDN} ${userLocalHosts6} localhost
             ''}
+            ${otherHosts}
             ${cfg.extraHosts}
           '';