From 5c6e4ea4beb54ed9470e34d902ac05256b2b2d78 Mon Sep 17 00:00:00 2001
From: Alastair Pharo <asppsa@gmail.com>
Date: Mon, 10 Jul 2017 15:11:53 +1000
Subject: [PATCH 1/3] jruby: only wrap jruby executables

Other executables all invoke jruby, and so don't need to be wrapped.

In some cases wrapping breaks the executables because one file is a ruby
script that directly loads the other (e.g. `gem` is a wrapper that loads
`jgem`).  In this case, if the latter script has been wrapped by nix,
loading will fail.
---
 pkgs/development/interpreters/jruby/default.nix | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pkgs/development/interpreters/jruby/default.nix b/pkgs/development/interpreters/jruby/default.nix
index 74203eca380..5c5ede3517c 100644
--- a/pkgs/development/interpreters/jruby/default.nix
+++ b/pkgs/development/interpreters/jruby/default.nix
@@ -18,7 +18,7 @@ stdenv.mkDerivation rec {
      rm $out/bin/*.{bat,dll,exe,sh}
      mv $out/COPYING $out/LICENSE* $out/docs
 
-     for i in $out/bin/*; do
+     for i in $out/bin/jruby{,.bash}; do
        wrapProgram $i \
          --set JAVA_HOME ${jre}
      done

From 68935892abf9a308afd9dcee3522124e08c575c4 Mon Sep 17 00:00:00 2001
From: Alastair Pharo <asppsa@gmail.com>
Date: Mon, 10 Jul 2017 16:40:26 +1000
Subject: [PATCH 2/3] bundix: use same ruby as bundler

---
 pkgs/development/ruby-modules/bundix/default.nix | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/pkgs/development/ruby-modules/bundix/default.nix b/pkgs/development/ruby-modules/bundix/default.nix
index 0caa8b226f0..17f09724d18 100644
--- a/pkgs/development/ruby-modules/bundix/default.nix
+++ b/pkgs/development/ruby-modules/bundix/default.nix
@@ -1,8 +1,8 @@
-{ buildRubyGem, fetchFromGitHub, makeWrapper, lib, bundler, ruby, nix,
+{ buildRubyGem, fetchFromGitHub, makeWrapper, lib, bundler, nix,
   nix-prefetch-git }:
 
 buildRubyGem rec {
-  inherit ruby;
+  inherit (bundler) ruby;
 
   name = "${gemName}-${version}";
   gemName = "bundix";

From 0383c0aa6be37839f353d14e5d885ea5a5fa3932 Mon Sep 17 00:00:00 2001
From: Alastair Pharo <asppsa@gmail.com>
Date: Mon, 10 Jul 2017 16:41:36 +1000
Subject: [PATCH 3/3] jruby: make package compatible with bundix; add devEnv

- Adds the necessary passthru vars for bundler, bundix, etc. to accept
  the package as a ruby.
- Adds the devEnv attribute, so that jruby.devEnv can be used to get
  an environment with bundler and bundix installed.
---
 .../interpreters/jruby/default.nix            | 35 +++++++++++++++++--
 1 file changed, 32 insertions(+), 3 deletions(-)

diff --git a/pkgs/development/interpreters/jruby/default.nix b/pkgs/development/interpreters/jruby/default.nix
index 5c5ede3517c..0ea1bf19231 100644
--- a/pkgs/development/interpreters/jruby/default.nix
+++ b/pkgs/development/interpreters/jruby/default.nix
@@ -1,6 +1,9 @@
-{ stdenv, fetchurl, makeWrapper, jre }:
+{ stdenv, callPackage, fetchurl, makeWrapper, jre }:
 
-stdenv.mkDerivation rec {
+let
+# The version number here is whatever is reported by the RUBY_VERSION string
+rubyVersion = callPackage ../ruby/ruby-version.nix {} "2" "3" "3" "";
+jruby = stdenv.mkDerivation rec {
   name = "jruby-${version}";
 
   version = "9.1.12.0";
@@ -22,12 +25,38 @@ stdenv.mkDerivation rec {
        wrapProgram $i \
          --set JAVA_HOME ${jre}
      done
+
+     ln -s $out/bin/jruby $out/bin/ruby
+
+     # Bundler tries to create this directory
+     mkdir -pv $out/${passthru.gemPath}
+     mkdir -p $out/nix-support
+     cat > $out/nix-support/setup-hook <<EOF
+       addGemPath() {
+         addToSearchPath GEM_PATH \$1/${passthru.gemPath}
+       }
+
+       envHooks+=(addGemPath)
+     EOF
   '';
 
+  passthru = rec {
+    rubyEngine = "jruby";
+    gemPath = "lib/${rubyEngine}/gems/${rubyVersion.libDir}";
+    libPath = "lib/${rubyEngine}/${rubyVersion.libDir}";
+  };
+
   meta = {
     description = "Ruby interpreter written in Java";
     homepage = http://jruby.org/;
     license = with stdenv.lib.licenses; [ cpl10 gpl2 lgpl21 ];
     platforms = stdenv.lib.platforms.unix;
   };
-}
+};
+in jruby.overrideAttrs (oldAttrs: {
+  passthru = oldAttrs.passthru // {
+    devEnv = callPackage ../ruby/dev.nix {
+      ruby = jruby;
+    };
+  };
+})