Avoid string interpolation nix

I am trying to configure nvim-dap in neovim with home manager. How can I have cwd be '${workspaceFolder}' when the init.lua file is created, and not nix trying to find a variable named workspaceFolder?

{
  plugin = nvim-dap;
  type = "lua";
  config = ''
    local dap = require('dap')

    dap.adapters.codelldb = {
      type = 'server',
      port = '13000',
      host = '127.0.0.1',
      executable = {
        command = '${pkgs.vscode-extensions.vadimcn.vscode-lldb}/share/vscode/extensions/vadimcn.vscode.lldb/adapter/codelldb',
        args = { '--port', '13000' },
      },
    }

    dap.configurations.c = {
      {
        name = 'Launch',
        type = 'codelldb',
        request = 'launch',
        program = function()
          return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file')
        end,
        cwd = '${workspaceFolder}',
        stopOnEntry = false,
        args = {},
      },
    }
  '';
}

You can do it in a multiline string like this:

let I_am_a_nix_variable = "hello"; in ''
  ${I_am_a_nix_variable}
  ''${I_am_not_a_nix_variable}
''

will evaluate to

hello
${I_am_not_a_nix_variable}

When using double-quoted strings, you have to use "\${I_am_not_a_nix_variable}"

2 Likes

Thank you, but how would I change this line: cwd = '${workspaceFolder}'?

It is as Ma27 said: if unclear refer to String interpolation - Nix Reference Manual

Thank you for the answer, but this did not work. Changing the line to cwd = "''${workspaceFolder}" does work however. If I try your suggestion, I get the following error:

error:
       … while calling the 'head' builtin

         at /nix/store/fs1ai4bxgp47vxx7zn0b7cr1ssz7ghws-source/lib/attrsets.nix:784:11:

          783|         || pred here (elemAt values 1) (head values) then
          784|           head values
             |           ^
          785|         else

       … while evaluating the attribute 'value'

         at /nix/store/fs1ai4bxgp47vxx7zn0b7cr1ssz7ghws-source/lib/modules.nix:768:9:

          767|     in warnDeprecation opt //
          768|       { value = builtins.addErrorContext "while evaluating the option `${showOption loc}':" value;
             |         ^
          769|         inherit (res.defsFinal') highestPrio;

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: undefined variable 'workspaceFolder'

       at /nix/store/mk49j2pywf5fr21qi22rsnwyg3rx0y9z-source/home/neovim/default.nix:182:26:

          181|               end,
          182|               cwd = '''${workspaceFolder}',
             |                          ^
          183|               stopOnEntry = false,

''' inside a multiline string gives you '':

nix-repl> '' ''' ''
"'' "

That means '''${foo} evaluates to ''<whatever "foo" is>.

This isn’t even a valid Nix string. The two string delimiters are " and ''. A single ' is not a string delimiter in Nix. In " strings you can escape a ${ with a \, e.g. \${, and as already mentioned in string literals starting with '' you can use ''${.

1 Like

That link is a 404 and moved to String interpolation - Nix Reference Manual

Unfortunately it doesn’t explain anything about this.

Thanks for the update. The broken link is a temporary outage (caused and to be cleaned up by yours truly), URLs should stay valid indefinitely.

The relevant bit is currently in the documentation on the string data type: Data Types - Nix Reference Manual

It’s not a good place, and I simply didn’t get around to moving it yet. PRs very much appreciated. If you don’t know where to start, I uploaded a video tutorial for the first steps: https://m.youtube.com/watch?v=8ogaUFuAkCY

2 Likes

Ooh, is there a coordination process for revisions to the reference manual, or should one just show up with PRs?

For such small things, just make PRs and make sure I’m set as a reviewer, then I’ll notice them and get them merged. For larger endeavors, make an issue and ping me for discussion.

@fricklerhandwerk thanks for the help. I gave it a try in string interpolation escape example by alicebob · Pull Request #10966 · NixOS/nix · GitHub

3 Likes

Thanks a lot for the contribution! And the broken links should (now finally?) be fixed by fix more broken URLs by fricklerhandwerk · Pull Request #1486 · NixOS/nixos-homepage · GitHub

1 Like

Unfortunately not. The link from bwolf, above, is https://nixos.org/manual/nix/stable/language/string-interpolation.html which redirects to https://nix.dev/manual/nix/2.22/stable/language/string-interpolation.html, which is a 404: it has a “/stable” too many.

edit: hadn’t seen it isn’t deployed yet, sorry :slight_smile: