Nix Language - Expressions

Let-expressions

let-in: let [ identifier = expr; ]… in expr

let x = 1; y = 2; in x + y

Inheriting attributes

inherit pkgs src; inherit (pkgs) src pkg1 pkg2 pkg3; Adds the variables to the current scope (attribute set or let bindings).

let
  x = { a = 1; b = 2; };
  inherit (builtins) attrNames;
in
{
  names = attrNames x;
}

let
  x = { a = 1; b = 2; };
in
{
  names = builtins.attrNames x;
}

With-expressions

with e1; e2 with builtins; head [ 1 2 3 ] with (import ./def.nix); …

let a = 3; in with { a = 1; }; let a = 4; in with { a = 2; }; a
let a = 3; in with { a = 1; }; let a = 4; in with { a = 2; }; a

Conditionals

if 1 + 1 == 2 then “yes!” else “no!

Assertions

assert 1+1==2; “yes!”

{ localServer ? false
, httpServer ? false
, sslSupport ? false
, pythonBindings ? false
, javaSwigBindings ? false
, javahlBindings ? false
, stdenv, fetchurl
, openssl ? null, httpd ? null, db4 ? null, expat, swig ? null, j2sdk ? null
}:

assert localServer -> db4 != null; ①
assert httpServer -> httpd != null && httpd.expat == expat; ②
assert sslSupport -> openssl != null && (httpServer -> httpd.openssl == openssl); ③
assert pythonBindings -> swig != null && swig.pythonSupport;
assert javaSwigBindings -> swig != null && swig.javaSupport;
assert javahlBindings -> j2sdk != null;

stdenv.mkDerivation {
  name = "subversion-1.1.1";
  ...
  openssl = if sslSupport then openssl else null; ④
  ...
}