Can I Nix It?

I’ve been using NixOS lately, and wanted to get octopress & jekyll set up so I could start writing about NixOS from within NixOS. I’m still finding my way around the nix ecosystem, and I took a couple wrong turns along the way, but once I found my bearings it was pretty simple.

Since I’m using octopress and jekyll, my blog is a Ruby project and dependencies are listed in Gemfile and Gemfile.lock. Neither Ruby nor octopress are installed globally on my NixOS system, so I can’t just cd into the project and start running octopress commands.

$ octopress new draft "Can I Nix It?"
octopress: command not found

$ type -t ruby || type -t gem || type -t bundler || echo Nothing

Instead, I’m using nix-shell to spawn a development environment that’s tailored to meet the needs of this particular project. bundix takes most of the legwork out of setting this up, so I ran a couple bundix commands in an ad hoc nix-shell.

nix-shell --packages bundix --run 'bundix --magic && bundix --init'

This generated a gemset.nix file, which describs the gem dependencies in a nix expression, as well as a shell.nix file, which configures the environment produced by running nix-shell in the project root directory.

I had to make one change to the generated shell.nix, specifying the gem groups I wanted included. In particular, octopress is grouped under jekyll_plugins in my Gemfile,so I had to ensure that group was included in the environment.

group :jekyll_plugins do
  gem 'octopress', '~> 3.0.11'

The full shell.nix looks like:

with (import <nixpkgs> {});
  env = bundlerEnv {
    name = "";
    inherit ruby;
    gemfile  = ./Gemfile;
    lockfile = ./Gemfile.lock;
    gemset   = ./gemset.nix;
    groups   = [ "default" "jekyll_plugins" ];
in stdenv.mkDerivation {
  name = "";
  buildInputs = [ env ];

Now I can cd into the project, run nix-shell, and all the octopress & jekyll commands are available.