Merge pull request #47241 from oxij/pull/36261-fix-local-hostname-alternative
nixos/networking: add hostname to /etc/hosts by default, simplify
This commit is contained in:
commit
6419bdac05
@ -16,6 +16,13 @@ let
|
|||||||
resolvconfOptions = cfg.resolvconfOptions
|
resolvconfOptions = cfg.resolvconfOptions
|
||||||
++ optional cfg.dnsSingleRequest "single-request"
|
++ optional cfg.dnsSingleRequest "single-request"
|
||||||
++ optional cfg.dnsExtensionMechanism "edns0";
|
++ optional cfg.dnsExtensionMechanism "edns0";
|
||||||
|
|
||||||
|
|
||||||
|
localhostMapped4 = cfg.hosts ? "127.0.0.1" && elem "localhost" cfg.hosts."127.0.0.1";
|
||||||
|
localhostMapped6 = cfg.hosts ? "::1" && elem "localhost" cfg.hosts."::1";
|
||||||
|
|
||||||
|
localhostMultiple = any (elem "localhost") (attrValues (removeAttrs cfg.hosts [ "127.0.0.1" "::1" ]));
|
||||||
|
|
||||||
in
|
in
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -23,8 +30,7 @@ in
|
|||||||
options = {
|
options = {
|
||||||
|
|
||||||
networking.hosts = lib.mkOption {
|
networking.hosts = lib.mkOption {
|
||||||
type = types.attrsOf ( types.listOf types.str );
|
type = types.attrsOf (types.listOf types.str);
|
||||||
default = {};
|
|
||||||
example = literalExample ''
|
example = literalExample ''
|
||||||
{
|
{
|
||||||
"127.0.0.1" = [ "foo.bar.baz" ];
|
"127.0.0.1" = [ "foo.bar.baz" ];
|
||||||
@ -192,6 +198,29 @@ in
|
|||||||
|
|
||||||
config = {
|
config = {
|
||||||
|
|
||||||
|
assertions = [{
|
||||||
|
assertion = localhostMapped4;
|
||||||
|
message = ''`networking.hosts` doesn't map "127.0.0.1" to "localhost"'';
|
||||||
|
} {
|
||||||
|
assertion = !cfg.enableIPv6 || localhostMapped6;
|
||||||
|
message = ''`networking.hosts` doesn't map "::1" to "localhost"'';
|
||||||
|
} {
|
||||||
|
assertion = !localhostMultiple;
|
||||||
|
message = ''
|
||||||
|
`networking.hosts` maps "localhost" to something other than "127.0.0.1"
|
||||||
|
or "::1". This will break some applications. Please use
|
||||||
|
`networking.extraHosts` if you really want to add such a mapping.
|
||||||
|
'';
|
||||||
|
}];
|
||||||
|
|
||||||
|
networking.hosts = {
|
||||||
|
"127.0.0.1" = [ "localhost" ];
|
||||||
|
} // optionalAttrs (cfg.hostName != "") {
|
||||||
|
"127.0.1.1" = [ cfg.hostName ];
|
||||||
|
} // optionalAttrs cfg.enableIPv6 {
|
||||||
|
"::1" = [ "localhost" ];
|
||||||
|
};
|
||||||
|
|
||||||
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";
|
||||||
@ -203,25 +232,13 @@ in
|
|||||||
"rpc".source = pkgs.glibc.out + "/etc/rpc";
|
"rpc".source = pkgs.glibc.out + "/etc/rpc";
|
||||||
|
|
||||||
# /etc/hosts: Hostname-to-IP mappings.
|
# /etc/hosts: Hostname-to-IP mappings.
|
||||||
"hosts".text =
|
"hosts".text = let
|
||||||
let oneToString = set : ip : ip + " " + concatStringsSep " " ( getAttr ip set );
|
oneToString = set: ip: ip + " " + concatStringsSep " " set.${ip};
|
||||||
allToString = set : concatMapStringsSep "\n" ( oneToString set ) ( attrNames set );
|
allToString = set: concatMapStringsSep "\n" (oneToString set) (attrNames set);
|
||||||
userLocalHosts = optionalString
|
in ''
|
||||||
( builtins.hasAttr "127.0.0.1" cfg.hosts )
|
${allToString cfg.hosts}
|
||||||
( concatStringsSep " " ( remove "localhost" cfg.hosts."127.0.0.1" ));
|
${cfg.extraHosts}
|
||||||
userLocalHosts6 = optionalString
|
'';
|
||||||
( builtins.hasAttr "::1" cfg.hosts )
|
|
||||||
( concatStringsSep " " ( remove "localhost" cfg.hosts."::1" ));
|
|
||||||
otherHosts = allToString ( removeAttrs cfg.hosts [ "127.0.0.1" "::1" ]);
|
|
||||||
in
|
|
||||||
''
|
|
||||||
127.0.0.1 ${userLocalHosts} localhost
|
|
||||||
${optionalString cfg.enableIPv6 ''
|
|
||||||
::1 ${userLocalHosts6} localhost
|
|
||||||
''}
|
|
||||||
${otherHosts}
|
|
||||||
${cfg.extraHosts}
|
|
||||||
'';
|
|
||||||
|
|
||||||
# /etc/host.conf: resolver configuration file
|
# /etc/host.conf: resolver configuration file
|
||||||
"host.conf".text = cfg.hostConf;
|
"host.conf".text = cfg.hostConf;
|
||||||
@ -296,4 +313,4 @@ in
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user