3
0
Fork 0
forked from mirrors/nixpkgs

nixos/lib.escapeSystemdPath: Implement the correct algorithm for escaping names in systemd units

Co-authored-by: ajs124 <git@ajs124.de>
This commit is contained in:
Daniel Olsen 2022-06-11 11:18:54 +02:00
parent 4284ac9dfb
commit 3251123a77

View file

@ -39,11 +39,19 @@ rec {
|| hasPrefix a'.mountPoint b'.mountPoint || hasPrefix a'.mountPoint b'.mountPoint
|| any (hasPrefix a'.mountPoint) b'.depends; || any (hasPrefix a'.mountPoint) b'.depends;
# Escape a path according to the systemd rules, e.g. /dev/xyzzy # Escape a path according to the systemd rules. FIXME: slow
# becomes dev-xyzzy. FIXME: slow. # The rules are described in systemd.unit(5) as follows:
escapeSystemdPath = s: # The escaping algorithm operates as follows: given a string, any "/" character is replaced by "-", and all other characters which are not ASCII alphanumerics, ":", "_" or "." are replaced by C-style "\x2d" escapes. In addition, "." is replaced with such a C-style escape when it would appear as the first character in the escaped string.
replaceChars ["/" "-" " "] ["-" "\\x2d" "\\x20"] # When the input qualifies as absolute file system path, this algorithm is extended slightly: the path to the root directory "/" is encoded as single dash "-". In addition, any leading, trailing or duplicate "/" characters are removed from the string before transformation. Example: /foo//bar/baz/ becomes "foo-bar-baz".
(removePrefix "/" s); escapeSystemdPath = s: let
replacePrefix = p: r: s: (if (hasPrefix p s) then r + (removePrefix p s) else s);
trim = s: removeSuffix "/" (removePrefix "/" s);
normalizedPath = strings.normalizePath s;
in
replaceChars ["/"] ["-"]
(replacePrefix "." (strings.escapeC ["."] ".")
(strings.escapeC (stringToCharacters " !\"#$%&'()*+,;<=>=@[\\]^`{|}~-")
(if normalizedPath == "/" then normalizedPath else trim normalizedPath)));
# Quotes an argument for use in Exec* service lines. # Quotes an argument for use in Exec* service lines.
# systemd accepts "-quoted strings with escape sequences, toJSON produces # systemd accepts "-quoted strings with escape sequences, toJSON produces