Commit 222ed185 authored by sternenseemann's avatar sternenseemann
Browse files

nixos/lib/systemd-lib: make unitNameType work with e.g. Rust regex

The unitNameType regex currently makes the Tvix CI (and likely Snix
in the future) fail since "sysroot-nix-.ro\\x2dstore.mount" will
fail the check since it doesn't interpret the single backslash
as part of the bracket expression.

POSIX doesn't require escaping the backslash in bracket exprs:
> The special characters '.', '*', '[', and '\\' ( <period>, <asterisk>,
> <left-square-bracket>, and <backslash>, respectively) shall lose their
> special meaning within a bracket expression.

However, Rust uses the backslash for escaping in bracket exprs,
so it also needs to be escaped:
> [\[\]]        Escaping in character classes (matching [ or ])

Making the Regex work with both POSIX-like regexes and Rust's regex
syntax is possible in this case, so let's do it.
parent a7dcc5e9
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -67,7 +67,9 @@ rec {
  mkPathSafeName = replaceStrings [ "@" ":" "\\" "[" "]" ] [ "-" "-" "-" "" "" ];

  # a type for options that take a unit name
  unitNameType = types.strMatching "[a-zA-Z0-9@%:_.\\-]+[.](service|socket|device|mount|automount|swap|target|path|timer|scope|slice)";
  # note: redundantly escaping backslash in the bracket expression makes the regex
  # slightly more portable even though POSIX doesn't require it.
  unitNameType = types.strMatching "[a-zA-Z0-9@%:_.\\\\-]+[.](service|socket|device|mount|automount|swap|target|path|timer|scope|slice)";

  makeUnit =
    name: unit: