From 9828446608cfc3c184ee030fcdf6d2092d22d2fb Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Sun, 20 Mar 2022 21:04:53 -0400 Subject: [PATCH] systemd-initrd: Fix Environment= and PATH --- nixos/lib/systemd-lib.nix | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/nixos/lib/systemd-lib.nix b/nixos/lib/systemd-lib.nix index 3129fbe7bdb9..75f1d83c2cfd 100644 --- a/nixos/lib/systemd-lib.nix +++ b/nixos/lib/systemd-lib.nix @@ -295,7 +295,7 @@ in rec { config = mkMerge [ { # Default path for systemd services. Should be quite minimal. path = mkAfter path; - environment.PATH = "${makeBinPath config.path}:${makeSearchPathOutput "bin" "sbin" config.path}"; + environment.PATH = mkIf (config.path != []) "${makeBinPath config.path}:${makeSearchPathOutput "bin" "sbin" config.path}"; } (mkIf (config.preStart != "") { serviceConfig.ExecStartPre = @@ -396,6 +396,13 @@ in rec { text = commonUnitText def + '' [Service] + ${let env = def.environment; + in concatMapStrings (n: + let s = optionalString (env.${n} != null) + "Environment=${builtins.toJSON "${n}=${env.${n}}"}\n"; + # systemd max line length is now 1MiB + # https://github.com/systemd/systemd/commit/e6dde451a51dc5aaa7f4d98d39b8fe735f73d2af + in if stringLength s >= 1048576 then throw "The value of the environment variable ‘${n}’ in systemd service ‘${name}.service’ is too long." else s) (attrNames env)} ${attrsToSection def.serviceConfig} ''; };