Jekyll Development Environment on Nix
An isolated and reproducible Ruby development environment for my website.
While playing with Nix, I found the ‘nix-shell’ command. I think of it as a lightweight container. Specify the dependencies, and Nix builds a sandboxed ephemeral environment. Exit the shell and everything is gone. It's a great way to keep my projects clean and reproducible: one project’s tools can’t pollute another.
I needed a small project to get my hands dirty. This website is an easy one to start with. It’s built with Jekyll, so it needs Ruby, two gems and ImageMagick (to convert some images).
Nix built-in Bundler environment makes this easy. Start with a ‘default.nix’ file in the root of the project.
This should be self-explanatory, except for the ‘gemset.nix’ part. This file is a ‘Gemfile.lock’ but with checksums for each gem as Nix requires. To create it, use the Bundix tool:
Next, add the dependencies and optionally a ‘shellHook’ to start the Jekyll server. I added ImageMagick as an example for other dependencies that might be needed.
Tadaa! A full reproducible auto cleaning Jekyll development environment on any Nix system from a single command: simply run ‘nix-shell’ from the root.
A project can have multiple .nix configs: a ‘tests.nix’ for example with different databases or Ruby versions.
More Resources
- What Is Nix, an introduction to the Nix store, derivations, sandboxing, and the Nix language by Shopify Engineering.
- The Nix primer by a newcomer article is a great introduction to the different components used in Nix.
- The Nixology screencasts on how to replace Homebrew with Nix.