Nix derivations are instructions (recipes) for building a Nix package.
Derivation
-
writeShellApplication
The function generates a Derivation for a shell script specified as the value fortext
attribute.The recommended way to create Derivation using shell scripts.
-
Rapid Introduction to Nix
Derivation output
-
Nixifying a Haskell project using nixpkgs
We used
callCabal2nix
function from nixpkgs to build thetodo-app
package above. This functio generates a Haskell package Derivation from its source, utilizing the “cabal2nix” program to convert a cabal file into a Nix derivation. -
Nix for Development
While package management is the key purpose of Nix, its derivations can also be used to produce non-package types, such as development environments (aka. “devShell”).
-
Nix Store
The directory used by Nix to store store paths (including derivations).
-
Introduction to module system
Normally we can achieve this by refactoring our Nix expression to be a function (see
lsdFor
⤵️) that takes arguments for these variations (dir
andtree
⤵️), producing the appropriate derivation as a result:This is a simple flake that exposes a package (a
writeShellApplication
Derivation wrapping lsd), that can be `nix run`ed to list the contents of the root directory. -
Import From Derivation (IFD)
Nix expressions are evaluated to produce derivations (among other values). These derivations when realized usually produce the compiled binary packages. Sometimes, realizing a derivation can produce a Nix expression representing another derivation. This generated Nix expression too needs to be evaluated to its derivation before it can be realized. This secondary evaluation is achieved by
import
ing from the derivation being evaluated, and is called “import from derivation” or IFD. -
First steps with Nix
Note that this is the same path used by both
nix build
andnix shell
. Each specific package is uniquely identified by their Store path; changing any part of its build recipe (including dependencies), changes that path. Hence, nix is reproducible.The
cowsay
Derivation produces two output paths, the second of which is the cowsay binary package (the first one is the separate documentation path), and if you inspect that* you will see the contents of it:What is a Nix “package”? Technically, a Nix package is a special Store path built using instructions from a Derivation, both of which reside in the Nix Store. To see what is contained by the
cowsay
package, look inside its Store path. To get the store path for a package (here,cowsay
), runnix build
as follows:[!info] Nix Store & Store Paths
/nix/store
is a special directory representing the Nix Store. The paths inside/nix/store
are known as Store path. Nix fundamentally is, in large part, about manipulating these store paths in a pure and reproducible fashion; Derivation are “recipes” that does this manipulation, and they too live in the Nix Store.[!info]
nix run
nix run
command will run the specified package from the flake. Herenixpkgs
is the flake, followed by the letter#
, which is followed by the package (Derivation) namecowsay
that is outputted by that flake. See Flake URL for details on the syntax. -
Binary Cache
A binary cache provides cached binaries of built Nix Derivation.