nixos/systemd-lib: fix building of empty unit files

This is a fixup for c1ae82f448.

nix' `passAsFile` does not create empty files for variables that are
`null`.

This results in the following error for units that have no overrides or
content, but are, e.g. `wantedBy`:
`mv: cannot stat '': No such file or directory`.

Minimal reproducer:
`systemd.units.empty.wantedBy = [ "multi-user.target" ];`

This is often necessary when a unit is loaded in via `systemd.packages`.
This commit is contained in:
emilylange 2023-11-01 14:16:59 +01:00
parent 90b0f71e5d
commit 6c7ad5e732
No known key found for this signature in database
GPG Key ID: 0AD773CE46FD0F87

View File

@ -20,7 +20,10 @@ in rec {
pkgs.runCommand "unit-${mkPathSafeName name}"
{ preferLocalBuild = true;
allowSubstitutes = false;
inherit (unit) text;
# unit.text can be null. But variables that are null listed in
# passAsFile are ignored by nix, resulting in no file being created,
# making the mv operation fail.
text = optionalString (unit.text != null) unit.text;
passAsFile = [ "text" ];
}
''