Nix Language

#nix #nixos #language #programming #functional

  • Nix expression: a piece of code evaluated that produce a nix value
  • A nix file is a nix expression
  • We can evaluate nix expression using nix repl
  • To exit nix repl you can press :q
  • Break lines, indentation, spaces is only to the code be more readable
  • Nix Function only has one argument and are separated by :
  • Functions Libraries are the standard libraries of language, containing pkgs.lib, builtins and import
  • The only impurities on the language are the Build Inputs, that are composed of file system.
  • Nix reference files using the content hash. If It does not know its content, it read the file during the evaluation of the derivations
  • [eaf9g-nix-let-in]]# are expression on on language that access variable inside itself, or a way to assign variable recursively
  • There is something similar to Nix: let…in is the Attribute Set with rec propriety. The variable can be used in the same scope it was defined.
  • Nix language can use variables on the code using String Interpolation. It is possible to escape them to no make confuse with bash variables.
  • Inherit is a way to assign variable on a nested scope.
nix-repl > 1+2
3
nix-repl > { a.b.c = 1; }
{ a = {...}; }
nix-repl > :p { a.b.c = 1;}
{ a = { b = {c 1;};};}

To evaluate one nix file:

$ echo 1+2 > default.nix
$ nix-instantiate --eval
3
Links to this page
#nix #nixos #language #programming #functional