2020-04-24 23:36:52 +00:00
|
|
|
# This builds gems in a way that is compatible with bundler.
|
|
|
|
#
|
|
|
|
# Bundler installs gems from git sources _very_ differently from how RubyGems
|
|
|
|
# installes gem packages, though they both install gem packages similarly.
|
|
|
|
#
|
|
|
|
# We monkey-patch Bundler to remove any impurities and then drive its internals
|
|
|
|
# to install git gems.
|
|
|
|
#
|
|
|
|
# For the sake of simplicity, gem packages are installed with the standard `gem`
|
|
|
|
# program.
|
|
|
|
#
|
|
|
|
# Note that bundler does not support multiple prefixes; it assumes that all
|
|
|
|
# gems are installed in a common prefix, and has no support for specifying
|
|
|
|
# otherwise. Therefore, if you want to be able to use the resulting derivations
|
|
|
|
# with bundler, you need to create a symlink forrest first, which is what
|
|
|
|
# `bundlerEnv` does for you.
|
|
|
|
#
|
|
|
|
# Normal gem packages can be used outside of bundler; a binstub is created in
|
|
|
|
# $out/bin.
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
{ lib, fetchurl, fetchgit, makeWrapper, gitMinimal, libobjc
|
2020-04-24 23:36:52 +00:00
|
|
|
, ruby, bundler
|
|
|
|
} @ defs:
|
|
|
|
|
|
|
|
lib.makeOverridable (
|
|
|
|
|
|
|
|
{ name ? null
|
|
|
|
, gemName
|
|
|
|
, version ? null
|
|
|
|
, type ? "gem"
|
|
|
|
, document ? [] # e.g. [ "ri" "rdoc" ]
|
|
|
|
, platform ? "ruby"
|
|
|
|
, ruby ? defs.ruby
|
|
|
|
, stdenv ? ruby.stdenv
|
|
|
|
, namePrefix ? (let
|
|
|
|
rubyName = builtins.parseDrvName ruby.name;
|
2024-01-25 14:12:00 +00:00
|
|
|
in "${rubyName.name}${lib.versions.majorMinor rubyName.version}-")
|
2022-08-12 12:06:08 +00:00
|
|
|
, nativeBuildInputs ? []
|
2020-04-24 23:36:52 +00:00
|
|
|
, buildInputs ? []
|
|
|
|
, meta ? {}
|
|
|
|
, patches ? []
|
|
|
|
, gemPath ? []
|
|
|
|
, dontStrip ? false
|
|
|
|
# Assume we don't have to build unless strictly necessary (e.g. the source is a
|
|
|
|
# git checkout).
|
|
|
|
# If you need to apply patches, make sure to set `dontBuild = false`;
|
|
|
|
, dontBuild ? true
|
|
|
|
, dontInstallManpages ? false
|
|
|
|
, propagatedBuildInputs ? []
|
|
|
|
, propagatedUserEnvPkgs ? []
|
|
|
|
, buildFlags ? []
|
|
|
|
, passthru ? {}
|
2020-12-03 08:41:04 +00:00
|
|
|
# bundler expects gems to be stored in the cache directory for certain actions
|
|
|
|
# such as `bundler install --redownload`.
|
|
|
|
# At the cost of increasing the store size, you can keep the gems to have closer
|
|
|
|
# alignment with what Bundler expects.
|
|
|
|
, keepGemCache ? false
|
2020-04-24 23:36:52 +00:00
|
|
|
, ...} @ attrs:
|
|
|
|
|
|
|
|
let
|
|
|
|
src = attrs.src or (
|
|
|
|
if type == "gem" then
|
|
|
|
fetchurl {
|
|
|
|
urls = map (
|
2024-06-05 15:53:02 +00:00
|
|
|
remote: "${remote}/gems/${gemName}-${suffix}.gem"
|
2020-04-24 23:36:52 +00:00
|
|
|
) (attrs.source.remotes or [ "https://rubygems.org" ]);
|
|
|
|
inherit (attrs.source) sha256;
|
|
|
|
}
|
|
|
|
else if type == "git" then
|
|
|
|
fetchgit {
|
|
|
|
inherit (attrs.source) url rev sha256 fetchSubmodules;
|
|
|
|
}
|
|
|
|
else if type == "url" then
|
|
|
|
fetchurl attrs.source
|
|
|
|
else
|
|
|
|
throw "buildRubyGem: don't know how to build a gem of type \"${type}\""
|
|
|
|
);
|
2023-07-15 17:15:38 +00:00
|
|
|
|
|
|
|
# See: https://github.com/rubygems/rubygems/blob/7a7b234721c375874b7e22b1c5b14925b943f04e/bundler/lib/bundler/source/git.rb#L103
|
|
|
|
suffix =
|
|
|
|
if type == "git" then
|
|
|
|
builtins.substring 0 12 attrs.source.rev
|
2024-06-05 15:53:02 +00:00
|
|
|
else if platform != "ruby" then
|
|
|
|
"${version}-${platform}"
|
2023-07-15 17:15:38 +00:00
|
|
|
else
|
|
|
|
version;
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
documentFlag =
|
|
|
|
if document == []
|
|
|
|
then "-N"
|
|
|
|
else "--document ${lib.concatStringsSep "," document}";
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
stdenv.mkDerivation ((builtins.removeAttrs attrs ["source"]) // {
|
|
|
|
inherit ruby;
|
|
|
|
inherit dontBuild;
|
|
|
|
inherit dontStrip;
|
2023-07-15 17:15:38 +00:00
|
|
|
inherit suffix;
|
2023-02-02 18:25:31 +00:00
|
|
|
gemType = type;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
nativeBuildInputs = [
|
2020-04-24 23:36:52 +00:00
|
|
|
ruby makeWrapper
|
2021-03-20 04:20:00 +00:00
|
|
|
] ++ lib.optionals (type == "git") [ gitMinimal ]
|
2020-04-24 23:36:52 +00:00
|
|
|
++ lib.optionals (type != "gem") [ bundler ]
|
2022-08-12 12:06:08 +00:00
|
|
|
++ nativeBuildInputs;
|
|
|
|
|
|
|
|
buildInputs = [
|
|
|
|
ruby
|
|
|
|
] ++ lib.optionals stdenv.isDarwin [ libobjc ]
|
2020-04-24 23:36:52 +00:00
|
|
|
++ buildInputs;
|
|
|
|
|
|
|
|
#name = builtins.trace (attrs.name or "no attr.name" ) "${namePrefix}${gemName}-${version}";
|
2023-07-15 17:15:38 +00:00
|
|
|
name = attrs.name or "${namePrefix}${gemName}-${suffix}";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
inherit src;
|
|
|
|
|
|
|
|
|
|
|
|
unpackPhase = attrs.unpackPhase or ''
|
|
|
|
runHook preUnpack
|
|
|
|
|
|
|
|
if [[ -f $src && $src == *.gem ]]; then
|
|
|
|
if [[ -z "''${dontBuild-}" ]]; then
|
|
|
|
# we won't know the name of the directory that RubyGems creates,
|
|
|
|
# so we'll just use a glob to find it and move it over.
|
|
|
|
gempkg="$src"
|
|
|
|
sourceRoot=source
|
|
|
|
gem unpack $gempkg --target=container
|
|
|
|
cp -r container/* $sourceRoot
|
|
|
|
rm -r container
|
|
|
|
|
|
|
|
# copy out the original gemspec, for convenience during patching /
|
|
|
|
# overrides.
|
|
|
|
gem specification $gempkg --ruby > original.gemspec
|
|
|
|
gemspec=$(readlink -f .)/original.gemspec
|
|
|
|
else
|
|
|
|
gempkg="$src"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
# Fall back to the original thing for everything else.
|
|
|
|
dontBuild=""
|
|
|
|
preUnpack="" postUnpack="" unpackPhase
|
|
|
|
fi
|
|
|
|
|
|
|
|
runHook postUnpack
|
|
|
|
'';
|
|
|
|
|
2021-03-19 17:17:44 +00:00
|
|
|
# As of ruby 3.0, ruby headers require -fdeclspec when building with clang
|
|
|
|
# Introduced in https://github.com/ruby/ruby/commit/0958e19ffb047781fe1506760c7cbd8d7fe74e57
|
2023-07-15 17:15:38 +00:00
|
|
|
env.NIX_CFLAGS_COMPILE = toString (lib.optionals (ruby.rubyEngine == "ruby" && stdenv.cc.isClang && lib.versionAtLeast ruby.version.major "3") [
|
2021-03-19 17:17:44 +00:00
|
|
|
"-fdeclspec"
|
2023-03-04 12:14:45 +00:00
|
|
|
]);
|
2021-03-19 17:17:44 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
buildPhase = attrs.buildPhase or ''
|
|
|
|
runHook preBuild
|
|
|
|
|
2023-02-02 18:25:31 +00:00
|
|
|
if [[ "$gemType" == "gem" ]]; then
|
2020-04-24 23:36:52 +00:00
|
|
|
if [[ -z "$gemspec" ]]; then
|
|
|
|
gemspec="$(find . -name '*.gemspec')"
|
|
|
|
echo "found the following gemspecs:"
|
|
|
|
echo "$gemspec"
|
|
|
|
gemspec="$(echo "$gemspec" | head -n1)"
|
|
|
|
fi
|
|
|
|
|
|
|
|
exec 3>&1
|
|
|
|
output="$(gem build $gemspec | tee >(cat - >&3))"
|
|
|
|
exec 3>&-
|
|
|
|
|
|
|
|
gempkg=$(echo "$output" | grep -oP 'File: \K(.*)')
|
|
|
|
|
|
|
|
echo "gem package built: $gempkg"
|
2023-02-02 18:25:31 +00:00
|
|
|
elif [[ "$gemType" == "git" ]]; then
|
2020-04-24 23:36:52 +00:00
|
|
|
git init
|
|
|
|
# remove variations to improve the likelihood of a bit-reproducible output
|
|
|
|
rm -rf .git/logs/ .git/hooks/ .git/index .git/FETCH_HEAD .git/ORIG_HEAD .git/refs/remotes/origin/HEAD .git/config
|
|
|
|
# support `git ls-files`
|
|
|
|
git add .
|
|
|
|
fi
|
|
|
|
|
|
|
|
runHook postBuild
|
|
|
|
'';
|
|
|
|
|
|
|
|
# Note:
|
|
|
|
# We really do need to keep the $out/${ruby.gemPath}/cache.
|
|
|
|
# This is very important in order for many parts of RubyGems/Bundler to not blow up.
|
|
|
|
# See https://github.com/bundler/bundler/issues/3327
|
|
|
|
installPhase = attrs.installPhase or ''
|
|
|
|
runHook preInstall
|
|
|
|
|
|
|
|
export GEM_HOME=$out/${ruby.gemPath}
|
|
|
|
mkdir -p $GEM_HOME
|
|
|
|
|
|
|
|
echo "buildFlags: $buildFlags"
|
|
|
|
|
|
|
|
${lib.optionalString (type == "url") ''
|
|
|
|
ruby ${./nix-bundle-install.rb} \
|
|
|
|
"path" \
|
|
|
|
'${gemName}' \
|
|
|
|
'${version}' \
|
|
|
|
'${lib.escapeShellArgs buildFlags}'
|
|
|
|
''}
|
|
|
|
${lib.optionalString (type == "git") ''
|
|
|
|
ruby ${./nix-bundle-install.rb} \
|
|
|
|
"git" \
|
|
|
|
'${gemName}' \
|
|
|
|
'${version}' \
|
|
|
|
'${lib.escapeShellArgs buildFlags}' \
|
|
|
|
'${attrs.source.url}' \
|
|
|
|
'.' \
|
|
|
|
'${attrs.source.rev}'
|
|
|
|
''}
|
|
|
|
|
|
|
|
${lib.optionalString (type == "gem") ''
|
|
|
|
if [[ -z "$gempkg" ]]; then
|
|
|
|
echo "failure: \$gempkg path unspecified" 1>&2
|
|
|
|
exit 1
|
|
|
|
elif [[ ! -f "$gempkg" ]]; then
|
|
|
|
echo "failure: \$gempkg path invalid" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
gem install \
|
|
|
|
--local \
|
|
|
|
--force \
|
|
|
|
--http-proxy 'http://nodtd.invalid' \
|
|
|
|
--ignore-dependencies \
|
|
|
|
--install-dir "$GEM_HOME" \
|
|
|
|
--build-root '/' \
|
|
|
|
--backtrace \
|
|
|
|
--no-env-shebang \
|
|
|
|
${documentFlag} \
|
|
|
|
$gempkg $gemFlags -- $buildFlags
|
|
|
|
|
|
|
|
# looks like useless files which break build repeatability and consume space
|
|
|
|
pushd $out/${ruby.gemPath}
|
2021-04-26 19:14:03 +00:00
|
|
|
find doc/ -iname created.rid -delete -print
|
|
|
|
find gems/*/ext/ extensions/ \( -iname Makefile -o -iname mkmf.log -o -iname gem_make.out \) -delete -print
|
2023-08-04 22:07:22 +00:00
|
|
|
${lib.optionalString (!keepGemCache) "rm -fvr cache"}
|
2020-04-24 23:36:52 +00:00
|
|
|
popd
|
|
|
|
|
|
|
|
# write out metadata and binstubs
|
|
|
|
spec=$(echo $out/${ruby.gemPath}/specifications/*.gemspec)
|
|
|
|
ruby ${./gem-post-build.rb} "$spec"
|
|
|
|
''}
|
|
|
|
|
|
|
|
${lib.optionalString (!dontInstallManpages) ''
|
|
|
|
for section in {1..9}; do
|
|
|
|
mandir="$out/share/man/man$section"
|
|
|
|
find $out/lib \( -wholename "*/man/*.$section" -o -wholename "*/man/man$section/*.$section" \) \
|
|
|
|
-execdir mkdir -p $mandir \; -execdir cp '{}' $mandir \;
|
|
|
|
done
|
|
|
|
''}
|
|
|
|
|
|
|
|
runHook postInstall
|
|
|
|
'';
|
|
|
|
|
|
|
|
propagatedBuildInputs = gemPath ++ propagatedBuildInputs;
|
|
|
|
propagatedUserEnvPkgs = gemPath ++ propagatedUserEnvPkgs;
|
|
|
|
|
|
|
|
passthru = passthru // { isRubyGem = true; };
|
2021-07-17 21:14:59 +00:00
|
|
|
meta = {
|
|
|
|
# default to Ruby's platforms
|
|
|
|
platforms = ruby.meta.platforms;
|
2022-10-21 18:38:19 +00:00
|
|
|
mainProgram = gemName;
|
2021-07-17 21:14:59 +00:00
|
|
|
} // meta;
|
2020-04-24 23:36:52 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
)
|