summary refs log tree commit diff
path: root/lib/types.nix
diff options
context:
space:
mode:
authorJules Aguillon <jules@j3s.fr>2022-01-20 18:49:54 +0100
committerJules Aguillon <jules@j3s.fr>2022-01-20 18:49:54 +0100
commit4baf8548fbf9957b53418e0aad06bd6a798c283e (patch)
tree1d4843631527de72f1aa1e66dfd8747fc29b1d8e /lib/types.nix
parentf25a13212be9bd716db9420a59cb74ecab02937a (diff)
downloadnixpkgs-4baf8548fbf9957b53418e0aad06bd6a798c283e.tar
nixpkgs-4baf8548fbf9957b53418e0aad06bd6a798c283e.tar.gz
nixpkgs-4baf8548fbf9957b53418e0aad06bd6a798c283e.tar.bz2
nixpkgs-4baf8548fbf9957b53418e0aad06bd6a798c283e.tar.lz
nixpkgs-4baf8548fbf9957b53418e0aad06bd6a798c283e.tar.xz
nixpkgs-4baf8548fbf9957b53418e0aad06bd6a798c283e.tar.zst
nixpkgs-4baf8548fbf9957b53418e0aad06bd6a798c283e.zip
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).
Diffstat (limited to 'lib/types.nix')
-rw-r--r--lib/types.nix17
1 files changed, 12 insertions, 5 deletions
diff --git a/lib/types.nix b/lib/types.nix
index 18e95caaee8..7acfa60f161 100644
--- a/lib/types.nix
+++ b/lib/types.nix
@@ -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}";