A minimal shell.nix example for managing system dependencies in Ruby on Rails projects

The other day I was helping someone get started with Nix. I wanted to give them the most minimal example of getting up and running with Nix and Ruby on Rails. No niv. No flakes. Just a 14 line shell.nix.

You can think of using Nix to manage system dependencies like we use Bundler to manage application dependencies. Everything can be self contained to the working directory and your future self won’t have to wrangle with different versions of Ruby, Node, or C libraries between projects.

Step 1: Install nix

Follow the official installation instructions on NixOS. It is recommended to use the multi-user installation if you’re on macOS or Linux. It should only take a few minutes.

Step 2: Clone the example app and enter the Nix shell

  1. Clone the example Git repository and change into the directory
  2. Enter the Nix shell by running $ nix-shell. Verify you’re in the Nix shell by running $ which ruby to see if Nix is in the path (E.g. /nix/store/vwa1c708p5447vjkk06f8xfvl66yy1wx-ruby-3.1.4/bin/ruby)
  3. Try running the console $ ./bin/rails console

Food for thought

We’ve only scratched the surface. To make your day-to-day usage more ergonomic consider the following:

Thanks to Brad Parker for introducing our team to Nix in 2020.

Written by Tate Johnson on 24 Apr 2023. Subscribe to my blog's RSS feed.