Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nixos/networking: add hostname to /etc/hosts by default, simplify #47241

Merged
merged 2 commits into from Oct 27, 2018

Conversation

oxij
Copy link
Member

@oxij oxij commented Sep 23, 2018

Motivation for this change

#1248 and #36261. This is an alternative simpler implementation of #36261.

Since networking.hosts is properly typed all of that magic /etc/hosts generator does on master can be dropped. People that disagree with the value of networking.hosts can simply mkForce.

Things done
  • It evaluates.
  • Tested all my configs to boot.

Do not merge before 2018-09-30.

@oxij
Copy link
Member Author

oxij commented Sep 23, 2018 via email

@danbst
Copy link
Contributor

danbst commented Sep 24, 2018

This can make localhost not in top of /etc/hosts:

10.0.0.1 xxx
127.0.0.1 localhost

Then localhost becomes a bit floating - if you add networking.hosts."4.x.x.x" = "localhost", localhost will resolve to 127.0.0.1, but if you add networking.hosts."10.x.x.x" = "localhost", localhost will resolve to 10.x.x.x

Probably add assert and warning that forbids redefining localhost in networking.hosts? On master both would result to localhost = 127.0.0.1

@danbst
Copy link
Contributor

danbst commented Sep 24, 2018

Nah, mkBefore/mkAfter don't work here

{ config, lib, ... }:
let
    inherit (lib) optionalAttrs;
    cfg = config.networking;
    oxijPR = {
        networking.hosts = {
            "127.0.0.1" = [ "localhost" ];
        } // optionalAttrs (cfg.hostName != "") {
            "127.0.1.1" = [ cfg.hostName ];
        } // optionalAttrs cfg.enableIPv6 {
            "::1" = [ "localhost" ];
        };
    };
    correctOrderOverride = {
        networking.hosts = lib.mkBefore {
            "4.0.0.0" = [ "localhost" ];
            "10.0.0.0" = [ "localhost" ];
        };
    };
in {
    imports = [
        oxijPR
        correctOrderOverride
    ];
}

result is

$ nix-instantiate -I nixos-config=$PWD/hosts.nix --eval -E 'with import <nixpkgs/nixos> { }; config.networking.hosts'
{ "10.0.0.0" = <CODE>; "127.0.0.1" = <CODE>; "127.0.1.1" = <CODE>; "4.0.0.0" = <CODE>; "::1" = <CODE>; }

@oxij
Copy link
Member Author

oxij commented Sep 24, 2018 via email

@oxij oxij force-pushed the pull/36261-fix-local-hostname-alternative branch from 2434e18 to f3362dc Compare September 30, 2018 14:43
@oxij
Copy link
Member Author

oxij commented Sep 30, 2018

Added the asserts.

message = ''
`networking.hosts` maps a non-loopback address to `localhost`. Generated
`/etc/hosts` might make `localhost` resolve to that non-loopback address instead
of `127.0.0.1` and `::1` which would break some applications. Please use
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message should be simplified. It is also not very correct, because one may map "127.0.0.2" to localhost and it would be perfectly correct mapping

@oxij
Copy link
Member Author

oxij commented Oct 1, 2018 via email

@danbst
Copy link
Contributor

danbst commented Oct 1, 2018

Maybe

You are trying to map "localhost" to address 127.0.0.2, but "localhost" must resolve
to 127.0.0.1 or ::1 only. Override environment.etc."hosts" and use /etc/hosts format if
you really want to do this.

Since `networking.hosts` is properly typed all of that magic `/etc/hosts` generator
does can be dropped. People that disagree with the value of `networking.hosts` can
simply `mkForce`.
We use `127.0.1.1` instead of `127.0.0.1` because some applications will fail if
`127.0.0.1` resolves to something other than `localhost`.

Debian does the same.

See NixOS#1248 and NixOS#36261.
@oxij oxij force-pushed the pull/36261-fix-local-hostname-alternative branch from f3362dc to c578924 Compare October 2, 2018 23:59
@oxij
Copy link
Member Author

oxij commented Oct 3, 2018

Done.

@oxij
Copy link
Member Author

oxij commented Oct 12, 2018 via email

@oxij
Copy link
Member Author

oxij commented Oct 19, 2018

@GrahamcOfBorg eval

@oxij
Copy link
Member Author

oxij commented Oct 26, 2018

Can we merge this yet?

/cc @xeji @Mic92

@xeji xeji merged commit 6419bda into NixOS:master Oct 27, 2018
@oxij oxij deleted the pull/36261-fix-local-hostname-alternative branch November 18, 2018 08:57
@costrouc
Copy link
Member

So by no means a problem with this PR but I wanted to note that this broke my dnsmasq configuration. The server that had dnsmasq running would return its ip address along with 127.0.1.1. which would cause that when I would try to ssh into the dnsmasq server it would instead ssh into localhost. This was my fault for using /etc/hosts in the addn-hosts option. Hope this may help someone in the future if they run into this issue. I instead created another /etc/dnsmasq.hosts file with environment.etc = { "dnsmasq.hosts".text = ''..........''};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants