summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorJoachim F <joachifm@users.noreply.github.com>2017-01-04 18:54:09 +0100
committerGitHub <noreply@github.com>2017-01-04 18:54:09 +0100
commit9e0dc9fa7c608dd658610badeffd6a6c24fc7dd2 (patch)
tree5641ae0eafb8928e90444ba14912552de17cbc24 /nixos
parentd4b960550bc8a8e177c161864306bb8cba7d3515 (diff)
parentbdb9cd1e172af5895cb47c1266350cd53edd0a47 (diff)
downloadnixpkgs-9e0dc9fa7c608dd658610badeffd6a6c24fc7dd2.tar
nixpkgs-9e0dc9fa7c608dd658610badeffd6a6c24fc7dd2.tar.gz
nixpkgs-9e0dc9fa7c608dd658610badeffd6a6c24fc7dd2.tar.bz2
nixpkgs-9e0dc9fa7c608dd658610badeffd6a6c24fc7dd2.tar.lz
nixpkgs-9e0dc9fa7c608dd658610badeffd6a6c24fc7dd2.tar.xz
nixpkgs-9e0dc9fa7c608dd658610badeffd6a6c24fc7dd2.tar.zst
nixpkgs-9e0dc9fa7c608dd658610badeffd6a6c24fc7dd2.zip
Merge pull request #21592 from joachifm/cjdns-optional-extraHosts
cjdns service: optional extraHosts
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/services/networking/cjdns.nix61
1 files changed, 41 insertions, 20 deletions
diff --git a/nixos/modules/services/networking/cjdns.nix b/nixos/modules/services/networking/cjdns.nix
index 0dd028997f4..a10851c1652 100644
--- a/nixos/modules/services/networking/cjdns.nix
+++ b/nixos/modules/services/networking/cjdns.nix
@@ -19,21 +19,30 @@ let
         type = types.str;
         description = "Public key at the opposite end of the tunnel.";
       };
+      hostname = mkOption {
+        default = "";
+        example = "foobar.hype";
+        type = types.str;
+        description = "Optional hostname to add to /etc/hosts; prevents reverse lookup failures.";
+      };
     };
   };
 
-  # check for the required attributes, otherwise
-  # permit attributes not undefined here
-  checkPeers = x:
-    x // {
-      connectTo = mapAttrs
-        (name: value:
-          if !hasAttr "publicKey" value then abort "cjdns peer ${name} missing a publicKey" else
-          if !hasAttr "password"  value then abort "cjdns peer ${name} missing a password"  else
-          value
-        )
-      x.connectTo;
-    };
+  # Additional /etc/hosts entries for peers with an associated hostname
+  cjdnsExtraHosts = import (pkgs.runCommand "cjdns-hosts" {}
+    # Generate a builder that produces an output usable as a Nix string value
+    ''
+      exec >$out
+      echo \'\'
+      ${concatStringsSep "\n" (mapAttrsToList (k: v:
+          optionalString (v.hostname != "")
+            "echo $(${pkgs.cjdns}/bin/publictoip6 ${v.publicKey}) ${v.hostname}")
+          (cfg.ETHInterface.connectTo // cfg.UDPInterface.connectTo))}
+      echo \'\'
+    '');
+
+  parseModules = x:
+    x // { connectTo = mapAttrs (name: value: { inherit (value) password publicKey; }) x.connectTo; };
 
   # would be nice to  merge 'cfg' with a //,
   # but the json nesting is wacky.
@@ -44,8 +53,8 @@ let
     };
     authorizedPasswords = map (p: { password = p; }) cfg.authorizedPasswords;
     interfaces = {
-      ETHInterface = if (cfg.ETHInterface.bind != "") then [ (checkPeers cfg.ETHInterface) ] else [ ];
-      UDPInterface = if (cfg.UDPInterface.bind != "") then [ (checkPeers cfg.UDPInterface) ] else [ ];
+      ETHInterface = if (cfg.ETHInterface.bind != "") then [ (parseModules cfg.ETHInterface) ] else [ ];
+      UDPInterface = if (cfg.UDPInterface.bind != "") then [ (parseModules cfg.UDPInterface) ] else [ ];
     };
 
     privateKey = "@CJDNS_PRIVATE_KEY@";
@@ -125,12 +134,12 @@ in
           '';
          };
         connectTo = mkOption {
-          type = types.attrsOf (types.attrsOf types.str);
+          type = types.attrsOf ( types.submodule ( connectToSubmodule ) );
           default = { };
           example = {
             "192.168.1.1:27313" = {
-              user      = "foobar";
-              password  = "5kG15EfpdcKNX3f2GSQ0H1HC7yIfxoCoImnO5FHM";
+              hostname = "homer.hype";
+              password = "5kG15EfpdcKNX3f2GSQ0H1HC7yIfxoCoImnO5FHM";
               publicKey = "371zpkgs8ss387tmr81q04mp0hg1skb51hw34vk1cq644mjqhup0.k";
             };
           };
@@ -170,12 +179,12 @@ in
         };
 
         connectTo = mkOption {
-          type = types.attrsOf (types.attrsOf types.str);
+          type = types.attrsOf ( types.submodule ( connectToSubmodule ) );
           default = { };
           example = {
             "01:02:03:04:05:06" = {
-              user      = "foobar";
-              password  = "5kG15EfpdcKNX3f2GSQ0H1HC7yIfxoCoImnO5FHM";
+              hostname = "homer.hype";
+              password = "5kG15EfpdcKNX3f2GSQ0H1HC7yIfxoCoImnO5FHM";
               publicKey = "371zpkgs8ss387tmr81q04mp0hg1skb51hw34vk1cq644mjqhup0.k";
             };
           };
@@ -186,6 +195,16 @@ in
         };
       };
 
+      addExtraHosts = mkOption {
+        type = types.bool;
+        default = false;
+        description = ''
+          Whether to add cjdns peers with an associated hostname to
+          <filename>/etc/hosts</filename>.  Beware that enabling this
+          incurs heavy eval-time costs.
+        '';
+      };
+
     };
 
   };
@@ -248,6 +267,8 @@ in
       };
     };
 
+    networking.extraHosts = mkIf cfg.addExtraHosts cjdnsExtraHosts;
+
     assertions = [
       { assertion = ( cfg.ETHInterface.bind != "" || cfg.UDPInterface.bind != "" || cfg.confFile != null );
         message = "Neither cjdns.ETHInterface.bind nor cjdns.UDPInterface.bind defined.";