Need help understanding how to escape special characters in the list of str type

Hello everyone,

I’m trying to construct config for pleroma-otp package, that recently appeared in unstable branch.

There’s an option, called configs, which according to the module in nixpkgs github repository(https://github.com/NixOS/nixpkgs/blob/25d09bddd609811ef0072129455469cd1cc8f256/nixos/modules/services/networking/pleroma.nix) is type of list of strings

According to Nix Expression Language manual(Overview of the Nix Language - NixOS Wiki) list is surrounded with [ and ]. I’m trying to get entire config from here https://git.pleroma.social/pleroma/pleroma/-/blob/develop/config/config.exs as one string in this list.
That’s why, I build my list this way:

config = [
''
<code goes here>
''
];

But I get:

error: undefined variable 'ref' at /etc/nixos/social/pleroma.nix:679:77
(use '--show-trace' to show detailed location information)

on this line:

 "https://git.pleroma.social/lambadalambda/kenoma/-/jobs/artifacts/${ref}/download?job=build",
      "ref" => "master"

I clearly understand, that this text has to be correctly escaped because "ref" and operator => are being treated as two variables comparison operation.

Please suggest, how to escape all possible special characters and treat all data as regular text that means nothing.

Thanks

1 Like

Escaping rules are described in the Nix manual. For '' strings:

Since ${ and '' have special meaning in indented strings, you need a way to quote them. $ can be escaped by prefixing it with '' (that is, two single quotes), i.e., ''$ . '' can be escaped by prefixing it with ' , i.e., ''' . $ removes any special meaning from the following $ . Linefeed, carriage-return and tab characters can be written as ''\n , ''\r , ''\t , and ''\ escapes any other character.

You should replace ${ref} with ''${ref} in your example, so that it does not try to interpolate a Nix variable ref. The sequence => is not special. Unfortunately the escaping rules are different from double-quote " strings, where \ is used to escape ${ instead.

10 Likes

Is it possible to include contents from file in the element of the list in some way?

For example, something like this:

config = [
include ./config.exs
]

Because my config is pretty big and there will be a lot of characters to escape

As long as it resolves to a string on eval you can put any expression in parens, and you can import external text files like so:

[
  (builtins.readFile ./config.exs)
  # or using interpolation to blend strings with code
  ''
    some import string content
    ${lib.fileContents ./other.txt}
  ''
]

builtins.readFile adds an extra newline at the end of the file, if you need to avoid this you can use lib.fileContents from nixpkgs instead.

2 Likes