diff --git a/lib/default.nix b/lib/default.nix index 3eb2ecc308a4..de38409b8c67 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -331,6 +331,7 @@ let hasInfix hasPrefix hasSuffix + join stringToCharacters stringAsChars escape diff --git a/lib/strings.nix b/lib/strings.nix index 359e1a4cd8db..9e091d40b12e 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -41,6 +41,36 @@ rec { unsafeDiscardStringContext ; + /** + Concatenates a list of strings with a separator between each element. + + # Inputs + + `sep` + : Separator to add between elements + + `list` + : List of strings that will be joined + + # Type + + ``` + join :: string -> [ string ] -> string + ``` + + # Examples + :::{.example} + ## `lib.strings.join` usage example + + ```nix + join ", " ["foo" "bar"] + => "foo, bar" + ``` + + ::: + */ + join = builtins.concatStringsSep; + /** Concatenate a list of strings. diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index a9c78defdffb..e36e2d128d3d 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -69,6 +69,7 @@ let id ifilter0 isStorePath + join lazyDerivation length lists @@ -435,6 +436,15 @@ runTests { # STRINGS + testJoin = { + expr = join "," [ + "a" + "b" + "c" + ]; + expected = "a,b,c"; + }; + testConcatMapStrings = { expr = concatMapStrings (x: x + ";") [ "a"