A gentle introduction to Nix

Nix is a package manager and system configuration tool that makes it easy to create a consistent developer environment. It helps with the type of "but it works on my machine!" problem, and is also nice for creating a sandbox for trying new things without potentially mucking up your current system's configurations.

Bike Brigade's dispatching software already has Nix set up, but I wanted to try setting it up for an existing project (this blog!) from scratch. Here is a quick rundown of what I did.

  1. Download Nix

(Okay, I already had it installed but for the purposes of a good narrative let's start from the beginning).

# Mac instructions
$ sh <(curl -L https://nixos.org/nix/install) --darwin-use-unencrypted-nix-store-volume --daemon

More install options here: https://nixos.org/guides/install-nix.html

  1. Create a shell.nix file
$ touch `shell.nix`

I found it surprisingly difficult to dig up a basic no-frills example of what a shell.nix file should contain. Though, eventually I watched the demo video from Nix's homepage which I'm basically writing out in non-video form here.

{ pkgs ? import <nixpkgs> {} }:
# Nixpkgs is a repository of software packages (hosted on Github) that can be installed with the Nix package manager.

pkgs.mkShell {
# specify which packages to add to the shell environment

buildInputs = [
pkgs.buildPackages.nodejs-16_x
# NodeJS, above version 16 please!
];
}

You can search for the packages you would like to be available in your development environment here (try, for example, "nodejs").

  1. Enter the Nix development environment with the command nix-shell

Now, you can verify that the version available is as expected (v16.9.1 at the time of writing).

$ `node -v`
=> v16.9.1