Nix Language Quirks: Difference between revisions

From NixOS Wiki
imported>Danbst
added let and with
 
imported>Danbst
added old let syntax
Line 21: Line 21:


[https://github.com/NixOS/nix/issues/1361 Good discussion on this topic]
[https://github.com/NixOS/nix/issues/1361 Good discussion on this topic]
== Old <code>let</code> syntax ==
This is an old Nix syntax, that probably isn't used much
<nowiki>
nix-repl> let { x = 1; y = x + 1; body = y; }
2</nowiki>
It is equivalent to modern syntax expression <code>let x = 1; y = x + 1; in y</code>. Note, that it doesn't require <code>rec</code> keyword.
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>

Revision as of 09:43, 26 October 2017

with and let

with gets less priority then let. This can lead to confusions, especially if you like to write with pkgs;:

nix-repl> pkgs = { x = 1; }

nix-repl> with pkgs; x
1

nix-repl> with pkgs; let x = 2; in x
2

So we see, that let binding overrides with binding. But what about this?

nix-repl> let x = 2; in with pkgs; x
2

Nah, with and let have different priority when resolving names.

Good discussion on this topic

Old let syntax

This is an old Nix syntax, that probably isn't used much

nix-repl> let { x = 1; y = x + 1; body = y; }
2

It is equivalent to modern syntax expression let x = 1; y = x + 1; in y. Note, that it doesn't require rec keyword.

Note, that it isn't equivalent to with rec { x = 1; y = x + 1; body = y; }; body because of mentioned with and let quirk, but is same as rec { x = 1; y = x + 1; body = y; }.body