Can't install mini_racer with bundlerEnv

My app uses the mini_racer gem, which has a dependency on the libv8 gem. The libv8 gem derivation builds fine, but it’s then failing to build the mini_racer derivation, apparently because the libv8 gem can’t find the v8 system package.

Gemfile
source 'https://rubygems.org'
gem 'mini_racer'
shell.nix
{ pkgs ? import <nixpkgs> {} }:
with pkgs;

let gems = pkgs.bundlerEnv {
    name = "app-gems";
    gemdir = ./.;
  };
in pkgs.mkShell {
  buildInputs = [gems gems.wrappedRuby];
}
gemset.nix
{
  libv8 = {
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "1jivcckillfvd4n2jnsnnlf93z3gpvqbwsczs0fvv9hc90zpj7yh";
      type = "gem";
    };
    version = "7.3.492.27.1";
  };
  mini_racer = {
    dependencies = ["libv8"];
    groups = ["default"];
    platforms = [];
    source = {
      remotes = ["https://rubygems.org"];
      sha256 = "0zi36qcg5qj4g1c11vwmc7lknjihirrmc6yxi6q8j6v4lfnyjbyg";
      type = "gem";
    };
    version = "0.2.9";
  };

build output
these derivations will be built:
  /nix/store/6qj135w223sikv58iaa5hgwjyf6w8jyn-ruby2.6.5-mini_racer-0.2.9.drv
  /nix/store/rwmb83iflr9cfd40y82z019kqndb340d-app-gems.drv
  /nix/store/kamydiwpc2xvp7swy3hfd690qv2r9s6l-wrapped-ruby-app-gems.drv
building '/nix/store/6qj135w223sikv58iaa5hgwjyf6w8jyn-ruby2.6.5-mini_racer-0.2.9.drv'...
unpacking sources
patching sources
configuring
no configure script, doing nothing
installing
buildFlags:
WARNING:  You build with buildroot.
  Build root: /
  Bin dir: /nix/store/z7l0jbinyrn8765nml83bqndbdk6h9vs-ruby2.6.5-mini_racer-0.2.9/lib/ruby/gems/2.6.0/bin
  Gem home: /nix/store/z7l0jbinyrn8765nml83bqndbdk6h9vs-ruby2.6.5-mini_racer-0.2.9/lib/ruby/gems/2.6.0
Building native extensions. This could take a while...
ERROR:  Error installing /nix/store/b8zgzhd496sknxhlcgj484d1f1rj95s3-mini_racer-0.2.9.gem:
        ERROR: Failed to build gem native extension.

    current directory: /nix/store/z7l0jbinyrn8765nml83bqndbdk6h9vs-ruby2.6.5-mini_racer-0.2.9/lib/ruby/gems/2.6.0/gems/mini_racer-0.2.9/ext/mini_racer_extension
/nix/store/4nkqcd429ml1d474imx8rfrj35vg4pgb-ruby-2.6.5/bin/ruby -I /nix/store/4nkqcd429ml1d474imx8rfrj35vg4pgb-ruby-2.6.5/lib/ruby/2.6.0 -r ./siteconf20200319-53559-1y3kwey.rb extconf.rb
checking for -lpthread... yes
checking for -lobjc... yes
SETTING CXX
checking for v8.h... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
        --with-opt-dir
        --without-opt-dir
        --with-opt-include
        --without-opt-include=${opt-dir}/include
        --with-opt-lib
        --without-opt-lib=${opt-dir}/lib
        --with-make-prog
        --without-make-prog
        --srcdir=.
        --curdir
        --ruby=/nix/store/4nkqcd429ml1d474imx8rfrj35vg4pgb-ruby-2.6.5/bin/$(RUBY_BASE_NAME)
        --with-pthreadlib
        --without-pthreadlib
        --with-objclib
        --without-objclib
        --enable-debug
        --disable-debug
        --enable-asan
        --disable-asan
        --with-v8-dir
        --without-v8-dir
        --with-v8-include
        --without-v8-include=${v8-dir}/include
        --with-v8-lib
        --without-v8-lib=${v8-dir}/lib
/nix/store/cnvr00rr94k7yb7qsax7n2jy7yxr8890-ruby2.6.5-libv8-7.3.492.27.1/lib/ruby/gems/2.6.0/gems/libv8-7.3.492.27.1/ext/libv8/location.rb:57:in `configure': By using --with-system-v8, you have chosen to use the version (Libv8::Location::System::NotFoundError)
of V8 found on your system and *not* the one that is bundled with
the libv8 rubygem.

However, your system version of v8 could not be located.

Please make sure your system version of v8 that is compatible
with 7.3.492.27.1 installed. You may need to use the
--with-v8-dir option if it is installed in a non-standard location
        from /nix/store/cnvr00rr94k7yb7qsax7n2jy7yxr8890-ruby2.6.5-libv8-7.3.492.27.1/lib/ruby/gems/2.6.0/gems/libv8-7.3.492.27.1/lib/libv8.rb:7:in `configure_makefile'
        from extconf.rb:56:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /nix/store/z7l0jbinyrn8765nml83bqndbdk6h9vs-ruby2.6.5-mini_racer-0.2.9/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-17/2.6.0/mini_racer-0.2.9/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in /nix/store/z7l0jbinyrn8765nml83bqndbdk6h9vs-ruby2.6.5-mini_racer-0.2.9/lib/ruby/gems/2.6.0/gems/mini_racer-0.2.9 for inspection.
Results logged to /nix/store/z7l0jbinyrn8765nml83bqndbdk6h9vs-ruby2.6.5-mini_racer-0.2.9/lib/ruby/gems/2.6.0/extensions/x86_64-darwin-17/2.6.0/mini_racer-0.2.9/gem_make.out
builder for '/nix/store/6qj135w223sikv58iaa5hgwjyf6w8jyn-ruby2.6.5-mini_racer-0.2.9.drv' failed with exit code 1
cannot build derivation '/nix/store/rwmb83iflr9cfd40y82z019kqndb340d-app-gems.drv': 1 dependencies couldn't be built
error: build of '/nix/store/kamydiwpc2xvp7swy3hfd690qv2r9s6l-wrapped-ruby-app-gems.drv', '/nix/store/rwmb83iflr9cfd40y82z019kqndb340d-app-gems.drv' failed

Any suggestions on fixing this? I wondered if I could patch the mini_racer gem in the same way that libv8 is, with something like:

    gemConfig = pkgs.defaultGemConfig // {
      mini_racer = attrs: {
        buildInputs = [ which v8 python ];
        buildFlags = [ "--with-v8-dir=${v8}" ];
      };
    };

but keep getting the same error

So for what it’s worth I was able to get mini_racer 0.2.x working with this gem config:

  gemConfig = pkgs.defaultGemConfig // {
    mini_racer = attrs: {
      dontBuild = false;
      buildFlags = "--with-v8-dir=${pkgs.v8}";
      postPatch = ''
        substituteInPlace ext/mini_racer_extension/extconf.rb --replace \
          Libv8.configure_makefile \
          '$CPPFLAGS += " -x c++"; Libv8.configure_makefile'
        '';
    };

However, 0.3.x relies on v8 8.4.255, which I’ve not been able to persuade to compile properly yet :disappointed:

If anyone is still trying to get a new version of mini_racer to work, this worked for me:

  gemConfig = {
    mini_racer = attrs: {
      buildInputs = [ pkgs.icu ];
      dontBuild = false;
      NIX_LDFLAGS = "-licui18n";
    };
    libv8-node =
      let
        noopScript = pkgs.writeShellScript "noop" "exit 0";
        linkFiles = pkgs.writeShellScript "link-files" ''
          cd ../..

          mkdir -p vendor/v8/${system}/libv8/obj/
          ln -s "${pkgs.nodejs_18.libv8}/lib/libv8.a" "vendor/v8/${system}/libv8/obj/libv8_monolith.a"

          ln -s ${pkgs.nodejs_18.libv8}/include vendor/v8/include

          mkdir -p ext/libv8-node
          echo '--- !ruby/object:Libv8::Node::Location::Vendor {}' >ext/libv8-node/.location.yml
        '';
      in
      attrs: {
        dontBuild = false;
        postPatch = ''
          cp ${noopScript} libexec/build-libv8
          cp ${noopScript} libexec/build-monolith
          cp ${noopScript} libexec/download-node
          cp ${noopScript} libexec/extract-node
          cp ${linkFiles} libexec/inject-libv8
        '';
      };
  };

shamelessly stolen from here: https://github.com/elohmeier/ptsd/blob/49f2a1c2a3e2693f4af8dc6beaffc222c9d11927/packages/dradis-ce/default.nix#L5