2022-09-19 23:21:40 +03:00
# 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].
2023-06-17 12:06:17 +03:00
[hierarchical layers]: https://en.wikipedia.org/wiki/Multitier_architecture#Layers
2022-09-19 23:21:40 +03:00
2022-12-20 10:59:59 +02:00
The following [concept map] shows its main components (rectangles), the objects they operate on (rounded rectangles), and their interactions (connecting phrases):
2023-06-17 12:06:17 +03:00
[concept map]: https://en.wikipedia.org/wiki/Concept_map
2022-12-20 10:59:59 +02:00
2022-09-19 23:21:40 +03:00
```
2022-12-20 10:59:59 +02:00
.----------------.
| Nix expression |----------.
'----------------' |
| passed to
| |
+----------|-------------------|--------------------------------+
| Nix | V |
| | +-------------------------+ |
| | | commmand line interface |------. |
| | +-------------------------+ | |
| | | | |
| evaluated by calls manages |
| | | | |
| | V | |
| | +--------------------+ | |
| '-------->| language evaluator | | |
| +--------------------+ | |
2022-09-19 23:21:40 +03:00
| | | |
2022-12-12 15:29:02 +02:00
| produces | |
2022-10-16 00:46:32 +03:00
| | V |
| +----------------------------|------------------------------+ |
| | store | | |
2022-09-19 23:21:40 +03:00
| | referenced by V builds | |
2022-10-16 00:46:32 +03:00
| | .-------------. .------------. .--------------. | |
2022-12-20 10:59:59 +02:00
| | | build input |----->| build plan |----->| build result | | |
2022-10-16 00:46:32 +03:00
| | '-------------' '------------' '--------------' | |
2022-12-20 10:59:59 +02:00
| +-------------------------------------------------|---------+ |
+---------------------------------------------------|-----------+
|
represented as
|
V
.---------------.
| file |
'---------------'
2022-09-19 23:21:40 +03:00
```
2022-12-20 10:59:59 +02:00
At the top is the [command line interface ](../command-ref/command-ref.md ) that drives the underlying layers.
2022-09-19 23:21:40 +03:00
2022-12-20 10:59:59 +02:00
The [Nix language ](../language/index.md ) evaluator transforms Nix expressions into self-contained *build plans* , which are used to derive *build results* from referenced *build inputs* .
2022-09-19 23:21:40 +03:00
2022-12-20 10:59:59 +02:00
The command line interface and Nix expressions are what users deal with most.
2022-09-19 23:21:40 +03:00
> **Note**
2023-10-25 05:28:35 +03:00
>
2022-09-19 23:21:40 +03:00
> 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.
2023-10-25 05:50:43 +03:00
Underlying the command line interface and the Nix language evaluator is the [Nix store ](../store/index.md ), a mechanism to keep track of build plans, data, and references between them.
2022-12-20 10:59:59 +02:00
It can also execute build plans to produce new data, which are made available to the operating system as files.
2022-09-19 23:21:40 +03:00
2022-12-20 10:59:59 +02:00
A build plan itself is a series of *build tasks* , together with their build inputs.
2022-09-19 23:21:40 +03:00
> **Important**
2023-01-03 11:11:06 +02:00
> A build task in Nix is called [derivation](../glossary.md#gloss-derivation).
2022-09-19 23:21:40 +03:00
2022-12-20 10:59:59 +02:00
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:
2023-06-17 12:06:17 +03:00
[data flow diagram]: https://en.wikipedia.org/wiki/Data-flow_diagram
2022-12-20 10:59:59 +02:00
2022-09-19 23:21:40 +03:00
```
2022-12-20 10:59:59 +02:00
+--------------------------------------------------------------------+
| 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 |---------' |
| '-------------' |
| |
+--------------------------------------------------------------------+
2022-09-19 23:21:40 +03:00
```