Derivations: Difference between revisions

Klinger (talk | contribs)
DoggoBit (talk | contribs)
Expand on what derivations actually ''are''
Line 1: Line 1:
A Nix derivation is ''a specification for running an executable on precisely defined input files to repeatably produce output files at uniquely determined file system paths''<ref>Derivations - Nix Reference Manual</ref>. Simply put, it describes a set of steps to take some input and produce some output in a deterministic manner.
'''Derivations''' are the [[Nix ecosystem]] way of describing any reproducible build process. While [[NixOS]] comes with a plethora of packages, applications and options, there will inevitably come a time when you need to build an application, a library, a package, etc. that is not available ''off the shelf'' already — those are all derivations ''under the hood''. This makes the build process ''reproducible'' and ''predictable''; without changing the derivation's input configuration, the output will remain the same. In essence, a derivation is a ''pure function'' of an executable, and a set of input configuration, that produces exactly the same output for every invocation, in unique locations on the filesystem.  


Derivations are extremely powerful and a core part of Nix utility and provide the core platform for how packaging is done in [[NixOS]]. The low-level <code>derivation</code> function is extremely simple, and can become repetitive to define package constructs with, so [[Nixpkgs]] hoists [https://nixos.org/manual/nixpkgs/unstable/#chap-stdenv the standard environment] (colloquially referred to as the <code>stdenv</code>), which provides a sparse set of compilation tools and a derivation construct that avoids the low-level details and provides abstractions to simplify packaging.
== Motivation ==
While the need to build software, package libraries and execute build processes is clear to anyone using any operating system, the natural question that may arise is ''Why go out of our way dealing with this complicated process when I can just run a few terminal commands?'' For most distributions, the answer is ''they don't do things this way''. Most Linux distributions, and most operating systems for that matter, are designed to be change over time; the same build process will yield different results each time it's invoked. For example, remember trying to build a package twice; the first time you build it the installation will be successful, but the second time it's built you might get an error about the paths it's trying to write to already exist. Build processes in most Linux distributions are ''stateful'', the context in which they're run might change as you're using that system.
 
However, the Nix ecosystem is ''fundamentally'' different in this regard; when you build a derivation, a unique path in the [[Nix store]] is assigned, and all possible outputs (including filesystem operations) produced by it will be persisted under that path. No other derivation can modify those files; the result of the derivation is '''uniquely determined''' by its input configuration, and subsequent reruns will produce ''exactly'' the same outputs, under different Nix store paths. Any potential issues regarding being able to reproduce a build process are addressed ''by design'', if a derivation was successful once, it will always build successfully as long as its inputs don't change.
 
Derivations are a powerful fundamental part of [[Nix]] and provide the core platform for managing packages in NixOS. Every package and library you include in your NixOS configuration is a derivation.
 
== Definition ==
A '''derivation''' is defined as ''a specification for running an executable on precisely defined input files to repeatably produce output files at uniquely determined file system paths''<ref>Derivations - Nix Reference Manual</ref>. Simply put, it describes a set of steps to take some input and produce some output in a deterministic manner.
 
Derivations can be written manually using the <code>derivation</code> function; this is the most fundamental way in which they can be defined. However, since this low-level function is quite simple, building derivations this way can easily become unwieldy and repetitive. To aid in the process of creating derivations, [[Nixpkgs]] contains [https://nixos.org/manual/nixpkgs/unstable/#chap-stdenv the standard environment] (also known as the <code>stdenv</code>), which provides a set of utility functions for the most common derivation types (e.g. a [[Python libraries|Python package]], a shell script, a [[Docker]] container, etc.)


== Getting Started ==
== Getting Started ==