Suffix

Updating Ruby Gems in Nix

Fixing the BUNDLE_FROZEN error in a nix-managed Rails project.

Drawing of a ruby gem stone in grayscale

I manage my Ruby on Rails projects with Nix. Each project has a `flake.nix` that sets up all the dependencies for the development environment, so starting work is as simple as running nix develop in the project directory.

This setup is great for reproducibility, except when it’s time to update gems. When I run bundle update inside my flake’s dev shell, Bundler will immediately stop me:

$ bundle update
Bundler is unlocking, but the lockfile can’t be updated because frozen mode is set

Trying to unset the flag or override it with BUNDLE_FROZEN=0 did’t work. When Nix packages the Ruby environment, it wraps the `bundle` and `ruby` binaries with BUNDLE_FROZEN=1 hardcoded and points GEM_HOME to a read-only `/nix/store/...` path. Because the Nix store is immutable, Bundler isn’t allowed to download or write new gems there anyway.

The Fix: Use an Ephemeral Nix Shell

Instead of fighting the wrapped binaries inside the flake environment, the cleanest way to update the dependencies is to temporarily step outside of it into an ephemeral shell with standard, unwrapped Ruby tools.

  1. Exit the flake environment and start a temporary shell with `ruby`, `bundler`, and `bundix`: nix shell nixpkgs#ruby nixpkgs#bundler nixpkgs#bundix.

    (Note: Run bundle config list inside this shell to verify `frozen` is no longer enforced.)

  2. Update the gem lockfile: Rather than running bundle update—which will try to download and compile native extensions locally—use bundle lock --update to only resolve the dependency tree and rewrite `Gemfile.lock`.

    (Note: To update a specific gem, pass its name to the lock command: bundle lock --update gem_name.)

  3. Regenerate `gemset.nix`: Run bundix to update the `gemset.nix`.

  4. Return to the project environment: Exit the temporary shell and run nix develop again. Nix will pick up the changes in `gemset.nix`, build the updated gems into the store, and drop you back into the locked development shell.

If you’re running into a “hash mismatch in fixed-output derivation” error, see my other post on gem mismatches.