GithubHelp home page GithubHelp logo

inscapist / ruby-nix Goto Github PK

View Code? Open in Web Editor NEW
101.0 5.0 3.0 150 KB

Generates reproducible ruby/bundler app environment with Nix

License: MIT License

Nix 55.50% Ruby 35.30% JavaScript 0.09% CSS 1.06% HTML 7.85% Shell 0.19%
nix nix-flake ruby ruby-nix bundix

ruby-nix's Introduction

Ruby Nix

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).

Tl;dr

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

Features

  1. supports local, path-based gems
  2. supports git sources
  3. supports pre-compiled native gems on multiple platforms (see this discussion)
  4. bundix out of the box
  5. A starter template based on flake

How is it different from bundlerEnv?

  1. it does not track the entire directory as inputSrc when gemDir is specified, requiring only gemset.nix.
  2. it does not use BUNDLE_GEMFILE variable.
  3. it works without Gemfile and Gemfile.lock.

The gist

      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 ]; };
        };
      });

Global Bundix Installation (optional)

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 '.*'

Usage

With nix installed and optionally direnv, you can run:

1. Initialize flake

cd myapp
nix flake init -t github:inscapist/ruby-nix/main

2. Enter nix shell

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

3. In nix shell

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.

FAQs

1. When does my environment gets build?

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.

2. How to bundle?

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.

bundle add

run bundle add GEM --skip-install instead

bundle install (after modifying Gemfile)

run bundle lock instead

bundle update GEM

run bundle lock --update=GEM instead

Adding multiple platforms to Gemfile.lock

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

3. bundle lock fails with Permission denied when Gemfile contains git sources

A fix is under way, meanwhile, do:

bundle config set --local path 'vendor/bundle'

4. How to use a different ruby version?

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.

Common issues

I want to use a custom ruby version

Check the sample. @bobvanderlinden maintains it at the nixpkgs-ruby project.

Local bundle config overriding environment

Check your .bundle/config

Ruby or gems don't come from nix

Check that you have removed .rbenv or .rvm

Bundle install doesn't work

Check the previous section. You should only use bundler to lock your dependencies, not install them.

I don't like that nix develop is a bash shell

Use nix develop -c zsh or even better install .envrc, following this guide.

Roadmap

  1. Try out more ruby versions, both old and new
  2. Make bundix runs faster
  3. More documentations

Credits

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 :)

ruby-nix's People

Contributors

dependabot[bot] avatar inscapist avatar ngiger avatar wojtodzio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ruby-nix's Issues

Error option `gmail-britta' does not exist

Following

Gemfile --[bundle lock]-->
Gemfile.lock --[bundix]-->
gemset.nix --[nix develop (actual build)]-->
reproducible ruby environment

I used the example devShell and the following:

nix develop .#ruby --accept-flake-config --impure --show-trace
touch Gemfile
echo "source 'https://rubygems.org'" > Gemfile
bundle add gmail-britta --skip-install
bundle lock
bundix
# leave the devShell and rerun with generated gemset.nix this time
nix develop .#ruby --accept-flake-config --impure --show-trace

This is what I get:

error: The option `gmail-britta' does not exist. Definition values:
       - In `/nix/store/1pa0xvp1r7znaym31mkcygnqmxbksskw-source/home/misc/gemset.nix':
           {
             dependencies = [
               "haml"
             ];
             groups = [
           ...

gemset isn't generated correctly for native deps

If you have a gemfile with sqlite3 gem on an M1 mac, the gemset is incorrectly constructed with the source.platform being set to the native platform that built it, and the version having the native platform suffix added on. I had to manually to fix all the values for it to work.

nokogiri = {
    dependencies = ["racc"];
    groups = ["default" "development" "test"];
    nativeSources = [{
      platform = "x86_64-darwin";
      remotes = ["https://rubygems.org"];
      sha256 = "06z9g7cyfxggx2gpvfiw9dkad85p7npyb8kniikzj3hkp325yxc1";
      type = "gem";
    } {
      platform = "x86_64-linux";
      remotes = ["https://rubygems.org"];
      sha256 = "18zhmnanxm9w19lnl4rjvnjq6wry3760jk9pkfhd1nny0mkj9mai";
      type = "gem";
    }];
    platforms = [];
    source = {
      platform = "arm64-darwin";
      remotes = ["https://rubygems.org"];
      sha256 = "1ai4avxcafq6b5kj3wmfgvilj4qw0gz78lr7grzy11hkqm3zycml";
      type = "gem";
    };
    version = "1.13.10-arm64-darwin";
  };

to

nokogiri = {
    dependencies = ["racc"];
    groups = ["default" "development" "test"];
    nativeSources = [{
      platform = "x86_64-darwin";
      remotes = ["https://rubygems.org"];
      sha256 = "06z9g7cyfxggx2gpvfiw9dkad85p7npyb8kniikzj3hkp325yxc1";
      type = "gem";
    } {
      platform = "x86_64-linux";
      remotes = ["https://rubygems.org"];
      sha256 = "18zhmnanxm9w19lnl4rjvnjq6wry3760jk9pkfhd1nny0mkj9mai";
      type = "gem";
    }];
    platforms = [];
    source = {
      platform = "ruby";
      remotes = ["https://rubygems.org"];
      sha256 = "1ai4avxcafq6b5kj3wmfgvilj4qw0gz78lr7grzy11hkqm3zycml";
      type = "gem";
    };
    version = "1.13.10";
  };

Error trying to start shell "error: value is null while a set was expected"

Hello!

Thank you for this project! I'm trying to build my app with it, but I'm running into an issue, and not sure how to fix it.

I've run:

nix flake init -t github:sagittaros/ruby-nix/main
nix develop
> bundix # which generated gemset.nix
> exit
git add gemset.nix
nix develop

And that's when I get:

error: value is null while a set was expected

Running it with trace shows:

error: value is null while a set was expected

       … while evaluating the attribute 'name'

       at /nix/store/bkvc2q8pdbn09jb9x7ihs1vvbl6nf6rw-source/pkgs/development/ruby-modules/gem/default.nix:103:3:

          102|   #name = builtins.trace (attrs.name or "no attr.name" ) "${namePrefix}${gemName}-${version}";
          103|   name = attrs.name or "${namePrefix}${gemName}-${version}";
             |   ^
          104|

       … while evaluating anonymous lambda

       at /nix/store/bkvc2q8pdbn09jb9x7ihs1vvbl6nf6rw-source/lib/strings.nix:1094:3:

         1093|   in
         1094|   string:
             |   ^
         1095|   # First detect the common case of already valid strings, to speed those up

       … from call site

       at /nix/store/bkvc2q8pdbn09jb9x7ihs1vvbl6nf6rw-source/pkgs/stdenv/generic/make-derivation.nix:321:9:

          320|         in
          321|         lib.strings.sanitizeDerivationName (
             |         ^
          322|           if attrs ? name

       … while evaluating the derivation attribute 'name'

       at /nix/store/bkvc2q8pdbn09jb9x7ihs1vvbl6nf6rw-source/pkgs/stdenv/generic/make-derivation.nix:303:7:

          302|     // (lib.optionalAttrs (attrs ? name || (attrs ? pname && attrs ? version)) {
          303|       name =
             |       ^
          304|         let

       … while evaluating the attribute 'gemPath' of the derivation 'ruby3.2.2-activesupport-7.0.4'

       at /nix/store/bkvc2q8pdbn09jb9x7ihs1vvbl6nf6rw-source/pkgs/stdenv/generic/make-derivation.nix:303:7:

          302|     // (lib.optionalAttrs (attrs ? name || (attrs ? pname && attrs ? version)) {
          303|       name =
             |       ^
          304|         let

       … while evaluating the attribute 'gemPath' of the derivation 'ruby3.2.2-actionview-7.0.4'

       at /nix/store/bkvc2q8pdbn09jb9x7ihs1vvbl6nf6rw-source/pkgs/stdenv/generic/make-derivation.nix:303:7:

          302|     // (lib.optionalAttrs (attrs ? name || (attrs ? pname && attrs ? version)) {
          303|       name =
             |       ^
          304|         let

       … while evaluating the attribute 'passAsFile'

       at /nix/store/bkvc2q8pdbn09jb9x7ihs1vvbl6nf6rw-source/pkgs/build-support/buildenv/default.nix:77:5:

           76|     # XXX: The size is somewhat arbitrary
           77|     passAsFile = if builtins.stringLength pkgs >= 128*1024 then [ "pkgs" ] else [ ];
             |     ^
           78|   }

       … while evaluating the attribute 'passAsFile' of the derivation 'my-rails-app-ruby-env'

       at /nix/store/bkvc2q8pdbn09jb9x7ihs1vvbl6nf6rw-source/pkgs/stdenv/generic/make-derivation.nix:303:7:

          302|     // (lib.optionalAttrs (attrs ? name || (attrs ? pname && attrs ? version)) {
          303|       name =
             |       ^
          304|         let

       … while evaluating the attribute 'passAsFile'

       at /nix/store/bkvc2q8pdbn09jb9x7ihs1vvbl6nf6rw-source/pkgs/build-support/buildenv/default.nix:77:5:

           76|     # XXX: The size is somewhat arbitrary
           77|     passAsFile = if builtins.stringLength pkgs >= 128*1024 then [ "pkgs" ] else [ ];
             |     ^
           78|   }

       … while evaluating the attribute 'passAsFile' of the derivation 'my-rails-app'

       at /nix/store/bkvc2q8pdbn09jb9x7ihs1vvbl6nf6rw-source/pkgs/stdenv/generic/make-derivation.nix:303:7:

          302|     // (lib.optionalAttrs (attrs ? name || (attrs ? pname && attrs ? version)) {
          303|       name =
             |       ^
          304|         let

       … while evaluating the attribute 'buildInputs' of the derivation 'nix-shell'

       at /nix/store/bkvc2q8pdbn09jb9x7ihs1vvbl6nf6rw-source/pkgs/stdenv/generic/make-derivation.nix:303:7:

          302|     // (lib.optionalAttrs (attrs ? name || (attrs ? pname && attrs ? version)) {
          303|       name =
             |       ^
          304|         let

Following the trace, it seems to be some kind of issue generating the derivation for activesupport? But I'm not sure 🤔 .

Here's my gemset.nix, and nothing looks out of the ordinary:

{
  actionview = {
    dependencies = ["activesupport" "builder" "erubi" "rails-dom-testing" "rails-html-sanitizer"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "06zxdzxgrb0a9p61p41nz5jlrq01cspd6kq621i9hk1h96sj5rj5";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "7.0.4";
  };
  activesupport = {
    dependencies = ["concurrent-ruby" "i18n" "minitest" "tzinfo"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "183az13i4fsm28d0l5xhbjpmcj3l1lxzcxlx8pi8zrbd933jwqd0";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "7.0.4";
  };
  ast = {
    source = null;
    targets = [];
  };
  builder = {
    source = null;
    targets = [];
  };
  byebug = {
    source = null;
    targets = [];
  };
  cli-ui = {
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "1aghiy4qrh6y6q421lcpal81c98zypj8jki4wymqnc8vjvqsyiv4";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "1.5.1";
  };
  coderay = {
    source = null;
    targets = [];
  };
  concurrent-ruby = {
    source = null;
    targets = [];
  };
  crass = {
    source = null;
    targets = [];
  };
  diff-lcs = {
    source = null;
    targets = [];
  };
  erubi = {
    source = null;
    targets = [];
  };
  ffi = {
    source = null;
    targets = [];
  };
  gem-release = {
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "108rrfaiayi14zrqbb6z0cbwcxh8n15am5ry2a86v7c8c3niysq9";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "2.2.2";
  };
  i18n = {
    dependencies = ["concurrent-ruby"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "1vdcchz7jli1p0gnc669a7bj3q1fv09y9ppf0y3k0vb1jwdwrqwi";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "1.12.0";
  };
  inst = {
    dependencies = ["actionview" "activesupport" "cli-ui" "thor" "zeitwerk"];
    groups = ["default"];
    platforms = [];
    source = {
      path = ".";
      type = "path";
    };
    targets = [];
    version = "0.4.0";
  };
  json = {
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "0nalhin1gda4v8ybk6lq8f407cgfrj6qzn234yra4ipkmlbfmal6";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "2.6.3";
  };
  listen = {
    dependencies = ["rb-fsevent" "rb-inotify"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "13rgkfar8pp31z1aamxf5y7cfq88wv6rxxcwy7cmm177qq508ycn";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "3.8.0";
  };
  loofah = {
    dependencies = ["crass" "nokogiri"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "1fpyk1965py77al7iadkn5dibwgvybknkr7r8bii2dj73wvr29rh";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "2.19.0";
  };
  method_source = {
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "1pviwzvdqd90gn6y7illcdd9adapw8fczml933p5vl739dkvl3lq";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "0.9.2";
  };
  minitest = {
    source = null;
    targets = [];
  };
  minitest-documentation = {
    dependencies = ["minitest"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "11zb2di8mw22zvdnq9p6lj9icig3dxz89dkfkcyb7yhijxr56p8n";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "1.0.0";
  };
  nokogiri = {
    dependencies = ["racc"];
    groups = ["default"];
    platforms = [];
    source = null;
    targets = [{
      remotes = ["https://rubygems.org"];
      sha256 = "0mj9fs2z93lvv2cz2z46hiczny86ifgjhh8m5lmwf0ifgd5vnvp7";
      target = "arm64-darwin";
      targetCPU = "arm64";
      targetOS = "darwin";
      type = "gem";
    } {
      remotes = ["https://rubygems.org"];
      sha256 = "15fpz7azgcq1zpd679fgx1njq220zxlcfjz91wfgjgw08qb0x0q1";
      target = "x86_64-darwin";
      targetCPU = "x86_64";
      targetOS = "darwin";
      type = "gem";
    }];
    version = "1.13.9";
  };
  parallel = {
    source = null;
    targets = [];
  };
  parser = {
    dependencies = ["ast"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "17qfhjvnr9q2gp1gfdl6kndy2mb6qdwsls3vnjhb1h8ddimdm4s5";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "3.1.3.0";
  };
  pry = {
    dependencies = ["coderay" "method_source"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "1mh312k3y94sj0pi160wpia0ps8f4kmzvm505i6bvwynfdh7v30g";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "0.11.3";
  };
  pry-byebug = {
    dependencies = ["byebug" "pry"];
    source = null;
    targets = [];
  };
  pry-doc = {
    dependencies = ["pry" "yard"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "1pp43n69p6bjvc640wgcz295w1q2v9awcqgbwcqn082dbvq5xvnx";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "1.4.0";
  };
  pry-reload = {
    dependencies = ["listen"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "1gld1454sd5xp2v4vihrhcjh4sgkx7m1kc29qx1nr96r4z2gm471";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "0.3";
  };
  pry-state = {
    dependencies = ["pry"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "12n5zk3bvwwqvmzc20l9n1pdxhkw9l3ayaqrhqlmnwhycv032nsi";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "0.1.10";
  };
  racc = {
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "0p685i23lr8pl7l09g9l2mcj615fr7g33w3mkcr472lcg34nq8n8";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "1.6.1";
  };
  rails-dom-testing = {
    dependencies = ["activesupport" "nokogiri"];
    source = null;
    targets = [];
  };
  rails-html-sanitizer = {
    dependencies = ["loofah"];
    source = null;
    targets = [];
  };
  rainbow = {
    source = null;
    targets = [];
  };
  rake = {
    source = null;
    targets = [];
  };
  rb-fsevent = {
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "1zmf31rnpm8553lqwibvv3kkx0v7majm1f341xbxc0bk5sbhp423";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "0.11.2";
  };
  rb-inotify = {
    dependencies = ["ffi"];
    source = null;
    targets = [];
  };
  regexp_parser = {
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "0rj7xcg7bkfw6y0h4wg8y3s4nmks9qrzdxag4zaw41xjqfanlysf";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "2.6.1";
  };
  rexml = {
    source = null;
    targets = [];
  };
  rspec = {
    dependencies = ["rspec-core" "rspec-expectations" "rspec-mocks"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "171rc90vcgjl8p1bdrqa92ymrj8a87qf6w20x05xq29mljcigi6c";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "3.12.0";
  };
  rspec-core = {
    dependencies = ["rspec-support"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "1ibb81slc35q5yp276sixp3yrvj9q92wlmi1glbnwlk6g49z8rn4";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "3.12.0";
  };
  rspec-expectations = {
    dependencies = ["diff-lcs" "rspec-support"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "0qldsmjhqr4344zdlamzggr3y98wdk2c4hihkhwx8imk800gkl8v";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "3.12.0";
  };
  rspec-mocks = {
    dependencies = ["diff-lcs" "rspec-support"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "1yvwnb0x5d6d4ff3wlgahk0wcw72ic51gd2snr1xxc5ify41kabv";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "3.12.0";
  };
  rspec-support = {
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "12y52zwwb3xr7h91dy9k3ndmyyhr3mjcayk0nnarnrzz8yr48kfx";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "3.12.0";
  };
  rubocop = {
    dependencies = ["json" "parallel" "parser" "rainbow" "regexp_parser" "rexml" "rubocop-ast" "ruby-progressbar" "unicode-display_width"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "0ggxkq68ddxmynr2lyrvzr8qbrdvc2irxlx9ihxmvdpkg1vimr3w";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "1.39.0";
  };
  rubocop-ast = {
    dependencies = ["parser"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "0sqkg84npyq9z4d3z46w59zyr1r1rbd1mrrlglws9ksw04wdq5x9";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "1.24.0";
  };
  ruby-progressbar = {
    source = null;
    targets = [];
  };
  thor = {
    source = null;
    targets = [];
  };
  tzinfo = {
    dependencies = ["concurrent-ruby"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "0rx114mpqnw2k4h98vc0rs0x0bmf0img84yh8mkkjkal07cjydf5";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "2.0.5";
  };
  unicode-display_width = {
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "0ra70s8prfacpqwj5v2mqn1rbfz6xds3n9nsr9cwzs3z2c0wm5j7";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "2.3.0";
  };
  webrick = {
    source = null;
    targets = [];
  };
  yard = {
    dependencies = ["webrick"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "0p1if8g9ww6hlpfkphqv3y1z0rbqnnrvb38c5qhnala0f8qpw6yk";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "0.9.28";
  };
  zeitwerk = {
    source = null;
    targets = [];
  };
}

Sorry for the wall of text, and thanks in advance!

env.ruby doesn't exist

Heya - I notice default.nix returns a ruby attribute alongside env & envMinimal, but I don't think it actually exists.

If I try to access it -

let
  rubyNix = ruby-nix.lib pkgs;
  inherit (rubyNix {
    name = "simple-ruby-app";
    gemset = ./gemset.nix;
    # run `bundle platform` to find your platform
    gemPlatforms = [ "ruby" "arm64-darwin-20" "x86_64-linux" ];
  }) env envMinimal ruby;
in pkgs.mkShellNoCC {
  nativeBuildInputs = [env];
    # Bootsnap doesn't seem to like switching between ruby versions ("unmatched version file (2.7 for 3.0)")
  BOOTSNAP_CACHE_DIR = "tmp/cache/bootsnap-${ruby.version.majMin}";
}

then I get this error:

error: attribute 'ruby' missing

       at /nix/store/rn1gdnpf2lz7ygyxs0vddmlpd08plraa-source/default.nix:46:10:

           45|   inherit (import ./modules/ruby-env requirements) env envMinimal;
           46|   ruby = env.ruby;
             |          ^
           47| }

Fortunately I know what ruby I'm using so don't actually need the return value, but might be useful to have it in other circumstances.

How does this compare with devenv.sh?

Just found out about devenv and it seems rather popular but I have not tried it.

For those who have tried it, is it on par with ruby-nix in terms of functionality?
If so, maybe we can sunset this project..

how to load gems on gemset as local gems

Hi,

I generated flake.nix with nix flake init -t github:inscapist/ruby-nix/main.

Then I use bundix to generate the gemset.nix.

Next I use nix developer -c $SHELL to build the dependencies and it gives me a shell, this also works.

Inside the shell, I notice that the gems specify on gemset.nix are not listed as local gems gem list and I am guessing this is the reason my project throws below error when I tried to build.

21:13:37 vite.1   | /nix/store/jsmycnp95j8nfj3yb1a471hc4ini939w-bundler-2.4.22/lib/ruby/gems/3.1.0/gems/bundler-2.4.22/lib/bundler/definition.rb:540:in `materialize': Could not find foreman-0.87.2 in locally installed gems (Bundler::GemNotFound)

On gemset.nix I have

  foreman = {
    groups = ["development"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "0szgxvnzwkzrfbq5dkwa98mig78aqglfy6irdsvq1gq045pbq9r7";
      target = "ruby";
      type = "gem";
    };
    targets = [];
    version = "0.87.2";
  };

In fact, if I use bundle info GEM to check the gem specified on gemset.nix, I am getting warning message all gems specified on this file could not be found in locally installed gems.

Does anyone know how to load gems on gemset to local gems?

Repo Wiki?

Would be nice if we have instructions on how to use ruby-nix on new projects or updating old projects to use flakes

Bundler not available on older Ruby versions

Hi there,

Not sure If I'm doing something wrong but following the instructions on README I get an environment with ruby but no bundler.

flake.nix

{
  description = "A simple ruby app demo";

  nixConfig = {
    extra-substituters = "https://nixpkgs-ruby.cachix.org";
    extra-trusted-public-keys =
      "nixpkgs-ruby.cachix.org-1:vrcdi50fTolOxWCZZkw0jakOnUI1T19oYJ+PRYdK4SM=";
  };

  inputs = {
    nixpkgs.url = "nixpkgs";
    ruby-nix.url = "github:sagittaros/ruby-nix";
    # a fork that supports platform dependant gem
    bundix = {
      url = "github:sagittaros/bundix/main";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    fu.url = "github:numtide/flake-utils";
    bob-ruby.url = "github:bobvanderlinden/nixpkgs-ruby";
    bob-ruby.inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs = { self, nixpkgs, fu, ruby-nix, bundix, bob-ruby }:
    with fu.lib;
    eachDefaultSystem (system:
      let
        pkgs = import nixpkgs {
          inherit system;
          overlays = [ bob-ruby.overlays.default ];
        };
        rubyNix = ruby-nix.lib pkgs;

        # TODO generate gemset.nix with bundix
        gemset =
          if builtins.pathExists ./gemset.nix then import ./gemset.nix else { };

        # If you want to override gem build config, see
        #   https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/ruby-modules/gem-config/default.nix
        gemConfig = { };

        # See available versions here: https://github.com/bobvanderlinden/nixpkgs-ruby/blob/master/ruby/versions.json
        ruby = pkgs."ruby-3.2";

        bundixcli = bundix.packages.${system}.default;
      in rec {
        inherit (rubyNix {
          inherit gemset ruby;
          name = "my-rails-app";
          gemConfig = pkgs.defaultGemConfig // gemConfig;
        })
          env;

        devShells = rec {
          default = dev;
          dev = pkgs.mkShell {
            buildInputs = [ env bundixcli ]
              ++ (with pkgs; [ nodejs-19_x yarn rufo ]);
          };
        };
      });
}

flake.lock

{
  "nodes": {
    "bob-ruby": {
      "inputs": {
        "flake-utils": "flake-utils",
        "nixpkgs": [
          "nixpkgs"
        ]
      },
      "locked": {
        "lastModified": 1681278043,
        "narHash": "sha256-RsoF1uJJRDBfl1yF4kxNFs2ZEq4XDY5fa205RbE/lwo=",
        "owner": "bobvanderlinden",
        "repo": "nixpkgs-ruby",
        "rev": "42dc88616a47462efc0c3aad7d3027cd04bbd202",
        "type": "github"
      },
      "original": {
        "owner": "bobvanderlinden",
        "repo": "nixpkgs-ruby",
        "type": "github"
      }
    },
    "bundix": {
      "inputs": {
        "fu": "fu",
        "nixpkgs": [
          "nixpkgs"
        ]
      },
      "locked": {
        "lastModified": 1679041248,
        "narHash": "sha256-E2Lo2y7EU3XUm4/HzyJz2uxbz55kBuap4sNcDMyRcCw=",
        "owner": "sagittaros",
        "repo": "bundix",
        "rev": "139e7726f9ae13d355e15c9224e045ccd9af7b3b",
        "type": "github"
      },
      "original": {
        "owner": "sagittaros",
        "ref": "main",
        "repo": "bundix",
        "type": "github"
      }
    },
    "flake-utils": {
      "locked": {
        "lastModified": 1667395993,
        "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=",
        "owner": "numtide",
        "repo": "flake-utils",
        "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f",
        "type": "github"
      },
      "original": {
        "owner": "numtide",
        "repo": "flake-utils",
        "type": "github"
      }
    },
    "fu": {
      "locked": {
        "lastModified": 1676283394,
        "narHash": "sha256-XX2f9c3iySLCw54rJ/CZs+ZK6IQy7GXNY4nSOyu2QG4=",
        "owner": "numtide",
        "repo": "flake-utils",
        "rev": "3db36a8b464d0c4532ba1c7dda728f4576d6d073",
        "type": "github"
      },
      "original": {
        "owner": "numtide",
        "repo": "flake-utils",
        "type": "github"
      }
    },
    "fu_2": {
      "inputs": {
        "systems": "systems"
      },
      "locked": {
        "lastModified": 1681202837,
        "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
        "owner": "numtide",
        "repo": "flake-utils",
        "rev": "cfacdce06f30d2b68473a46042957675eebb3401",
        "type": "github"
      },
      "original": {
        "owner": "numtide",
        "repo": "flake-utils",
        "type": "github"
      }
    },
    "nixpkgs": {
      "locked": {
        "lastModified": 1681389457,
        "narHash": "sha256-Z6TRJ2aI1eRd+kICdrkNyL2aH7XKw8ogzLdtGz1Q9qI=",
        "owner": "NixOS",
        "repo": "nixpkgs",
        "rev": "c58e6fbf258df1572b535ac1868ec42faf7675dd",
        "type": "github"
      },
      "original": {
        "id": "nixpkgs",
        "type": "indirect"
      }
    },
    "nixpkgs_2": {
      "locked": {
        "lastModified": 1678875422,
        "narHash": "sha256-T3o6NcQPwXjxJMn2shz86Chch4ljXgZn746c2caGxd8=",
        "owner": "NixOS",
        "repo": "nixpkgs",
        "rev": "126f49a01de5b7e35a43fd43f891ecf6d3a51459",
        "type": "github"
      },
      "original": {
        "id": "nixpkgs",
        "type": "indirect"
      }
    },
    "root": {
      "inputs": {
        "bob-ruby": "bob-ruby",
        "bundix": "bundix",
        "fu": "fu_2",
        "nixpkgs": "nixpkgs",
        "ruby-nix": "ruby-nix"
      }
    },
    "ruby-nix": {
      "inputs": {
        "nixpkgs": "nixpkgs_2"
      },
      "locked": {
        "lastModified": 1679572735,
        "narHash": "sha256-LZJ79cu146aTSLkX5OgXCSv1lZZ2RcE+a2gjeB9Mk5Y=",
        "owner": "sagittaros",
        "repo": "ruby-nix",
        "rev": "dfb2a4531fec56793b84c5cec9bffd5d5e19c0b6",
        "type": "github"
      },
      "original": {
        "owner": "sagittaros",
        "repo": "ruby-nix",
        "type": "github"
      }
    },
    "systems": {
      "locked": {
        "lastModified": 1681028828,
        "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
        "owner": "nix-systems",
        "repo": "default",
        "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
        "type": "github"
      },
      "original": {
        "owner": "nix-systems",
        "repo": "default",
        "type": "github"
      }
    }
  },
  "root": "root",
  "version": 7
}

.envrc

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

Output:

λ which bundle                                                                                                                                    
/usr/bin/bundle
~/test-ruby-env 
λ which ruby                                                                                                                                      
/nix/store/ccd0ack4dw4dsg28zj86pbnhlajpqlfl-my-rails-app/bin/ruby
~/test-ruby-env 
λ echo $PATH | tr : "\n"                                                                                                                        
/nix/store/1mq693ljrcnrdzmkfiywbzxk9pz2c3m8-clang-wrapper-11.1.0/bin
/nix/store/lv1r3zd6jrs5zvw9k72nxav7xwd0cbl7-clang-11.1.0/bin
/nix/store/v2v8ljfaid7icj00ay6lyv6k57xirak8-coreutils-9.1/bin
/nix/store/23siajrr39w6wj691mk7i9ryzn0g8zks-cctools-binutils-darwin-wrapper-973.0.1/bin
/nix/store/zrqd8yv2kmv460zyrgrlb2514nq4fk9m-cctools-binutils-darwin-973.0.1/bin
/nix/store/ccd0ack4dw4dsg28zj86pbnhlajpqlfl-my-rails-app/bin
/nix/store/3m0rs8awi836kabivdv70kszx8j59123-bundix/bin
/nix/store/mknm4z3zw3ai12w7jgghzbc41g1qbsyv-nodejs-19.9.0/bin
/nix/store/hr2rph6jmp9l85837zcc4yfgv4asnf32-yarn-1.22.19/bin
/nix/store/1ybk7dfryn8ngnjhjnhvc9pba8fwypi4-rufo-0.12.0/bin
/nix/store/v2v8ljfaid7icj00ay6lyv6k57xirak8-coreutils-9.1/bin
/nix/store/cph6k98y8sjmr0mis2y60s56b3sdpxj1-findutils-4.9.0/bin
/nix/store/pcfw2fnaxcxxs7ln4hy1jaaqgb8rmdhj-diffutils-3.9/bin
/nix/store/n023kyx1zmzdjqhc5jwb2vrlxmgm3pzv-gnused-4.9/bin
/nix/store/ynp1z7azqz6xcs9x3isgpm3ry4526nx0-gnugrep-3.7/bin
/nix/store/q4zlavvz0x1iyx7mrv4zwkz7lk77yams-gawk-5.2.1/bin
/nix/store/swrn8a0pzdvyqb9k4snj0f81qi2zj482-gnutar-1.34/bin
/nix/store/mbxlf6szaixjhqw07k139jam6g5vb005-gzip-1.12/bin
/nix/store/28azg0h5mighcayy28x1shgaz6wbw3nr-bzip2-1.0.8-bin/bin
/nix/store/d1bybmym70qv03h82ij7sc7055b75zj0-gnumake-4.4.1/bin
/nix/store/24myb0ky5zv2zfkm6lwrijiavly13cf7-bash-5.2-p15/bin
/nix/store/aa17c5qkavymj717hif7hsdyhglb1ngd-patch-2.7.6/bin
/nix/store/yvdv2x7z7qjhqzym5zndnimisg2b74nq-xz-5.4.2-bin/bin
/nix/store/3jsn64538qqfjyix6z89cs1ippigmf8q-file-5.44/bin
/opt/homebrew/bin
/opt/homebrew/sbin
/Users/pcasaretto/.nix-profile/bin
/etc/profiles/per-user/pcasaretto/bin
/run/current-system/sw/bin
/nix/var/nix/profiles/default/bin
/usr/local/bin
/usr/bin
/usr/sbin
/bin
/sbin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.