Nix Language Quirks: Difference between revisions

imported>Makefu
<nowiki> -> <syntaxHighlight lang=nix>
imported>Makefu
add interpolation again
Line 90: Line 90:
error: attribute ‘x’ at (string):1:31 already defined at (string):1:24 </syntaxHighlight>
error: attribute ‘x’ at (string):1:31 already defined at (string):1:24 </syntaxHighlight>
This makes it a sane citizen of Nix lanugage... except it has a twin, called <code>{ inherit ...; }</code>. They DON'T do the same - <code>let inherit ...</code> adds let-bindings, and <code>{ inherit ...; }</code> adds attributes to a record.
This makes it a sane citizen of Nix lanugage... except it has a twin, called <code>{ inherit ...; }</code>. They DON'T do the same - <code>let inherit ...</code> adds let-bindings, and <code>{ inherit ...; }</code> adds attributes to a record.
 
== Nix Language FAQ ==
== Q: What is the shortest <code>id</code> function definition? ==
=== Q: What is the shortest <code>id</code> function definition? ===


A: <code>x: x</code>
A: <code>x: x</code>


Q: Why not <code>x:x</code>?
=== Q: Why not <code>x:x</code>? ===


A:
A:
Line 104: Line 104:
"string" </syntaxHighlight>
"string" </syntaxHighlight>
! [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
<syntaxHlighlight lang=nix>
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</syntaxHighlight>
=== Q: Can it be <code>eval</code>-ed from string? ===
A: Sure, but only via "import from derivation":
<syntaxHighlight lang=nix>
nix-repl> let code = "(x: x) ''id function was called''"; in import (builtins.toFile "eval" code)
"id function was called"</syntaxHighlight>