Nix derivations are instructions (recipes) for building a Nix package.
Derivation
-
writeShellApplicationThe function generates a Derivation for a shell script specified as the value fortextattribute.The recommended way to create Derivation using shell scripts.
-
Rapid Introduction to Nix
Derivation output
-
Nixifying a Haskell project using nixpkgs
We used
callCabal2nixfunction from nixpkgs to build thetodo-apppackage 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 (dirandtree⤵️), producing the appropriate derivation as a result:This is a simple flake that exposes a package (a
writeShellApplicationDerivation 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
importing 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 buildandnix 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
cowsayDerivation 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
cowsaypackage, look inside its Store path. To get the store path for a package (here,cowsay), runnix buildas follows:[!info] Nix Store & Store Paths
/nix/storeis a special directory representing the Nix Store. The paths inside/nix/storeare 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 runnix runcommand will run the specified package from the flake. Herenixpkgsis the flake, followed by the letter#, which is followed by the package (Derivation) namecowsaythat 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.