Easy Peasy Nix Versions

This is a convention for using third-party packages in Nix. It has a simple directory structure, makes using the packages straightforward and automatizes updates.

Where should you declare the third-party packages you use in your Nix project? Should all specs be declared in the default.nix? Should each third-party package get its own directory with a default.nix and a spec.src.json? Or maybe the specs should only exist at call site, potentially duplicated if the package is used in different places? I settled on a simple convention which I’ve been using for a year across all my personal and work Nix projects.

The idea is to use a single JSON file to store all versions rather than having each package in a directory defining its URL and version separately (I tend to get lost with the latter). Two more files are involved: one that acts as glue between JSON and Nix, and one that automatizes the package updates. It’s not a full-blown solution like require.nix (a.k.a. flake.nix) but it does the job very well for small- to medium-sized project.

Specifying and fetching third-party packages

We’ll take the homies repository as an example.

homies is a Nix-based reproducible environment, you can read about it in the dedicated article.

If you checkout the repository you’ll see a nix/ directory with two files: versions.json and fetch.nix. The nix/versions.json file contains information about where to find each of the third-party packages:

Each third-party package is given a name, like nixpkgs or snack in the above example. That name is used as a key in a JSON dictionary. For each package the corresponding value is the GitHub repository information as well as the sha256 for fetchurl (this is tailored for GitHub, but make sure to read the closing thoughts for ideas). The branch attribute is not used by Nix but comes in handy for updating the packages.

The nix/fetch.nix file is a wrapper for using the third-party packages in your Nix code. The third-party packages are retrieved using the name you gave them in the JSON file; see the default.nix in homies:

The nix/fetch.nix file creates a record where the keys are the package names (nixpkgs, snack) and the values are the unpacked archives. Neat, heh?

The first time I used this scheme it greatly simplified my life, because I knew exactly where all the third-party packages were defined. No more going up and down directory trees to figure out this or that package’s repo name, and moreover it only takes a second to check whether any package is defined locally or if you’re pulling it from GitHub. Another benefit of this approach is that it makes updating the packages super easy as well. Read on!

Updating third-party packages

If your third-party package specs are spread all over your codebase, updating them all will take ages. With the approach described above updating all packages only takes a bash loop and some jq-ing. The homies repo has a single update script, script/update. It takes the path to the versions.json file and a list of packages to update (below I stick to a single package for simplicity’s sake):

Then it reads that package’s owner, repository name and the branch that we’re tracking:

Using this it retrieves the GitHub URL and calls nix-prefetch-url to compute the sha256:

Finally it re-reads the versions.json content, updates the rev and sha256 attributes of that package, and writes it back to versions.json:

Package: UPDATED!

Closing thoughts

As mentioned in the introduction, this is a convention and should be tweaked to fit your needs. The important points are:

When I create a new Nix project I simply copy over the nix/fetch.nix file and the update script. This cannot be abstracted away in a separate repo because there’s a chicken and egg problem: if this is a Nix “library”, how do we fetch it?

The versions.json file is very simple; YMMV. For instance you could store URLs instead of GitHub repositories (you should then also tweak the update script). The script/update can also be adapted to your needs; for instance, in homies, it’s possible to only compute the sha256 without pulling the branch’s latest revision. That’s super convenient when adding new packages: you don’t have to call nix-prefetch-url by hand!

zimbatm – thanks for reviewing this post bro – also started work on something similar which you might want to check out: nix-path