Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Nix expression in regard to Nix package management?

Tags:

nix

Even after reading the Nix manuals it is still confusing what Nix expressions really are. Sometimes they are referred to as derivations, but store derivations also mean something else.

like image 346
toraritte Avatar asked Oct 04 '19 21:10

toraritte


1 Answers

In Nix, a Nix expression is just a general term for any type of value that you can write in the Nix language. A Nix expression can be a set, a list, a number, a string, a function, a name, a arithmetic operation, a function call, and much more.

Nix expressions can contain other Nix expressions: for example, the expression 1 + 2 contains two expressions inside it: 1 and 2.

People often like to write complicated Nix expressions that represent how to build a piece of software. Those expressions are really just sets with some special attributes. The Nix software can evaluate such an expression and turn it into a .drv file (a very simple, compact way of describing how to build some software), which can then be built.

You can do lots of things with the Nix language and Nix expressions that don't involve derivations or building software. The nix eval command lets you evaluate a Nix expression. Run nix eval --help to see its help screen, or run these commands to evaluate some simple expressions:

nix eval '(1 + 2)'  # gives 3

nix eval '({ a = 1; b = 2; }.a)'  # gives 1

(For some reason, this command seems to require parentheses to be put around most of the Nix expressions it evaluates, but that just seems like a bug or odd design choice, and surrounding parentheses are not an essential part of every Nix expression.)

like image 85
David Grayson Avatar answered Oct 25 '22 06:10

David Grayson