Home Explore Blog CI



nix

1st chunk of `doc/manual/source/language/evaluation.md`
8c0f3bc27b887d7366ce58f3abab714efde4ade316a2935a0000000100000981
# Evaluation

Evaluation is the process of turning a Nix expression into a [Nix value](types.md).

This happens by a number of rules, such as:
- Constructing values from literals. 
  For example the number literal `1` is turned into the number value `1`.
- Applying operators
  For example the addition operator `+` is applied to two number values to produce a new number value.
- Applying built-in functions
  For example the expression `builtins.isInt 1` is evaluated to `true`.
- Applying user-defined functions
  For example the expression `(x: x + 1) 10` can[*](#laziness) be thought of rewriting `x` in the function body to the argument, `10 + 1`, which is then evaluated to `11`.

These rules are applied as needed, driven by the specific use of the expression. For example, this can occur in the Nix command line interface or interactively with the [repl (read-eval-print loop)](@docroot@/command-ref/new-cli/nix3-repl.md), which is a useful tool when learning about evaluation.

# Details

## Values {#values}

Nix values can be thought of as a subset of Nix expressions.
For example, the expression `1 + 2` is not a value, because it can be reduced to `3`. The expression `3` is a value, because it cannot be reduced any further.

Evaluation normally happens by applying rules to the "head" of the expression, which is the outermost part of the expression. The head of an expression like `[ 1 2 ]` is the list literal (`[ a1 a2 ]`), for `1 + 2` it is the addition operator (`+`), and for `f 1` it is the function application "operator" (` `).

After applying all possible rules to the head until no rules can be applied, the expression is in "weak head normal form" (WHNF). This means that the outermost constructor of the expression is evaluated, but the inner values may or may not be. "Weak" only signifies that the expression may be a function. This is an historical or academic artifact, and Nix has no use for the non-weak "head normal form".

## Laziness and thunks {#laziness}

The Nix language implements _call by need_ (as opposed to _call by value_ or _call by reference_). <!-- No wikipedia link, which would be a huge distraction. --> Call by need is commonly known as laziness in functional programming, as it is a specific implementation of the concept where evaluation is deferred until the result is required, aiming to only evaluate the parts of an expression that are needed to produce the final result.

Title: Nix Expression Evaluation, Values, and Laziness
Summary
This section describes the process of evaluating Nix expressions into Nix values using rules like literal construction, operator application, and function application. Nix values are irreducible expressions. Evaluation occurs by applying rules to the head of the expression until it's in weak head normal form (WHNF). Nix uses call-by-need (laziness), deferring evaluation until the result is needed, optimizing for only evaluating necessary parts.