Nix Language Quirks: Difference between revisions
→Mimicking case statements with attribute sets: remove typo from example |
m propose merge |
||
| (2 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
{{merge|Nix | {{merge|Nix (language)}} | ||
== <code>with</code> and <code>let</code> == | == <code>with</code> and <code>let</code> == | ||
| Line 36: | Line 36: | ||
Note, that it isn't equivalent to <code>with rec { x = 1; y = x + 1; body = y; }; body</code> because of mentioned <code>with</code> and <code>let</code> quirk, but is same as <code>rec { x = 1; y = x + 1; body = y; }.body</code> | Note, that it isn't equivalent to <code>with rec { x = 1; y = x + 1; body = y; }; body</code> because of mentioned <code>with</code> and <code>let</code> quirk, but is same as <code>rec { x = 1; y = x + 1; body = y; }.body</code> | ||
== Default values are not bound in @ syntax == | == Default values are not bound in <code>@</code> syntax == | ||
Destructured arguments can have default values, but those default values are part of the full function argument. | Destructured arguments can have default values, but those default values are part of the full function argument. | ||
| Line 87: | Line 87: | ||
consider the use of <code>import</code> here similar to using <code>qualified import ...</code> in Haskell or <code>import ...</code> in Python. Another (discouraged and increasingly uncommon) way of importing is [https://nix.dev/manual/nix/2.24/language/syntax#with-expressions <code>with import ...;</code>], which corresponds to Python <code>from ... import *</code>. This use of <code>with</code> imports everything from the target into scope, which has numerous potential gotchas and problems, and so using [https://nix.dev/guides/best-practices#with-scopes <code>inherit</code>] instead is encouraged and preferred. | consider the use of <code>import</code> here similar to using <code>qualified import ...</code> in Haskell or <code>import ...</code> in Python. Another (discouraged and increasingly uncommon) way of importing is [https://nix.dev/manual/nix/2.24/language/syntax#with-expressions <code>with import ...;</code>], which corresponds to Python <code>from ... import *</code>. This use of <code>with</code> imports everything from the target into scope, which has numerous potential gotchas and problems, and so using [https://nix.dev/guides/best-practices#with-scopes <code>inherit</code>] instead is encouraged and preferred. | ||
== builtins.replaceStrings key match on "" == | == <code>builtins.replaceStrings</code> key match on "" == | ||
Syntax: | Syntax: | ||
| Line 246: | Line 246: | ||
There is an example in <code>coq</code> package code [https://github.com/NixOS/nixpkgs/blob/5185539c51ba658e70b29e01c0c320a85f4e2098/pkgs/build-support/coq/extra-lib.nix#L98 here] where someone used this behavior to build a reusable function <code>switch</code>. | There is an example in <code>coq</code> package code [https://github.com/NixOS/nixpkgs/blob/5185539c51ba658e70b29e01c0c320a85f4e2098/pkgs/build-support/coq/extra-lib.nix#L98 here] where someone used this behavior to build a reusable function <code>switch</code>. | ||
== <code>builtins.toString</code> handling of <code>true</code> and <code>false</code> is inconsistent == | |||
<syntaxHighlight lang=nix> | |||
nix-repl> builtins.toString true | |||
"1" | |||
nix-repl> builtins.toString false | |||
"" | |||
</syntaxHighlight> | |||
== Nix Language FAQ == | == Nix Language FAQ == | ||
| Line 280: | Line 290: | ||
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) | ||
"id function was called"</syntaxHighlight> | "id function was called"</syntaxHighlight> | ||
= Resources = | |||
* [https://md.darmstadt.ccc.de/xtNP7JuIQ5iNW1FjuhUccw# A separately maintained list of Nix language quirks] | |||
[[Category:Nix Language]] | [[Category:Nix Language]] | ||