From 30971173aba3105b42b9cc7976f1c6c9e018b56c Mon Sep 17 00:00:00 2001 From: Ivan Trubach Date: Sun, 26 Nov 2023 22:18:23 +0300 Subject: [PATCH] ruby-modules: improve cross-compilation support --- .../ruby-modules/bundled-common/default.nix | 8 ++++++-- .../ruby-modules/bundled-common/functions.nix | 17 +++++++++++++++-- .../bundled-common/gen-bin-stubs.rb | 1 + .../ruby-modules/bundler-env/default.nix | 9 +++++---- pkgs/development/ruby-modules/gem/default.nix | 10 ++++++++++ 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/pkgs/development/ruby-modules/bundled-common/default.nix b/pkgs/development/ruby-modules/bundled-common/default.nix index cd2f6f379efc..ad1d354f6e51 100644 --- a/pkgs/development/ruby-modules/bundled-common/default.nix +++ b/pkgs/development/ruby-modules/bundled-common/default.nix @@ -1,4 +1,8 @@ -{ stdenv, runCommand, ruby, lib, rsync +{ stdenv +, lib +, buildPackages +, runCommand +, ruby , defaultGemConfig, buildRubyGem, buildEnv , makeBinaryWrapper , bundler @@ -190,7 +194,7 @@ let runCommand name' basicEnvArgs '' mkdir -p $out for i in $paths; do - ${rsync}/bin/rsync -a $i/lib $out/ + ${buildPackages.rsync}/bin/rsync -a $i/lib $out/ done eval "$postBuild" '' diff --git a/pkgs/development/ruby-modules/bundled-common/functions.nix b/pkgs/development/ruby-modules/bundled-common/functions.nix index 57a10dadc400..3f9e49fc53be 100644 --- a/pkgs/development/ruby-modules/bundled-common/functions.nix +++ b/pkgs/development/ruby-modules/bundled-common/functions.nix @@ -59,8 +59,21 @@ in rec { then attrs // gemConfig.${attrs.gemName} attrs else attrs); - genStubsScript = { lib, ruby, confFiles, bundler, groups, binPaths, ... }: '' - ${ruby}/bin/ruby ${./gen-bin-stubs.rb} \ + genStubsScript = { lib, runCommand, ruby, confFiles, bundler, groups, binPaths, ... }: + let + genStubsScript = runCommand "gen-bin-stubs" + { + strictDeps = true; + nativeBuildInputs = [ ruby ]; + } + '' + cp ${./gen-bin-stubs.rb} $out + chmod +x $out + patchShebangs --build $out + ''; + in + '' + ${genStubsScript} \ "${ruby}/bin/ruby" \ "${confFiles}/Gemfile" \ "$out/${ruby.gemPath}" \ diff --git a/pkgs/development/ruby-modules/bundled-common/gen-bin-stubs.rb b/pkgs/development/ruby-modules/bundled-common/gen-bin-stubs.rb index 822996b7cbf6..d1d4ad954438 100644 --- a/pkgs/development/ruby-modules/bundled-common/gen-bin-stubs.rb +++ b/pkgs/development/ruby-modules/bundled-common/gen-bin-stubs.rb @@ -1,3 +1,4 @@ +#!/usr/bin/env ruby require 'rbconfig' require 'rubygems' require 'rubygems/specification' diff --git a/pkgs/development/ruby-modules/bundler-env/default.nix b/pkgs/development/ruby-modules/bundler-env/default.nix index faef3be4d911..35fecb70c005 100644 --- a/pkgs/development/ruby-modules/bundler-env/default.nix +++ b/pkgs/development/ruby-modules/bundler-env/default.nix @@ -1,5 +1,6 @@ { ruby, lib, callPackage, defaultGemConfig, buildEnv, runCommand -, bundler, rsync +, buildPackages +, bundler }@defs: { name ? null @@ -21,7 +22,7 @@ }@args: let - inherit (import ../bundled-common/functions.nix {inherit lib ruby gemConfig groups; }) genStubsScript; + inherit (import ../bundled-common/functions.nix { inherit lib ruby gemConfig groups; }) genStubsScript; basicEnv = (callPackage ../bundled-common { inherit bundler; }) (args // { inherit pname name; mainGemName = pname; }); @@ -51,7 +52,7 @@ in pathsToLink = [ "/lib" ]; postBuild = genStubsScript { - inherit lib ruby bundler groups; + inherit lib runCommand ruby bundler groups; confFiles = basicEnv.confFiles; binPaths = [ basicEnv.gems.${pname} ]; } + lib.optionalString (postBuild != null) postBuild; @@ -67,7 +68,7 @@ in runCommand basicEnv.name bundlerEnvArgs '' mkdir -p $out for i in $paths; do - ${rsync}/bin/rsync -a $i/lib $out/ + ${buildPackages.rsync}/bin/rsync -a $i/lib $out/ done eval "$postBuild" '' diff --git a/pkgs/development/ruby-modules/gem/default.nix b/pkgs/development/ruby-modules/gem/default.nix index 409c2c9d027b..2ddbcffd18de 100644 --- a/pkgs/development/ruby-modules/gem/default.nix +++ b/pkgs/development/ruby-modules/gem/default.nix @@ -252,6 +252,16 @@ stdenv.mkDerivation ((builtins.removeAttrs attrs ["source"]) // { runHook postInstall ''; + # For Ruby-generated binstubs, shebang paths are already in Nix store but for + # ruby used to build the package. Update them to match the host system. Note + # that patchShebangsAuto ignores scripts where shebang line is already in Nix + # store. + fixupPhase = attrs.fixupPhase or '' + runHook preFixup + patchShebangs --update --host $out/${ruby.gemPath}/bin + runHook postFixup + ''; + propagatedBuildInputs = gemPath ++ propagatedBuildInputs; propagatedUserEnvPkgs = gemPath ++ propagatedUserEnvPkgs;