From 5f1622e95f166cbe4738cef956eb0851d9ea0a00 Mon Sep 17 00:00:00 2001
From: Charles Strahan <charles.c.strahan@gmail.com>
Date: Tue, 20 Jan 2015 23:33:57 -0500
Subject: [PATCH] ruby: allow for passing build arguments

---
 .../interpreters/ruby/bundler-env.nix         | 28 +++++++++++++++++--
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/pkgs/development/interpreters/ruby/bundler-env.nix b/pkgs/development/interpreters/ruby/bundler-env.nix
index 5e471d36f2d..1ea982c111e 100644
--- a/pkgs/development/interpreters/ruby/bundler-env.nix
+++ b/pkgs/development/interpreters/ruby/bundler-env.nix
@@ -8,9 +8,13 @@
 # This is a work-in-progress.
 # The idea is that his will replace load-ruby-env.nix.
 
-{ name, gemset, gemfile, lockfile, ruby ? defs.ruby, fixes ? gemFixes }@args:
+{ name, gemset, gemfile, lockfile, ruby ? defs.ruby, fixes ? gemFixes
+, enableParallelBuilding ? false # TODO: this might not work, given the env-var shinanigans.
+, documentation ? false}@args:
 
 let
+
+  shellEscape = x: "'${lib.replaceChars ["'"] [("'\\'" + "'")] x}'";
   const = x: y: x;
   bundler = bundler_HEAD.override { inherit ruby; };
   inherit (builtins) attrValues;
@@ -128,7 +132,7 @@ let
     then acc
     else acc + ''
       cp ${writeScript "${next.name}-pre-install" ''
-        #!/bin/sh
+        #!${stdenv.shell}
 
         buildInputs="${toString (next.buildInputs or [])}"
         nativeBuildInputs="${toString (next.nativeBuildInputs or [])}"
@@ -227,6 +231,16 @@ let
     end
   '';
 
+  needsBuildArgs = attrs: attrs ? buildArgs;
+
+  mkBuildArgs = spec:
+    "export BUNDLE_BUILD__${lib.toUpper spec.name}='${lib.concatStringsSep " " (map shellEscape spec.buildArgs)}'";
+
+  allBuildArgs =
+    lib.concatStringsSep "\n"
+      (map mkBuildArgs
+        (lib.filter needsBuildArgs (attrValues instantiated)));
+
 in
 
 stdenv.mkDerivation {
@@ -262,10 +276,18 @@ stdenv.mkDerivation {
     mkdir pre-installers
     ${createPreInstallers}
 
+    ${allBuildArgs}
+
+    ${lib.optionalString (!documentation) ''
+      mkdir home
+      HOME="$(pwd -P)/home"
+      echo "gem: --no-rdoc --no-ri" > $HOME/.gemrc
+    ''}
+
     mkdir $out/bin
     cp ${./monkey_patches.rb} monkey_patches.rb
     export RUBYOPT="-rmonkey_patches.rb -I $(pwd -P)"
-    bundler install --frozen --binstubs
+    bundler install --frozen --binstubs ${lib.optionalString enableParallelBuilding "--jobs $NIX_BUILD_CORES"}
   '';
   passthru = {
     inherit ruby;