summary refs log tree commit diff
path: root/nixos/lib
diff options
context:
space:
mode:
authorWill Fancher <elvishjerricco@gmail.com>2022-03-20 21:04:53 -0400
committerWill Fancher <elvishjerricco@gmail.com>2022-03-22 21:28:51 -0400
commit9828446608cfc3c184ee030fcdf6d2092d22d2fb (patch)
treefd761f92919cdb6c3c39c4ff64f02a414e102912 /nixos/lib
parent3365666840152b7b1ef2a97f171102575b6d743f (diff)
downloadnixpkgs-9828446608cfc3c184ee030fcdf6d2092d22d2fb.tar
nixpkgs-9828446608cfc3c184ee030fcdf6d2092d22d2fb.tar.gz
nixpkgs-9828446608cfc3c184ee030fcdf6d2092d22d2fb.tar.bz2
nixpkgs-9828446608cfc3c184ee030fcdf6d2092d22d2fb.tar.lz
nixpkgs-9828446608cfc3c184ee030fcdf6d2092d22d2fb.tar.xz
nixpkgs-9828446608cfc3c184ee030fcdf6d2092d22d2fb.tar.zst
nixpkgs-9828446608cfc3c184ee030fcdf6d2092d22d2fb.zip
systemd-initrd: Fix Environment= and PATH
Diffstat (limited to 'nixos/lib')
-rw-r--r--nixos/lib/systemd-lib.nix9
1 files changed, 8 insertions, 1 deletions
diff --git a/nixos/lib/systemd-lib.nix b/nixos/lib/systemd-lib.nix
index 3129fbe7bdb..75f1d83c2cf 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}
         '';
     };