Merge pull request #81945 from Infinisil/hostFiles

Introduce `networking.hostFiles` option
This commit is contained in:
Silvan Mosberger 2020-03-12 15:56:30 +01:00 committed by GitHub
commit 8f2109cda4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 32 deletions

View file

@ -35,12 +35,22 @@ in
''; '';
}; };
networking.hostFiles = lib.mkOption {
type = types.listOf types.path;
defaultText = lib.literalExample "Hosts from `networking.hosts` and `networking.extraHosts`";
example = lib.literalExample ''[ "''${pkgs.my-blocklist-package}/share/my-blocklist/hosts" ]'';
description = ''
Files that should be concatenated together to form <filename>/etc/hosts</filename>.
'';
};
networking.extraHosts = lib.mkOption { networking.extraHosts = lib.mkOption {
type = types.lines; type = types.lines;
default = ""; default = "";
example = "192.168.0.1 lanlocalhost"; example = "192.168.0.1 lanlocalhost";
description = '' description = ''
Additional verbatim entries to be appended to <filename>/etc/hosts</filename>. Additional verbatim entries to be appended to <filename>/etc/hosts</filename>.
For adding hosts from derivation results, use <option>networking.hostFiles</option> instead.
''; '';
}; };
@ -159,6 +169,15 @@ in
"::1" = [ "localhost" ]; "::1" = [ "localhost" ];
}; };
networking.hostFiles = let
stringHosts =
let
oneToString = set: ip: ip + " " + concatStringsSep " " set.${ip} + "\n";
allToString = set: concatMapStrings (oneToString set) (attrNames set);
in pkgs.writeText "string-hosts" (allToString (filterAttrs (_: v: v != []) cfg.hosts));
extraHosts = pkgs.writeText "extra-hosts" cfg.extraHosts;
in mkBefore [ stringHosts extraHosts ];
environment.etc = environment.etc =
{ # /etc/services: TCP/UDP port assignments. { # /etc/services: TCP/UDP port assignments.
services.source = pkgs.iana-etc + "/etc/services"; services.source = pkgs.iana-etc + "/etc/services";
@ -167,12 +186,8 @@ in
protocols.source = pkgs.iana-etc + "/etc/protocols"; protocols.source = pkgs.iana-etc + "/etc/protocols";
# /etc/hosts: Hostname-to-IP mappings. # /etc/hosts: Hostname-to-IP mappings.
hosts.text = let hosts.source = pkgs.runCommandNoCC "hosts" {} ''
oneToString = set: ip: ip + " " + concatStringsSep " " set.${ip}; cat ${escapeShellArgs cfg.hostFiles} > $out
allToString = set: concatMapStringsSep "\n" (oneToString set) (attrNames set);
in ''
${allToString (filterAttrs (_: v: v != []) cfg.hosts)}
${cfg.extraHosts}
''; '';
# /etc/host.conf: resolver configuration file # /etc/host.conf: resolver configuration file

View file

@ -29,17 +29,13 @@ let
}; };
# Additional /etc/hosts entries for peers with an associated hostname # Additional /etc/hosts entries for peers with an associated hostname
cjdnsExtraHosts = import (pkgs.runCommand "cjdns-hosts" {} cjdnsExtraHosts = pkgs.runCommandNoCC "cjdns-hosts" {} ''
# Generate a builder that produces an output usable as a Nix string value exec >$out
'' ${concatStringsSep "\n" (mapAttrsToList (k: v:
exec >$out optionalString (v.hostname != "")
echo \'\' "echo $(${pkgs.cjdns}/bin/publictoip6 ${v.publicKey}) ${v.hostname}")
${concatStringsSep "\n" (mapAttrsToList (k: v: (cfg.ETHInterface.connectTo // cfg.UDPInterface.connectTo))}
optionalString (v.hostname != "") '';
"echo $(${pkgs.cjdns}/bin/publictoip6 ${v.publicKey}) ${v.hostname}")
(cfg.ETHInterface.connectTo // cfg.UDPInterface.connectTo))}
echo \'\'
'');
parseModules = x: parseModules = x:
x // { connectTo = mapAttrs (name: value: { inherit (value) password publicKey; }) x.connectTo; }; x // { connectTo = mapAttrs (name: value: { inherit (value) password publicKey; }) x.connectTo; };
@ -144,13 +140,15 @@ in
connectTo = mkOption { connectTo = mkOption {
type = types.attrsOf ( types.submodule ( connectToSubmodule ) ); type = types.attrsOf ( types.submodule ( connectToSubmodule ) );
default = { }; default = { };
example = { example = literalExample ''
"192.168.1.1:27313" = { {
hostname = "homer.hype"; "192.168.1.1:27313" = {
password = "5kG15EfpdcKNX3f2GSQ0H1HC7yIfxoCoImnO5FHM"; hostname = "homer.hype";
publicKey = "371zpkgs8ss387tmr81q04mp0hg1skb51hw34vk1cq644mjqhup0.k"; password = "5kG15EfpdcKNX3f2GSQ0H1HC7yIfxoCoImnO5FHM";
}; publicKey = "371zpkgs8ss387tmr81q04mp0hg1skb51hw34vk1cq644mjqhup0.k";
}; };
}
'';
description = '' description = ''
Credentials for making UDP tunnels. Credentials for making UDP tunnels.
''; '';
@ -189,13 +187,15 @@ in
connectTo = mkOption { connectTo = mkOption {
type = types.attrsOf ( types.submodule ( connectToSubmodule ) ); type = types.attrsOf ( types.submodule ( connectToSubmodule ) );
default = { }; default = { };
example = { example = literalExample ''
"01:02:03:04:05:06" = { {
hostname = "homer.hype"; "01:02:03:04:05:06" = {
password = "5kG15EfpdcKNX3f2GSQ0H1HC7yIfxoCoImnO5FHM"; hostname = "homer.hype";
publicKey = "371zpkgs8ss387tmr81q04mp0hg1skb51hw34vk1cq644mjqhup0.k"; password = "5kG15EfpdcKNX3f2GSQ0H1HC7yIfxoCoImnO5FHM";
}; publicKey = "371zpkgs8ss387tmr81q04mp0hg1skb51hw34vk1cq644mjqhup0.k";
}; };
}
'';
description = '' description = ''
Credentials for connecting look similar to UDP credientials Credentials for connecting look similar to UDP credientials
except they begin with the mac address. except they begin with the mac address.
@ -278,7 +278,7 @@ in
}; };
}; };
networking.extraHosts = mkIf cfg.addExtraHosts cjdnsExtraHosts; networking.hostFiles = mkIf cfg.addExtraHosts [ cjdnsExtraHosts ];
assertions = [ assertions = [
{ assertion = ( cfg.ETHInterface.bind != "" || cfg.UDPInterface.bind != "" || cfg.confFile != null ); { assertion = ( cfg.ETHInterface.bind != "" || cfg.UDPInterface.bind != "" || cfg.confFile != null );