Skip to content

nixos/os-release: make default_hostname distribution default#359571

Merged
jopejoe1 merged 1 commit intoNixOS:masterfrom
oneingan:patch-2
Nov 29, 2024
Merged

nixos/os-release: make default_hostname distribution default#359571
jopejoe1 merged 1 commit intoNixOS:masterfrom
oneingan:patch-2

Conversation

@oneingan
Copy link
Contributor

@oneingan oneingan commented Nov 27, 2024

Motivation for this change

The current implementation of DEFAULT_HOSTNAME in version.nix does not account for cases where networking.hostName is an empty string. This can lead to incorrect or unexpected behavior in scenarios where networking.hostName is unset or explicitly set to an empty value, even when networking.domain is defined.

In such corner cases, fqdnOrHostName can produce inconsistent results or cause runtime errors due to assumptions about hostName always being set. By explicitly checking for an empty hostName, we can ensure that DEFAULT_HOSTNAME only propagates valid values.

Changes Made

  • Updated the DEFAULT_HOSTNAME definition in nixos/modules/misc/version.nix to:
    DEFAULT_HOSTNAME = optionalString (config.networking.hostName != "") config.networking.fqdnOrHostName;

Impact

This change ensures that configurations with networking.hostName = ""; do not inadvertently propagate invalid or unexpected values for DEFAULT_HOSTNAME. It also prevents potential errors when hostName is empty but domain is defined, as fqdnOrHostName may behave unpredictably in such cases.


Things Done:

  • Updated nixos/modules/misc/version.nix to handle empty networking.hostName.
  • Built and tested NixOS system configurations with different networking.hostName and networking.domain settings to verify behavior.

Testing:

  • Built on platform(s):
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • Tested nixos-rebuild with varying configurations:
    • networking.hostName = ""; networking.domain = null;
    • networking.hostName = ""; networking.domain = "example.com";
    • networking.hostName = "example"; networking.domain = "example.com";
    • networking.hostName = null; networking.domain = null;

Submission Checklist:
Make sure you’ve followed the Nixpkgs contribution guidelines:

@github-actions github-actions bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Nov 27, 2024
@oneingan oneingan requested a review from jopejoe1 November 27, 2024 12:59
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Nov 28, 2024
Copy link
Contributor

@SigmaSquadron SigmaSquadron left a comment

Choose a reason for hiding this comment

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

For reference, the relevant /etc/os-release manual page.

Distribution-level defaults and metadata

DEFAULT_HOSTNAME=

A string specifying the hostname if hostname(5) is not
present and no other configuration source specifies the
hostname. Must be either a single DNS label (a string
composed of 7-bit ASCII lower-case characters and no spaces
or dots, limited to the format allowed for DNS domain name
labels), or a sequence of such labels separated by single
dots that forms a valid DNS FQDN. The hostname must be at
most 64 characters, which is a Linux limitation (DNS allows
longer names).

See org.freedesktop.hostname1(5) for a description of how
systemd-hostnamed.service(8) determines the fallback
hostname.

Added in version 248.

@oneingan oneingan changed the title nixos/os-release: make default_hostname optional nixos/os-release: make default_hostname distribution default Nov 28, 2024
@oneingan
Copy link
Contributor Author

I accept your changes and have also updated the PR title. My only concern is whether this change can be backported, as the regression exists in 24.11

Copy link
Contributor

@SigmaSquadron SigmaSquadron left a comment

Choose a reason for hiding this comment

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

Please squash the commits; otherwise LGTM.

@ofborg ofborg bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. and removed 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. labels Nov 29, 2024
@SigmaSquadron SigmaSquadron added the 12.approvals: 1 This PR was reviewed and approved by one person. label Nov 29, 2024
@jopejoe1 jopejoe1 merged commit bf5d64a into NixOS:master Nov 29, 2024
@github-actions
Copy link
Contributor

Successfully created backport PR for release-24.11:

@SigmaSquadron
Copy link
Contributor

@jopejoe1: While you're here, might I persuade you to take a look at this related PR?

@jopejoe1
Copy link
Member

Sure

@nfroger
Copy link
Contributor

nfroger commented Jan 31, 2025

This change brings a regression because this breaks hostname fetching from DHCP with dhcpcd. The default behaviour of dhcpcd is to change the hostname only if it is localhost or localhost.localdomain. Before this change, the default hostname was localhost when networking.hostName was explicitly set to an empty string but now it is nixos so dhcpcd doesn't set the hostname anymore. To me this can have large consequences for users of NixOS in cloud environments or with network boot.

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

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants