From 7cc66df3ec13587facaf48fe7c6d122984a2d613 Mon Sep 17 00:00:00 2001 From: Eduard Bachmakov Date: Sat, 10 Aug 2024 11:16:25 +0200 Subject: [PATCH] nixos/systemd/initrd: Set /etc/hostname in the initrd. With the the Systemd-based initrd, systemd-journald is doing the logging. One of Journald's Trusted Journal Fields is `_HOSTNAME` (systemd.journal-fields(7)). Without explicitly setting the hostname via this file or the kernel cmdline, `localhost` is used and captured in the journal. As a result, a boot's log references multiple hostnames. With centralized log collection this breaks filtering (more so when logs from multiple Systemd-based initrds are streaming in simultaneously. Fixes #318907. --- nixos/modules/system/boot/systemd/initrd.nix | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nixos/modules/system/boot/systemd/initrd.nix b/nixos/modules/system/boot/systemd/initrd.nix index f2ffb9209719..957c2fa53fa9 100644 --- a/nixos/modules/system/boot/systemd/initrd.nix +++ b/nixos/modules/system/boot/systemd/initrd.nix @@ -160,7 +160,7 @@ in { description = "Set of files that have to be linked into the initrd"; example = literalExpression '' { - "/etc/hostname".text = "mymachine"; + "/etc/machine-id".source = /etc/machine-id; } ''; default = {}; @@ -444,6 +444,9 @@ in { "/etc/os-release".source = config.boot.initrd.osRelease; "/etc/initrd-release".source = config.boot.initrd.osRelease; + # For systemd-journald's _HOSTNAME field; needs to be set early, cannot be backfilled. + "/etc/hostname".text = config.networking.hostName; + } // optionalAttrs (config.environment.etc ? "modprobe.d/nixos.conf") { "/etc/modprobe.d/nixos.conf".source = config.environment.etc."modprobe.d/nixos.conf".source; };