⟰ Some NixOS Notes
⟸ Modules
⟹ Supplementary Notes

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 packages from nixpkgs and outputs a derivation.

A derivation represents a build action. Derivations are what make Nix packages deterministic: a derivation is defined by all of the inputs used to make it, 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.

{ stdenv, fetchurl }: stdenv.mkDerivation rec {
  pname = "hello";
  version = "2.10";

  #! fetchurl downloads a file and checks it against a (required!) hash.
  #! the `mirror://` scheme is for downloading from one of the lists of mirrors
  #! specified in `nixpkgs/pkgs/build-support/fetchurl/mirrors.nix`
  src = fetchurl {
    url = "mirror://gnu/hello/${pname}-${version}.tar.gz";
    sha256 = "0ssi1wpaf7plaswqqjwigppsg5fyh99vdlb9kzl7c9lng89ndq1i";
  };

  # run the check phase, by default just `make check`
  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, to make a derivation you’re most likely going to be using some provided function to do it, like the stdenv.mkDerivation used here. Functions like these are generally 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 in Nixpkgs Manual: 6.3. Specifying dependencies.

In general, the Nixpkgs Manual provides lots of helpful documentation for writing Nix packages, including information on

⟰ Some NixOS Notes
⟸ Modules
⟹ Supplementary Notes