[Nix-dev] Not understanding evaluation of with inside let
Layus
layus.on at gmail.com
Mon May 16 00:11:20 CEST 2016
Hi,
At first it seemed like a bug to me. I would not expected that behavior
at all.
This is all defined in [1]. The var is first looked up in the enclosing
environment.
When the environment search fails, then the enclosing "with" clauses are
looked up.
The manual says nothing about precedence:
A with-expression, `with e1; e2` introduces the set `e1` into the
lexical scope of the expression `e2`. [...]
So the set is available in the lexical scope of the expression, but with
lower precedence than the "real" environment.
I guess this makes sense in nix.
A with clause can import pretty big sets into scope, so if the variable
is already provided in the current scope, it is difficult to know if the
varible comes from the "normal" scope or if it is provided by the "with"
clause. It is even worse in the case of `with pkgs`, where packages can
come and go.
Just my two cents,
-- Layus.
[1]
https://github.com/NixOS/nix/blob/master/src/libexpr/nixexpr.cc#L246-L274
On 15/05/16 22:25, Harald van Dijk wrote:
> Hello list,
>
> I'm having trouble understanding the result of the following (reduced)
> evaluation:
>
> nix-instantiate --eval --expr 'let x = 1; y = with { x = 2; }; x; in y'
>
> I expect the last x to resolve to the one introduced by with, giving a
> result of 2. Instead, it resolves to the one introduced by let, giving a
> result of 1.
>
> I can trivially rewrite this to avoid the whole problem, but I'd like to
> understand the scope rules better. If this behaviour is intended, I
> can't tell from the documentation. It merely says the set e1 ({x = 2;})
> is introduced into the lexical scope of the expression e2 (x). It does
> not explicitly address what happens when the lexical scope of the
> expression e2 already contains those same keys, but if some elements of
> e1 are *not* introduced into the lexical scope, then I think the
> documentation is somewhat misleading when it says the whole set is
> introduced into the lexical scope.
>
> So what's going on here? Using Nix 1.11.2 if it matters.
>
> Cheers,
> Harald van Dijk
> _______________________________________________
> nix-dev mailing list
> nix-dev at lists.science.uu.nl
> http://lists.science.uu.nl/mailman/listinfo/nix-dev
More information about the nix-dev
mailing list