I need to use different version of a language in different project directories. E.g. Ruby 3.1 in one, Ruby 3.2 in another.
What is the recommended approach or tool when using nix?
I installed Ruby using
nix-env -iA nixpkgs.ruby, but I will like to switch versions as I cd into a directory.
There are a bunch of competing tools related to this use case, but the starting point for many (most?) of them is direnv’s Nix integration.
Direnv is nice because it’s also popular outside of the Nix world, so most editors/IDEs have plugins that offer some measure of integration with it.
I’d start by playing with that, and then see if anyone here argues for their other tools of choice in a way that resonates with you. If you try to review the whole landscape of Nix tooling for that use case before you start experimenting, you might find yourself overwhelmed or bored.
The wiki has a brief guide of the oldest and most basic way to do this, which still serves pretty well imo: Development environment with nix-shell - NixOS Wiki
My vote also goes to direnv although I would suggest combining it with nix-direnv which has better performance
devenv.sh has support for this.
It is a tool to declaratively specify the development environment of a project. Including what packages should be available.
For Ruby there are a number of options and integrations available.
See the Ruby example of how a devenv configuration could look like: devenv/devenv.nix at be4166d07170c94bf055b93cc9e022a538dfead9 · cachix/devenv · GitHub
It uses the
languages.ruby.version option to choose a specific Ruby version. Behind the scenes it uses GitHub - bobvanderlinden/nixpkgs-ruby: A Nix repository with all Ruby versions being kept up-to-date automatically as a package repository, which includes all versions of Ruby.
devenv.sh also has direnv integration, which will automatically load the devenv into your shell when you
cd into your project directory. See Automatic Shell Activation - devenv
If you’re used to
rvm, it’ll work very similarly. The devenv config also has an option to specify a
.ruby-version so your project can stay compatible with rbenv and rvm.
(I didn’t want to be the guy to write this recommendation because I’m not actually doing development right now so I’m not using it in any serious way, but devenv.sh is the tool in this space that I’m personally most excited about today.
The lovely, lovely, NixOS-like configuration interface is just killer.)