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
Technically, a Nix package is a special directory (a Store path) inside the Nix Store (
/nix/store), built using instructions from a Derivation.[!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.