Nix lang: short syntax to add element to a list

There’s this syntax trap that might confuse people. When I was doing a Nix talk (sorry no recording) and I showed them this slide, someone asked: How is that purely functional? The code in question is here for convenience:

{
  a = 1; 
  b = {
    x = 0;
  };
}

Turns out at this point in time, when people see {} and a = 1;, they automatically think it’s a block with an assignment expression. It’s not! This is actually a complex literal equivalent to this JSON:

{
  "a": 1,
  "b": {
    "x": 0
  }
}

And you can abbreviate it to:

{
  a = 1; 
  b.x = 0;
}

If you say b.x = 0; and b.y = 1;, they will ‘merge’ into a single b, but you can’t say b.x twice just like you can’t say a twice. NixOS modules merging the configurations is a completely different mechanism at play.

nix repl is a good place to play with the syntax of the language

1 Like