2024-07-31 10:19:44 +00:00
|
|
|
{ stdenv
|
|
|
|
, lib
|
|
|
|
, buildPackages
|
|
|
|
, runCommand
|
|
|
|
, ruby
|
2020-04-24 23:36:52 +00:00
|
|
|
, defaultGemConfig, buildRubyGem, buildEnv
|
2022-03-10 19:12:11 +00:00
|
|
|
, makeBinaryWrapper
|
2020-04-24 23:36:52 +00:00
|
|
|
, bundler
|
|
|
|
}@defs:
|
|
|
|
|
|
|
|
{
|
|
|
|
name ? null
|
|
|
|
, pname ? null
|
|
|
|
, mainGemName ? null
|
|
|
|
, gemdir ? null
|
|
|
|
, gemfile ? null
|
|
|
|
, lockfile ? null
|
|
|
|
, gemset ? null
|
|
|
|
, ruby ? defs.ruby
|
|
|
|
, copyGemFiles ? false # Copy gem files instead of symlinking
|
|
|
|
, gemConfig ? defaultGemConfig
|
|
|
|
, postBuild ? null
|
|
|
|
, document ? []
|
|
|
|
, meta ? {}
|
|
|
|
, groups ? null
|
|
|
|
, ignoreCollisions ? false
|
2022-09-09 14:08:57 +00:00
|
|
|
, nativeBuildInputs ? []
|
2020-04-24 23:36:52 +00:00
|
|
|
, buildInputs ? []
|
2022-08-12 12:06:08 +00:00
|
|
|
, extraConfigPaths ? []
|
2024-06-20 14:57:18 +00:00
|
|
|
, passthru ? {}
|
2020-04-24 23:36:52 +00:00
|
|
|
, ...
|
|
|
|
}@args:
|
|
|
|
|
|
|
|
assert name == null -> pname != null;
|
|
|
|
|
|
|
|
let
|
2024-04-21 15:54:59 +00:00
|
|
|
functions = import ./functions.nix { inherit lib gemConfig; };
|
|
|
|
|
|
|
|
inherit (functions)
|
|
|
|
applyGemConfigs
|
|
|
|
bundlerFiles
|
|
|
|
composeGemAttrs
|
|
|
|
filterGemset
|
|
|
|
genStubsScript
|
|
|
|
pathDerivation
|
|
|
|
;
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
gemFiles = bundlerFiles args;
|
|
|
|
|
|
|
|
importedGemset = if builtins.typeOf gemFiles.gemset != "set"
|
|
|
|
then import gemFiles.gemset
|
|
|
|
else gemFiles.gemset;
|
|
|
|
|
|
|
|
filteredGemset = filterGemset { inherit ruby groups; } importedGemset;
|
|
|
|
|
|
|
|
configuredGemset = lib.flip lib.mapAttrs filteredGemset (name: attrs:
|
2021-10-01 09:20:50 +00:00
|
|
|
applyGemConfigs (attrs // { inherit ruby document; gemName = name; })
|
2020-04-24 23:36:52 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
hasBundler = builtins.hasAttr "bundler" filteredGemset;
|
|
|
|
|
|
|
|
bundler =
|
|
|
|
if hasBundler then gems.bundler
|
|
|
|
else defs.bundler.override (attrs: { inherit ruby; });
|
|
|
|
|
|
|
|
gems = lib.flip lib.mapAttrs configuredGemset (name: attrs: buildGem name attrs);
|
|
|
|
|
|
|
|
name' = if name != null then
|
|
|
|
name
|
|
|
|
else
|
|
|
|
let
|
|
|
|
gem = gems.${pname};
|
2023-07-15 17:15:38 +00:00
|
|
|
suffix = gem.suffix;
|
2020-04-24 23:36:52 +00:00
|
|
|
in
|
2023-07-15 17:15:38 +00:00
|
|
|
"${pname}-${suffix}";
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
pname' = if pname != null then
|
|
|
|
pname
|
|
|
|
else
|
|
|
|
name;
|
|
|
|
|
|
|
|
copyIfBundledByPath = { bundledByPath ? false, ...}:
|
2023-02-16 17:41:37 +00:00
|
|
|
(lib.optionalString bundledByPath (
|
|
|
|
assert gemFiles.gemdir != null; "cp -a ${gemFiles.gemdir}/* $out/") #*/
|
2020-04-24 23:36:52 +00:00
|
|
|
);
|
|
|
|
|
2023-08-04 22:07:22 +00:00
|
|
|
maybeCopyAll = pkgname: lib.optionalString (pkgname != null) (
|
|
|
|
let
|
|
|
|
mainGem = gems.${pkgname} or (throw "bundlerEnv: gem ${pkgname} not found");
|
|
|
|
in
|
|
|
|
copyIfBundledByPath mainGem
|
|
|
|
);
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
# We have to normalize the Gemfile.lock, otherwise bundler tries to be
|
|
|
|
# helpful by doing so at run time, causing executables to immediately bail
|
|
|
|
# out. Yes, I'm serious.
|
|
|
|
confFiles = runCommand "gemfile-and-lockfile" {} ''
|
|
|
|
mkdir -p $out
|
|
|
|
${maybeCopyAll mainGemName}
|
|
|
|
cp ${gemFiles.gemfile} $out/Gemfile || ls -l $out/Gemfile
|
|
|
|
cp ${gemFiles.lockfile} $out/Gemfile.lock || ls -l $out/Gemfile.lock
|
2022-08-12 12:06:08 +00:00
|
|
|
|
|
|
|
${lib.concatMapStringsSep "\n" (path: "cp -r ${path} $out/") extraConfigPaths}
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
buildGem = name: attrs: (
|
|
|
|
let
|
|
|
|
gemAttrs = composeGemAttrs ruby gems name attrs;
|
|
|
|
in
|
|
|
|
if gemAttrs.type == "path" then
|
|
|
|
pathDerivation (gemAttrs.source // gemAttrs)
|
|
|
|
else
|
|
|
|
buildRubyGem gemAttrs
|
|
|
|
);
|
|
|
|
|
|
|
|
envPaths = lib.attrValues gems ++ lib.optional (!hasBundler) bundler;
|
|
|
|
|
|
|
|
|
|
|
|
basicEnvArgs = {
|
2022-09-09 14:08:57 +00:00
|
|
|
inherit nativeBuildInputs buildInputs ignoreCollisions;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
name = name';
|
|
|
|
|
|
|
|
paths = envPaths;
|
|
|
|
pathsToLink = [ "/lib" ];
|
|
|
|
|
|
|
|
postBuild = genStubsScript (defs // args // {
|
|
|
|
inherit confFiles bundler groups;
|
|
|
|
binPaths = envPaths;
|
|
|
|
}) + lib.optionalString (postBuild != null) postBuild;
|
|
|
|
|
|
|
|
meta = { platforms = ruby.meta.platforms; } // meta;
|
|
|
|
|
2024-01-13 08:15:51 +00:00
|
|
|
passthru = (lib.optionalAttrs (pname != null) {
|
2023-07-15 17:15:38 +00:00
|
|
|
inherit (gems.${pname}) gemType;
|
2024-01-13 08:15:51 +00:00
|
|
|
} // rec {
|
|
|
|
inherit ruby bundler gems confFiles envPaths;
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
wrappedRuby = stdenv.mkDerivation {
|
|
|
|
name = "wrapped-ruby-${pname'}";
|
2022-03-10 19:12:11 +00:00
|
|
|
|
|
|
|
nativeBuildInputs = [ makeBinaryWrapper ];
|
|
|
|
|
|
|
|
dontUnpack = true;
|
|
|
|
|
|
|
|
buildPhase = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
mkdir -p $out/bin
|
|
|
|
for i in ${ruby}/bin/*; do
|
|
|
|
makeWrapper "$i" $out/bin/$(basename "$i") \
|
|
|
|
--set BUNDLE_GEMFILE ${confFiles}/Gemfile \
|
|
|
|
--unset BUNDLE_PATH \
|
|
|
|
--set BUNDLE_FROZEN 1 \
|
|
|
|
--set GEM_HOME ${basicEnv}/${ruby.gemPath} \
|
|
|
|
--set GEM_PATH ${basicEnv}/${ruby.gemPath}
|
|
|
|
done
|
|
|
|
'';
|
2022-03-10 19:12:11 +00:00
|
|
|
|
|
|
|
dontInstall = true;
|
|
|
|
|
|
|
|
doCheck = true;
|
|
|
|
checkPhase = ''
|
|
|
|
$out/bin/ruby --help > /dev/null
|
|
|
|
'';
|
|
|
|
|
|
|
|
inherit (ruby) meta;
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
env = let
|
|
|
|
irbrc = builtins.toFile "irbrc" ''
|
|
|
|
if !(ENV["OLD_IRBRC"].nil? || ENV["OLD_IRBRC"].empty?)
|
|
|
|
require ENV["OLD_IRBRC"]
|
|
|
|
end
|
|
|
|
require 'rubygems'
|
|
|
|
require 'bundler/setup'
|
|
|
|
'';
|
|
|
|
in stdenv.mkDerivation {
|
|
|
|
name = "${pname'}-interactive-environment";
|
|
|
|
nativeBuildInputs = [ wrappedRuby basicEnv ];
|
|
|
|
shellHook = ''
|
|
|
|
export OLD_IRBRC=$IRBRC
|
|
|
|
export IRBRC=${irbrc}
|
|
|
|
'';
|
|
|
|
buildCommand = ''
|
|
|
|
echo >&2 ""
|
|
|
|
echo >&2 "*** Ruby 'env' attributes are intended for interactive nix-shell sessions, not for building! ***"
|
|
|
|
echo >&2 ""
|
|
|
|
exit 1
|
|
|
|
'';
|
|
|
|
};
|
2024-06-20 14:57:18 +00:00
|
|
|
} // passthru);
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
basicEnv =
|
|
|
|
if copyGemFiles then
|
|
|
|
runCommand name' basicEnvArgs ''
|
|
|
|
mkdir -p $out
|
|
|
|
for i in $paths; do
|
2024-07-31 10:19:44 +00:00
|
|
|
${buildPackages.rsync}/bin/rsync -a $i/lib $out/
|
2020-04-24 23:36:52 +00:00
|
|
|
done
|
|
|
|
eval "$postBuild"
|
|
|
|
''
|
|
|
|
else
|
|
|
|
buildEnv basicEnvArgs;
|
|
|
|
in
|
|
|
|
basicEnv
|