Nix Language Quirks: Difference between revisions

imported>Danbst
added x:x example
imported>Danbst
added interpolation/eval examples
Line 104: Line 104:
"string"</nowiki>
"string"</nowiki>
! [https://github.com/NixOS/nix/issues/836 Can you figure out how can this happens before reading explanation?]
! [https://github.com/NixOS/nix/issues/836 Can you figure out how can this happens before reading explanation?]
== Q: Can Nix code be interpolated? ==
Yes, but not everywhere
<nowiki>
nix-repl> let ${"x"} = 2; in x
2
nix-repl> with { ${"x"} = 2; }; x
2
nix-repl> let x = 1; y = ${x}; in y
error: syntax error, unexpected DOLLAR_CURLY, at (string):1:16</nowiki>
Q: Can it be <code>eval</code>-ed from string?
A: Sure, but only via "import from derivation":
<nowiki>
nix-repl> let code = "(x: x) ''id function was called''"; in import (builtins.toFile "eval" code)
"id function was called"</nowiki>