Architecture
Architecture
This chapter describes how Nix works. It should help users understand why Nix behaves as it does, and it should help developers understand how to modify Nix and how to write similar tools.
Overview
Nix consists of hierarchical layers.
The following concept map shows its main components (rectangles), the objects they operate on (rounded rectangles), and their interactions (connecting phrases):
.----------------.
| Nix expression |----------.
'----------------' |
| passed to
| |
+----------|-------------------|--------------------------------+
| Nix | V |
| | +-------------------------+ |
| | | commmand line interface |------. |
| | +-------------------------+ | |
| | | | |
| evaluated by calls manages |
| | | | |
| | V | |
| | +--------------------+ | |
| '-------->| language evaluator | | |
| +--------------------+ | |
| | | |
| produces | |
| | V |
| +----------------------------|------------------------------+ |
| | store | | |
| | referenced by V builds | |
| | .-------------. .------------. .--------------. | |
| | | build input |----->| build plan |----->| build result | | |
| | '-------------' '------------' '--------------' | |
| +-------------------------------------------------|---------+ |
+---------------------------------------------------|-----------+
|
represented as
|
V
.---------------.
| file |
'---------------'
At the top is the command line interface that drives the underlying layers.
The Nix language evaluator transforms Nix expressions into self-contained build plans, which are used to derive build results from referenced build inputs.
The command line interface and Nix expressions are what users deal with most.
Note The Nix language itself does not have a notion of packages or configurations. As far as we are concerned here, the inputs and results of a build plan are just data.
Underlying the command line interface and the Nix language evaluator is the Nix store, a mechanism to keep track of build plans, data, and references between them. It can also execute build plans to produce new data, which are made available to the operating system as files.
A build plan itself is a series of build tasks, together with their build inputs.
Important A build task in Nix is called derivation.
Each build task has a special build input executed as build instructions in order to perform the build. The result of a build task can be input to another build task.
The following data flow diagram shows a build plan for illustration. Build inputs used as instructions to a build task are marked accordingly:
+--------------------------------------------------------------------+
| build plan |
| |
| .-------------. |
| | build input |---------. |
| '-------------' | |
| instructions |
| | |
| v |
| .-------------. .----------. |
| | build input |-->( build task )-------. |
| '-------------' '----------' | |
| instructions |
| | |
| v |
| .-------------. .----------. .--------------. |
| | build input |---------. ( build task )--->| build result | |
| '-------------' | '----------' '--------------' |
| instructions ^ |
| | | |
| v | |
| .-------------. .----------. | |
| | build input |-->( build task )-------' |
| '-------------' '----------' |
| ^ |
| | |
| | |
| .-------------. | |
| | build input |---------' |
| '-------------' |
| |
+--------------------------------------------------------------------+
File System Object
Nix uses a simplified model of the file system, which consists of file system objects. Every file system object is one of the following:
File
- A possibly empty sequence of bytes for contents
- A single boolean representing the executable permission
Directory
Mapping of names to child file system objects
-
An arbitrary string. Nix does not assign any semantics to symbolic links.
File system objects and their children form a tree. A bare file or symlink can be a root file system object.
Nix does not encode any other file system notions such as hard links, permissions, timestamps, or other metadata.
Examples of file system objects
A plain file:
50 B, executable: false
An executable file:
122 KB, executable: true
A symlink:
-> /usr/bin/sh
A directory with contents:
âââ bin
â  âââ hello: 35 KB, executable: true
âââ share
âââ info
â  âââ hello.info: 36 KB, executable: false
âââ man
âââ man1
âââ hello.1.gz: 790 B, executable: false
A directory that contains a symlink and other directories:
âââ bin -> share/go/bin
âââ nix-support/
âââ share/