From 222ed18540ff853dcf8df329fc0f247e7252a8c5 Mon Sep 17 00:00:00 2001 From: sternenseemann Date: Fri, 1 May 2026 18:09:28 +0200 Subject: [PATCH] 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 '\\' ( , , > , and , 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. --- nixos/lib/systemd-lib.nix | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nixos/lib/systemd-lib.nix b/nixos/lib/systemd-lib.nix index 13c529d53619..5c974f41dda5 100644 --- a/nixos/lib/systemd-lib.nix +++ b/nixos/lib/systemd-lib.nix @@ -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: