1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2024-10-18 14:32:45 -04:00
nix/doc/manual/source/language/scope.md
John Ericson eb7d7780b1 Rename doc/manual{src -> source}
This is needed to avoid this
https://github.com/mesonbuild/meson/issues/13774 when we go back to
making our subproject directory `src`.
2024-10-14 11:21:24 -04:00

1.7 KiB

Scoping rules

A scope in the Nix language is a dictionary keyed by name, mapping each name to an expression and a definition type. The definition type is either explicit or implicit. Each entry in this dictionary is a definition.

Explicit definitions are created by the following expressions:

Implicit definitions are only created by with-expressions.

Every expression is enclosed by a scope. The outermost expression is enclosed by the built-in, global scope, which contains only explicit definitions. The expressions listed above extend their enclosing scope by adding new definitions, or replacing existing ones with the same name. An explicit definition can replace a definition of any type; an implicit definition can only replace another implicit definition.

Each of the above expressions defines which of its subexpressions are enclosed by the extended scope. In all other cases, the same scope that encloses an expression is the enclosing scope for its subexpressions.

The Nix language is statically scoped; the value of a variable is determined only by the variable's enclosing scope, and not by the dynamic context in which the variable is evaluated.

Note

Expressions entered into the Nix REPL are enclosed by a scope that can be extended by command line arguments or previous REPL commands. These ways of extending scope are not, strictly speaking, part of the Nix language.