3
0
Fork 0
forked from mirrors/nixpkgs

types.singleLineStr: Allow and trim trailing \n

Allow a \n character at the end of the string and remove it during the
merge function.

An option of this type will resolve to the value "foo" whether it is set
to "foo" or "foo\n".

This is useful when using 'builtins.readFile' or ''-strings, which might
add an unintended newline (for example, bash trim the final newline from
a subshell).
This commit is contained in:
Jules Aguillon 2022-01-20 18:49:54 +01:00
parent f25a13212b
commit 4baf8548fb

View file

@ -300,11 +300,18 @@ rec {
inherit (str) merge;
};
singleLineStr = mkOptionType {
name = "singleLineStr";
description = "string that doesn't contain [\\n\\r]";
inherit (strMatching "[^\n\r]*") check merge;
};
# Allow a newline character at the end and trim it in the merge function.
singleLineStr =
let
inherit (strMatching "[^\n\r]*\n?") check merge;
in
mkOptionType {
name = "singleLineStr";
description = "string that doesn't contain [\\n\\r]";
inherit check;
merge = loc: defs:
lib.removeSuffix "\n" (merge loc defs);
};
strMatching = pattern: mkOptionType {
name = "strMatching ${escapeNixString pattern}";