about summary refs log tree commit diff
path: root/nixos/modules/config/networking.nix
diff options
context:
space:
mode:
authorValentin Shirokov <regellosigkeitsaxiom@yandex.ru>2017-07-08 21:13:16 +0300
committerValentin Shirokov <regellosigkeitsaxiom@yandex.ru>2017-07-08 21:13:16 +0300
commit5f2826fbed5ee26fb905f3950c2d7c5631933ba3 (patch)
tree7c0df064ab9ec774515cc134e86582947c03755a /nixos/modules/config/networking.nix
parentf9ec52dedceb3904b49c27d49076a881c43ba4cf (diff)
downloadnixlib-5f2826fbed5ee26fb905f3950c2d7c5631933ba3.tar
nixlib-5f2826fbed5ee26fb905f3950c2d7c5631933ba3.tar.gz
nixlib-5f2826fbed5ee26fb905f3950c2d7c5631933ba3.tar.bz2
nixlib-5f2826fbed5ee26fb905f3950c2d7c5631933ba3.tar.lz
nixlib-5f2826fbed5ee26fb905f3950c2d7c5631933ba3.tar.xz
nixlib-5f2826fbed5ee26fb905f3950c2d7c5631933ba3.tar.zst
nixlib-5f2826fbed5ee26fb905f3950c2d7c5631933ba3.zip
Added networking.hosts and networking.fqdn options
Diffstat (limited to 'nixos/modules/config/networking.nix')
-rw-r--r--nixos/modules/config/networking.nix47
1 files changed, 36 insertions, 11 deletions
diff --git a/nixos/modules/config/networking.nix b/nixos/modules/config/networking.nix
index ea9e8b712c68..c0b0c8494c84 100644
--- a/nixos/modules/config/networking.nix
+++ b/nixos/modules/config/networking.nix
@@ -20,24 +20,35 @@ in
 
   options = {
 
-    networking.extraLocalHosts = lib.mkOption {
-      type = types.listOf types.str;
-      default = [];
-      example = [ "localhost.localdomain" "workinprogress.example.com" ];
+    networking.fqdn = lib.mkOption {
+      type = types.nullOr types.str;
+      default = null;
+      example = "foo.example.com";
       description = ''
-        Additional entries to be appended to 127.0.0.1 entry in <filename>/etc/hosts</filename>.
+        Full qualified domain name, if any.
       '';
     };
 
+    networking.hosts = lib.mkOption {
+      type = types.attrsOf ( types.listOf types.str );
+      default = {};
+      example = ''
+        {
+          "localhost" = [ "foo.bar" ];
+          "192.168.0.2" = [ "fileserver.local" "nameserver.local" ];
+        };
+      '';
+      description = ''
+        Locally defined maps of IP addresses to hostnames'
+      '';
+    };
 
     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>.
-        Note that entries for 127.0.0.1 will not always work correctly if added from here.
-        They should be added via <literal>networking.extraLocalHosts</literal>.
+        Additional verbatim entries to be appended to <filename>/etc/hosts</filename>.
       '';
     };
 
@@ -199,12 +210,26 @@ in
         "rpc".source = pkgs.glibc.out + "/etc/rpc";
 
         # /etc/hosts: Hostname-to-IP mappings.
-        "hosts".text = let foo = concatStringsSep " " cfg.extraLocalHosts; in
+        "hosts".text =
+          let oneToString = set : ip : ip + " " + concatStringsSep " " ( getAttr ip set );
+              allToString = set : concatStringsSep "\n" ( map ( oneToString set ) ( builtins.attrNames set ));
+              userLocalHosts =
+                if builtins.hasAttr "127.0.0.1" cfg.hosts
+                then concatStringsSep " " ( filter (x : x != "localhost" ) ( getAttr "127.0.0.1" cfg.hosts))
+                else "";
+              userLocalHosts6 =
+                if builtins.hasAttr "::1" cfg.hosts
+                then concatStringsSep " " ( filter (x : x != "localhost" ) ( getAttr "::1" cfg.hosts))
+                else "";
+              otherHosts = allToString ( removeAttrs cfg.hosts [ "127.0.0.1" "::1" ]);
+              maybeFQDN = if cfg.fqdn == null then "" else cfq.fqdn;
+          in
           ''
-            127.0.0.1 localhost ${foo}
+            127.0.0.1 ${maybeFQDN} ${userLocalHosts} localhost
             ${optionalString cfg.enableIPv6 ''
-              ::1 localhost ${foo}
+              ::1 ${maybeFQDN} ${userLocalHosts6} localhost
             ''}
+            ${otherHosts}
             ${cfg.extraHosts}
           '';