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
This commit is contained in:
Steven Allen
2026-03-10 10:02:38 -07:00
parent a1f063abde
commit 55a73b26e9
+23 -2
View File
@@ -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 = {