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: provide /etc/hostname #21566

Merged
merged 2 commits into from Jan 2, 2017
Merged

nixos: provide /etc/hostname #21566

merged 2 commits into from Jan 2, 2017

Conversation

bjornfor
Copy link
Contributor

@bjornfor bjornfor commented Jan 1, 2017

Motivation for this change

/etc/hostname is the file used by hostnamectl(1) and the
org.freedesktop.hostname1 dbus service (both provided by systemd) to get
the "static hostname". Better provide it so that users of those
tools/services get a proper hostname.

An example of an issue created by the lack of /etc/hostname is that the
bluetooth stack on NixOS identifies itself to peers as "BlueZ $VERSION"
instead of the hostname.

References:
https://www.freedesktop.org/software/systemd/man/hostname.html

Things done
  • Tested using sandboxing
    (nix.useSandbox on NixOS,
    or option build-use-sandbox in nix.conf
    on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • Linux
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

Create the file using attrset instead of list, to make it easier to
later provide other files in the same module.
@mention-bot
Copy link

@bjornfor, thanks for your PR! By analyzing the history of the files in this pull request, we identified @wizeman, @wkennington and @ericsagnes to be potential reviewers.

@bjornfor
Copy link
Contributor Author

bjornfor commented Jan 1, 2017

One side effect I just noticed. Without this PR, hostnamectl prints

Static hostname: n/a
Transient hostname: the_hostname
...

and if I run sudo hostname foo, that "Transient hostname" line gets updated. As expected.

With this PR, only the "Static hostname" line is shown. (I guess that's fine, now that it includes the proper hostname.) BUT, there is no "Transient hostname" line, even after sudo hostname foo. That surprised me.

cc @edolstra

@grahamc
Copy link
Member

grahamc commented Jan 1, 2017

I added "policy discussion" since adding new files to /etc by default seems to require some discussion. Feel free to remove if you disagree :)

@Mic92
Copy link
Member

Mic92 commented Jan 1, 2017

Strange, why does Bluez not just call hostname(2) like any other tool?

@bjornfor
Copy link
Contributor Author

bjornfor commented Jan 1, 2017

@Mic92: I think it's because they are fetching more than just the "transient" hostname that you get from the kernel. Basically, they get "PrettyHostname", "StaticHostname" and "Chassis", all provided by org.freedesktop.hostname1[1].

See bluez code here: http://git.kernel.org/cgit/bluetooth/bluez.git/tree/plugins/hostname.c#n133

[1] https://www.freedesktop.org/wiki/Software/systemd/hostnamed/


I thought about patching bluez at first. But when I saw that hostnamectl prints "Static hostname: n/a" without an /etc/hostname file, I dropped that thought. (How much energy should we spend to work around/against upstream?)

Another option could be patching systemd to treat the hostname from the kernel as "static hostname". But I kind of like the idea of a static vs transient hostname. Snippet from hostnamed documentation:

The transient (dynamic) host name is the one configured via the kernel's sethostbyname(). It can be different from the static hostname in case DHCP or mDNS have been configured to change the name based on network information.

@jokogr
Copy link
Contributor

jokogr commented Jan 1, 2017

I use the /etc/hostname file to identify my machine and load a specific configuration file from my centralized configuration repository, e.g. have a look at https://git.joko.gr/joko/etc-nixos/src/master/configuration.nix#L4 (idea taken from @nathan7's configuration files).

Would this change cause any issues on my setups?

@bjornfor
Copy link
Contributor Author

bjornfor commented Jan 2, 2017

@jokogr: AFAICT, just change networking.hostName = builtins.readFile /etc/hostname; to networking.hostName = "thehostname";.

@bjornfor
Copy link
Contributor Author

bjornfor commented Jan 2, 2017

BUT, there is no "Transient hostname" line, even after sudo hostname foo. That surprised me.

It turns out that hostnamectl caches the "Transient hostname" for a few minutes (5?). Eventually it picks up the value from "sudo hostname foo".

@edolstra
Copy link
Member

edolstra commented Jan 2, 2017

This looks fine to me.

@LnL7
Copy link
Member

LnL7 commented Jan 2, 2017

I thought we wanted as little mutable stuff in /etc as possible, unless it makes sense. (eg. a service that can reconfigure itself without restarting)
In this case the contents of /etc/hostname might not even match what is returned by hostname(2).

@bjornfor
Copy link
Contributor Author

bjornfor commented Jan 2, 2017

I thought we wanted as little mutable stuff in /etc as possible, unless it makes sense. (eg. a service that can reconfigure itself without restarting)

This PR creates an immutable /etc/hostname file, like most other files in /etc created by NixOS. (It'll be a symlink to /etc/static/hostname which in turn is a symlink to /nix/store/xybf7pjnnr1nq2j7apbx1110zqhx7qa1-etc-hostname.)

In this case the contents of /etc/hostname might not even match what is returned by hostname(2).

Correct. That's why systemd has the concept of static vs transient hostname. The static name is stored in /etc/hostname and the transient (dynamic) hostname is the one stored in the kernel. See https://www.freedesktop.org/wiki/Software/systemd/hostnamed/.

@LnL7
Copy link
Member

LnL7 commented Jan 2, 2017

With mutable I was referring to the /etc/static symlink that changes with a nixos-rebuild. Anyway it looks fine to me, I just expected there would be more discussion around this.

# org.freedesktop.hostname1 dbus service (both provided by systemd)
environment.etc."hostname" = mkIf (cfg.hostName != "")
{
text = cfg.hostName;
Copy link
Member

@Mic92 Mic92 Jan 2, 2017

Choose a reason for hiding this comment

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

It would be nice to have a trailing newline in this file.

$ cat /etc/hostname
turingmachine$
text = "${cfg.hostname}\n";

This would be consistent with hostnamectl's behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. Fixed.

/etc/hostname is the file used by hostnamectl(1) and the
org.freedesktop.hostname1 dbus service (both provided by systemd) to get
the "static hostname". Better provide it so that users of those
tools/services get a proper hostname.

An example of an issue created by the lack of /etc/hostname is that the
bluetooth stack on NixOS identifies itself to peers as "BlueZ $VERSION"
instead of the hostname.

References:
https://www.freedesktop.org/software/systemd/man/hostname.html

Changes v1 -> v2:
  * ensure /etc/hostname ends with a newline
@Mic92 Mic92 merged commit 1cc8b83 into NixOS:master Jan 2, 2017
@bjornfor bjornfor deleted the hostname branch January 2, 2017 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants