diff --git a/nixos/modules/system/boot/systemd.nix b/nixos/modules/system/boot/systemd.nix index 4b7bb1271392..8bfd2a784f47 100644 --- a/nixos/modules/system/boot/systemd.nix +++ b/nixos/modules/system/boot/systemd.nix @@ -807,6 +807,31 @@ in # Don't bother with certain units in containers. systemd.services.systemd-remount-fs.unitConfig.ConditionVirtualization = "!container"; + # When using the classic /etc mechanism, we set certain paths in /etc to + # /etc/static so that systemd cannot change them (as they are symlinks to + # the read-only Nix Store). This is only done so that these services cannot + # change the values. All other parts of systemd should read them from their + # canonical locations. + # + # If you use the overlay mechanism to manage /etc, this is unnecessary + # because either the overlay is mutable (and users can legitimately change + # values without them being overridden) or it is immutable and systemd will + # suggest to only make runtime changes. + systemd.services."systemd-localed".environment = lib.mkIf (!config.system.etc.overlay.enable) { + SYSTEMD_ETC_LOCALE_CONF = "/etc/static/locale.conf"; + SYSTEMD_ETC_VCONSOLE_CONF = "/etc/static/vconsole.conf"; + }; + systemd.services."systemd-timedated".environment = + lib.mkIf (!config.system.etc.overlay.enable && config.time.timeZone != null) + { + SYSTEMD_ETC_LOCALTIME = "/etc/static/localtime"; + SYSTEMD_ETC_ADJTIME = "/etc/static/adjtime"; + }; + systemd.services."systemd-hostnamed".environment = lib.mkIf (!config.system.etc.overlay.enable) { + SYSTEMD_ETC_HOSTNAME = "/etc/static/hostname"; + SYSTEMD_ETC_MACHINE_INFO = "/etc/static/machine-info"; + }; + # Increase numeric PID range (set directly instead of copying a one-line file from systemd) # https://github.com/systemd/systemd/pull/12226 boot.kernel.sysctl."kernel.pid_max" = mkIf pkgs.stdenv.hostPlatform.is64bit (lib.mkDefault 4194304);