From 55a73b26e9529f10e81d180e2d6d12e6622c6b63 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Mon, 9 Mar 2026 12:15:19 -0700 Subject: [PATCH] nixos/power-management: ensure that the power management scripts are never empty When a service's `script` attribute is the empty string, the systemd module treats it as "unset". This leads to services without `ExecStart` directives, which fail to validate. We fix this by ensuring that we always have something in these scripts, even if that's only whitespace and comments. Alternatives considered: 1. Removing the respective systemd units (with mkIf) when there's nothing to do can lead to recursive evaluation errors if a config modifies any of these power-management commands based on the presence of a systemd unit. 2. Setting `enabled = false` on units with empty scripts works, but enabled is primarily designed for masking systemd units which isn't what we're trying to do here. 3. We could, e.g., set ExecStart to the `true` command instead of an empty script, but that adds complexity for a negligible performance gain. fixes #498310 --- nixos/modules/config/power-management.nix | 25 +++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/nixos/modules/config/power-management.nix b/nixos/modules/config/power-management.nix index 47b3ad908f0a..202ecdb7cffb 100644 --- a/nixos/modules/config/power-management.nix +++ b/nixos/modules/config/power-management.nix @@ -104,7 +104,12 @@ in description = "Pre-Sleep Actions"; wantedBy = [ "sleep.target" ]; before = [ "sleep.target" ]; - script = cfg.powerDownCommands; + script = '' + # NixOS pre-sleep script + + # config.powerManagement.powerDownCommands + ${cfg.powerDownCommands} + ''; serviceConfig.Type = "oneshot"; }; @@ -114,8 +119,14 @@ in # Pulled in by post-resume.service above after = [ "sleep.target" ]; script = '' + # NixOS pre-resume script + /run/current-system/systemd/bin/systemctl try-restart --no-block post-resume.target + + # config.powerManagement.resumeCommands ${cfg.resumeCommands} + + # config.powerManagement.powerUpCommands ${cfg.powerUpCommands} ''; serviceConfig.Type = "oneshot"; @@ -130,7 +141,12 @@ in before = [ "shutdown.target" ]; - script = cfg.powerDownCommands; + script = '' + # NixOS pre-shutdown script + + # config.powerManagement.powerDownCommands + ${cfg.powerDownCommands} + ''; serviceConfig.Type = "oneshot"; unitConfig.DefaultDependencies = false; }; @@ -143,7 +159,12 @@ in wantedBy = [ "multi-user.target" ]; restartIfChanged = false; script = '' + # NixOS post-boot script + + # config.powerManagement.bootCommands ${cfg.bootCommands} + + # config.powerManagement.powerUpCommands ${cfg.powerUpCommands} ''; serviceConfig = {