Packages and Derivations
Like modules, packages are just expressions with a few specific qualities.
A package is a function that takes in a set of attributes (normally from
<nixpkgs>
) and outputs a
derivation.
A derivation represents a build action. Derivations are a good part of what makes Nix packages deterministic: they are defined by all the inputs used to make them, so the same package given different inputs will necessarily be identified as an entirely different derivation.
Let's take a look at a simple package, hello
.
# <nixpkgs/pkgs/applications/misc/hello/default.nix>
{ stdenv, fetchurl }: stdenv.mkDerivation rec {
pname = "hello";
version = "2.10";
src = fetchurl {
# `mirror://` is just a shorthand for one of the mirrors specified in
# <nixpkgs/pkgs/build-support/fetchurl/mirrors.nix>
url = "mirror://gnu/hello/${pname}-${version}.tar.gz";
# a hash is a *required* argument to fetchurl, in order to guarantee that
# the URL will always point to the same file
sha256 = "0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i";
};
# run the check phase, just `make check` by default
doCheck = true;
meta = with stdenv.lib; {
description = "A program that produces a familiar, friendly greeting";
longDescription = ''
GNU Hello is a program that prints "Hello, world!" when you run it.
It is fully customizable.
'';
homepage = "https://www.gnu.org/software/hello/manual/";
changelog = "https://git.savannah.gnu.org/cgit/hello.git/plain/NEWS?h=v${version}";
license = licenses.gpl3Plus;
maintainers = [ maintainers.eelco ];
platforms = platforms.all;
}
}
As you can see, the derivation here is created using stdenv.mkDerivation
.
Functions like these are wrappers around the builtin function derivation
,
which I will not be documenting here as it would be unproductive.
The attributes you can provide to mkDerivation
are numerous; they control dependencies, build phases, and other aspects of the build. They're described in full at Nixpkgs Manual: 6.3. Specifying dependencies.
The [Nixpkgs Manual] provides lots of other helpful documentation for writing Nix packages, including information on