Skip to content

Instantly share code, notes, and snippets.

@execat
Last active March 8, 2023 16:36
Show Gist options
  • Select an option

  • Save execat/ccc5c323cbc03f23bdadbb8f644950c4 to your computer and use it in GitHub Desktop.

Select an option

Save execat/ccc5c323cbc03f23bdadbb8f644950c4 to your computer and use it in GitHub Desktop.
Using Nix to run a Rails project

Create new Rails project $PROJECT_NAME:

$ nix-shell -p "ruby_3_0.withPackages (ps: with ps; [ rails ])" -p bundix -p sqlite \
    --run "rails new $PROJECT_NAME --skip-bundle --webpacker react && \
       cd $PROJECT_NAME && \
       bundle config set force_ruby_platform true && \
       bundle lock && \
       bundix --ruby=ruby_3_0"

Create shell.nix, replace $PROJECT_NAME with your project name:

{ pkgs ? import <nixpkgs> {} }:

with pkgs;
let
  env = bundlerEnv {
    name = "$PROJECT_NAME-bundle";
    gemdir = ./.;
    ruby = ruby_3_0;
  };

in mkShell {
  buildInputs = [
    env
    env.wrappedRuby

    # Additional utilities
    sqlite
    nodejs-16_x
    yarn
  ];
}

Post install:

$ rails webpacker:install
$ rails db:migrate

To update Rails versions (or any other gem versions):

  1. Make changes in Gemfile

  2. Remove lockfile: rm Gemfile.lock

  3. Run: nix-shell -p bundix --run "bundle lock && bundix --ruby=ruby_3_0"

    This does 2 things:

    1. Updates the lockfile
    2. Creates the updated gemset.nix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment