[Nix-dev] Variable Assignment in Nix Expressions

Eelco Dolstra eelco.dolstra at logicblox.com
Mon Aug 18 11:52:07 CEST 2014


Hi,

On 18/08/14 11:21, Roger Qiu wrote:

> How come the nix-repl allows a freestanding variable assignment, but the 
> actual Nix expressions don't?

Because it's a special feature of nix-repl. It allows assigning the result of an
expression to a variable which is then added to nix-repl's scope. (You can even
say "x = x + 1".)

> Apparently this has something to do with the purity and lambda calculus. 
> Assigning a variable is basically passing this variable into the 
> function scope of the body of code (which is done in Nix as let + {} 
> attributes). But wouldn't it be easier if we could have simpler variable 
> assignment semantics and allow a straightforward x = 5 in a function 
> body or outside of the function body without having to explicitly 
> pushing it into a set? Even Haskell and Erlang allow you to define free 
> standing variables.

The only difference with Haskell is that Nix doesn't have a notion of a module,
so the only way to return multiple things from a Nix expression is to return a
set.  So instead of

  module Foo where
  x = 1

you have to say

  { x = 1; }

-- 
Eelco Dolstra | LogicBlox, Inc. | http://nixos.org/~eelco/


More information about the nix-dev mailing list