How to avoid new line in Nix Variable

Hi All, I have very basic question.

I was trying to use long paragraph text but somehow Nix adds newline, anyway to avoid it?

Example:

let longText = 
  ''
    This is first line with long text. 
    This is continuation of first line with long text. 
    This still need to be continued in one line with long text. 
  '';
in
  "${longText}"

Actual Output:
“This is first line with long text. \nThis is continuation of first line with long text. \nThis still need to be continued in one line with long text. \n”

Expected Output:
“This is first line with long text. This is continuation of first line with long text. This still need to be continued in one line with long text.”

1 Like

This is quite simple: Just don’t use a newline in the string.

Thanks @NobbZ for your quick response. I am aware about not using newline in string. For my case its will be huge text and for better file readability any special character that I can use to mark text continuation and don’t add extra newline?

In that case using lib.concatStrings is probably your best bet:

let longText = lib.concatStrings [
    "This is first line with long text. "
    "This is continuation of first line with long text. "
    "This still need to be continued in one line with long text. ";
];
in "${longText}"

There is also lib.concatStringsSep which you can use to “inject” the interleaving space ( ).

3 Likes

Works perfect @NobbZ , appreciate your help. :slightly_smiling_face:

Another possible solution would be to substitute newline characters with spaces in longText using lib.replaceStrings.

1 Like

Thanks @Atemu for your suggestion.

1 Like