Skip to content

Commit

Permalink
systemd: Properly escape environment options.
Browse files Browse the repository at this point in the history
Using toJSON on a string value works because the allowed JSON escape
sequences is almost a subset of the systemd allowed escape sequences.
The only exception is `\/` which JSON allows but systemd doesn't.
Luckily this sequence isn't required and toJSON don't produce it making
the result valid for systemd consumption.

Examples of things that this fixes are environment variables with double
quotes or newlines.
  • Loading branch information
kevincox authored and grahamc committed Feb 21, 2017
1 parent 85fe839 commit da33c8a
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion nixos/modules/system/boot/systemd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ let
${let env = cfg.globalEnvironment // def.environment;
in concatMapStrings (n:
let s = optionalString (env."${n}" != null)
"Environment=\"${n}=${env.${n}}\"\n";
"Environment=${builtins.toJSON "${n}=${env.${n}}"}\n";
in if stringLength s >= 2048 then throw "The value of the environment variable ‘${n}’ in systemd service ‘${name}.service’ is too long." else s) (attrNames env)}
${if def.reloadIfChanged then ''
X-ReloadIfChanged=true
Expand Down

0 comments on commit da33c8a

Please sign in to comment.