Nix Language Quirks: Difference between revisions

imported>Milahu
m fix whitespace
imported>Roberth
Correct and explain the most egregious stuff
Line 199: Line 199:
{{note|[https://github.com/NixOS/nix/commit/f90f660b243866b8860eeb24cc4a345d32cc7ce7 Nix 1.12 should fix this]}}
{{note|[https://github.com/NixOS/nix/commit/f90f660b243866b8860eeb24cc4a345d32cc7ce7 Nix 1.12 should fix this]}}
=== Q: Can Nix code be interpolated? ===
=== Q: Can Nix code be interpolated? ===
Yes, but not everywhere
No, only attribute names can.
  <syntaxHighlight lang=nix>
  <syntaxHighlight lang=nix>
nix-repl> let ${"x"} = 2; in x
nix-repl> let ${"x"} = 2; in x
Line 212: Line 212:
=== Q: Can it be <code>eval</code>-ed from string? ===
=== Q: Can it be <code>eval</code>-ed from string? ===


A: Sure, but only via "import from derivation":
A: Yes, but it is not recommended as "eval" is generally regarded as an easy to abuse language feature. It is possible but only via the store (not as bad as "import from derivation", but still not suitable for hot code paths):
  <syntaxHighlight lang=nix>
  <syntaxHighlight lang=nix>
nix-repl> let code = "(x: x) ''id function was called''"; in import (builtins.toFile "eval" code)
nix-repl> let code = "(x: x) ''id function was called''"; in import (builtins.toFile "eval" code)