This is technically a fork of bundlerEnv
that attempts to better meet the needs of ruby app developers instead of package maintainers.
This flake exports a function that is suitable for application development (eg. Rails).
The gemset.nix
file, generated by executing the bundix
command, manages your environment. Bundix depends on the contents of Gemfile.lock
, which is produced by running the bundle lock
command after modifying the Gemfile
.
Gemfile --[bundle lock]-->
Gemfile.lock --[bundix]-->
gemset.nix --[nix develop (actual build)]-->
reproducible ruby environment
- supports local, path-based gems
- supports git sources
- supports pre-compiled native gems on multiple platforms (see this discussion)
- bundix out of the box
- A starter template based on flake
- it does not track the entire directory as
inputSrc
whengemDir
is specified, requiring onlygemset.nix
. - it does not use
BUNDLE_GEMFILE
variable. - it works without
Gemfile
andGemfile.lock
.
let
pkgs = import nixpkgs {
inherit system;
overlays = [ ruby-nix.overlays.ruby ];
};
rubyNix = ruby-nix.lib pkgs;
inherit (rubyNix {
name = "simple-ruby-app";
gemset = ./gemset.nix;
})
env ruby;
in {
devShells = rec {
default = dev;
dev = pkgs.mkShell { buildInputs = [ env ruby ]; };
};
});
My bundix fork is needed to generate the correct gemset.nix
. It is available in nix shell out of the box.
# installation
nix profile install github:inscapist/bundix/main
# upgrade
nix profile upgrade '.*'
With nix installed and optionally direnv, you can run:
cd myapp
nix flake init -t github:inscapist/ruby-nix/main
If you are a direnv user, add the following content to .envrc
and run direnv allow
. Otherwise, run nix develop -c zsh
.
if ! has nix_direnv_version || ! nix_direnv_version 2.2.0; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.2.0/direnvrc" "sha256-5EwyKnkJNQeXrRkYbwwRBcXbibosCJqyIUuz9Xq+LRc="
fi
use flake
In a nix shell, you have ruby
, irb
, bundle
at your disposal. Additional gems will only be available if they are specified in gemset.nix
. To generate that, ensure Gemfile
and Gemfile.lock
are present, then run bundix
.
If you use direnv, running git add gemset.nix
would trigger a rebuild automatically.
Otherwise, Ctrl-D to exit the current nix shell, and enter again.
With ruby-nix, you shouldn't install gems using bundle. Nix will build the gems for you. Always run bundix
to update your gemset after making changes to Gemfile.lock.
run bundle add GEM --skip-install
instead
run bundle lock
instead
run bundle lock --update=GEM
instead
One example could be:
bundle lock --update --add-platform ruby arm64-darwin-21 x86_64-darwin-20 x86_64-linux
You can retrieve the platform names by running bundle platform
. Having multiple platforms would allow your colleagues to use precompiled gems, if they are available.
In your Gemfile.lock, it should show up as:
sorbet-runtime (0.5.10626)
sorbet-static (0.5.10626-universal-darwin-20)
sorbet-static (0.5.10626-universal-darwin-21)
sorbet-static (0.5.10626-universal-darwin-22)
sorbet-static (0.5.10626-x86_64-linux)
sorbet-static-and-runtime (0.5.10626)
sorbet (= 0.5.10626)
@@ -1232,6 +1233,7 @@ GEM
PLATFORMS
arm64-darwin-20
arm64-darwin-21
arm64-darwin-22
x86_64-darwin-20
x86_64-darwin-21
x86_64-linux
A fix is under way, meanwhile, do:
bundle config set --local path 'vendor/bundle'
Code comment of simple-app shows how to use ruby_3_1 instead of the current default version (2.7.6). You can also write your own overlay that overrides globally with your own ruby derivation.
Check the sample. @bobvanderlinden maintains it at the nixpkgs-ruby project.
Check your .bundle/config
Check that you have removed .rbenv
or .rvm
Check the previous section. You should only use bundler
to lock your dependencies, not install them.
Use nix develop -c zsh
or even better install .envrc
, following this guide.
- Try out more ruby versions, both old and new
- Make bundix runs faster
- More documentations
All credits goes to the original authors of buildRubyGem
, bundlerEnv
, and bundix
(@manveru, @lavoiesl, @jdelStrother). This is only a thin layer with a different take on top of these solid foundation.
PRs welcomed :)