2020-03-03 19:07:32 +00:00
|
|
|
{ stdenv, runCommand, ruby, lib, rsync
|
2017-04-25 01:45:00 +00:00
|
|
|
, defaultGemConfig, buildRubyGem, buildEnv
|
2022-02-22 00:12:57 +00:00
|
|
|
, makeBinaryWrapper
|
2017-04-25 01:45:00 +00:00
|
|
|
, bundler
|
|
|
|
}@defs:
|
|
|
|
|
|
|
|
{
|
2017-06-28 05:33:18 +00:00
|
|
|
name ? null
|
|
|
|
, pname ? null
|
2017-06-26 00:40:22 +00:00
|
|
|
, mainGemName ? null
|
2017-06-09 16:04:33 +00:00
|
|
|
, gemdir ? null
|
|
|
|
, gemfile ? null
|
|
|
|
, lockfile ? null
|
|
|
|
, gemset ? null
|
2017-04-25 01:45:00 +00:00
|
|
|
, ruby ? defs.ruby
|
2020-03-03 19:07:32 +00:00
|
|
|
, copyGemFiles ? false # Copy gem files instead of symlinking
|
2017-04-25 01:45:00 +00:00
|
|
|
, gemConfig ? defaultGemConfig
|
|
|
|
, postBuild ? null
|
|
|
|
, document ? []
|
|
|
|
, meta ? {}
|
2018-12-11 21:19:32 +00:00
|
|
|
, groups ? null
|
2017-04-25 01:45:00 +00:00
|
|
|
, ignoreCollisions ? false
|
bundlerApp: take buildInputs (#45435)
It would be reasonable to have a Ruby program that depends on some other
program being in the PATH. In this case, the obvious thing to do would
be something like this:
bundlerApp {
# ...
buildInputs = [ makeWrapper ];
postBuild = ''
wrapProgram "$out/bin/foo" \
--prefix PATH : ${lib.makeBinPath [ dep ]}
'';
}
However, this doesn't work, because even though it just forwards most of
its arguments to `runCommand`, `bundlerApp` won't take a `buildInputs`
parameter. It doesn't even specify its own `buildInputs`, which means
that the `scripts` parameter to `bundlerApp` (which depends on
`makeWrapper`) is completely broken, and, as far as I can tell, has been
since its inception. I've added a `makeWrapper` build input if the
scripts parameter is present to fix this.
I've added a `buildInputs` option to `bundlerApp`. It's also passed
through to bundled-common because `postBuild` scripts are run there as
well. This actually means that in this example we'd end up going through
two layers of wrappers (one from `bundlerApp` and one from
bundled-common), but that has always been the case and isn't likely to
break anything. That oddity does suggest that it might be prudent to
not forward `postBuild` to bundled-common (or to at least use a
different option) though...
FWIW, as far as I can tell no package in nixpkgs uses either the
`scripts` or `postBuild` options to `bundlerApp`.
2018-10-29 21:39:51 +00:00
|
|
|
, buildInputs ? []
|
2017-04-25 01:45:00 +00:00
|
|
|
, ...
|
|
|
|
}@args:
|
|
|
|
|
2017-06-28 05:33:18 +00:00
|
|
|
assert name == null -> pname != null;
|
|
|
|
|
2017-05-10 17:00:21 +00:00
|
|
|
with import ./functions.nix { inherit lib gemConfig; };
|
2017-04-25 01:45:00 +00:00
|
|
|
|
|
|
|
let
|
2017-06-09 16:04:33 +00:00
|
|
|
gemFiles = bundlerFiles args;
|
2017-04-25 01:45:00 +00:00
|
|
|
|
2018-04-15 10:53:15 +00:00
|
|
|
importedGemset = if builtins.typeOf gemFiles.gemset != "set"
|
2017-10-05 08:28:45 +00:00
|
|
|
then import gemFiles.gemset
|
|
|
|
else gemFiles.gemset;
|
2017-04-25 01:45:00 +00:00
|
|
|
|
2017-05-10 17:00:21 +00:00
|
|
|
filteredGemset = filterGemset { inherit ruby groups; } importedGemset;
|
2017-04-25 01:45:00 +00:00
|
|
|
|
|
|
|
configuredGemset = lib.flip lib.mapAttrs filteredGemset (name: attrs:
|
2021-09-18 10:58:24 +00:00
|
|
|
applyGemConfigs (attrs // { inherit ruby document; gemName = name; })
|
2017-04-25 01:45:00 +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);
|
|
|
|
|
2017-06-28 05:33:18 +00:00
|
|
|
name' = if name != null then
|
|
|
|
name
|
|
|
|
else
|
|
|
|
let
|
2019-08-13 21:52:01 +00:00
|
|
|
gem = gems.${pname};
|
2017-06-28 05:33:18 +00:00
|
|
|
version = gem.version;
|
|
|
|
in
|
|
|
|
"${pname}-${version}";
|
|
|
|
|
|
|
|
pname' = if pname != null then
|
|
|
|
pname
|
|
|
|
else
|
|
|
|
name;
|
|
|
|
|
2018-07-20 19:54:05 +00:00
|
|
|
copyIfBundledByPath = { bundledByPath ? false, ...}:
|
2017-06-09 16:04:33 +00:00
|
|
|
(if bundledByPath then
|
2017-07-29 22:03:24 +00:00
|
|
|
assert gemFiles.gemdir != null; "cp -a ${gemFiles.gemdir}/* $out/" #*/
|
2017-06-09 16:04:33 +00:00
|
|
|
else ""
|
2017-04-25 01:45:00 +00:00
|
|
|
);
|
|
|
|
|
2017-06-26 00:40:22 +00:00
|
|
|
maybeCopyAll = pkgname: if pkgname == null then "" else
|
2017-05-01 16:07:42 +00:00
|
|
|
let
|
2019-08-13 21:52:01 +00:00
|
|
|
mainGem = gems.${pkgname} or (throw "bundlerEnv: gem ${pkgname} not found");
|
2017-05-01 16:07:42 +00:00
|
|
|
in
|
|
|
|
copyIfBundledByPath mainGem;
|
2017-04-25 01:45:00 +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
|
2017-06-26 00:40:22 +00:00
|
|
|
${maybeCopyAll mainGemName}
|
2017-06-09 16:04:33 +00:00
|
|
|
cp ${gemFiles.gemfile} $out/Gemfile || ls -l $out/Gemfile
|
|
|
|
cp ${gemFiles.lockfile} $out/Gemfile.lock || ls -l $out/Gemfile.lock
|
2017-04-25 01:45:00 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
buildGem = name: attrs: (
|
|
|
|
let
|
2017-05-10 17:00:21 +00:00
|
|
|
gemAttrs = composeGemAttrs ruby gems name attrs;
|
2017-04-25 01:45:00 +00:00
|
|
|
in
|
|
|
|
if gemAttrs.type == "path" then
|
2018-11-25 12:38:39 +00:00
|
|
|
pathDerivation (gemAttrs.source // gemAttrs)
|
2017-04-25 01:45:00 +00:00
|
|
|
else
|
2017-05-10 17:00:21 +00:00
|
|
|
buildRubyGem gemAttrs
|
2017-04-25 01:45:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
envPaths = lib.attrValues gems ++ lib.optional (!hasBundler) bundler;
|
|
|
|
|
2020-03-03 19:07:32 +00:00
|
|
|
|
|
|
|
basicEnvArgs = {
|
bundlerApp: take buildInputs (#45435)
It would be reasonable to have a Ruby program that depends on some other
program being in the PATH. In this case, the obvious thing to do would
be something like this:
bundlerApp {
# ...
buildInputs = [ makeWrapper ];
postBuild = ''
wrapProgram "$out/bin/foo" \
--prefix PATH : ${lib.makeBinPath [ dep ]}
'';
}
However, this doesn't work, because even though it just forwards most of
its arguments to `runCommand`, `bundlerApp` won't take a `buildInputs`
parameter. It doesn't even specify its own `buildInputs`, which means
that the `scripts` parameter to `bundlerApp` (which depends on
`makeWrapper`) is completely broken, and, as far as I can tell, has been
since its inception. I've added a `makeWrapper` build input if the
scripts parameter is present to fix this.
I've added a `buildInputs` option to `bundlerApp`. It's also passed
through to bundled-common because `postBuild` scripts are run there as
well. This actually means that in this example we'd end up going through
two layers of wrappers (one from `bundlerApp` and one from
bundled-common), but that has always been the case and isn't likely to
break anything. That oddity does suggest that it might be prudent to
not forward `postBuild` to bundled-common (or to at least use a
different option) though...
FWIW, as far as I can tell no package in nixpkgs uses either the
`scripts` or `postBuild` options to `bundlerApp`.
2018-10-29 21:39:51 +00:00
|
|
|
inherit buildInputs ignoreCollisions;
|
2017-04-25 01:45:00 +00:00
|
|
|
|
2017-06-28 05:33:18 +00:00
|
|
|
name = name';
|
2017-05-15 16:36:30 +00:00
|
|
|
|
2017-04-25 01:45:00 +00:00
|
|
|
paths = envPaths;
|
|
|
|
pathsToLink = [ "/lib" ];
|
|
|
|
|
2017-05-04 03:27:42 +00:00
|
|
|
postBuild = genStubsScript (defs // args // {
|
|
|
|
inherit confFiles bundler groups;
|
2017-04-25 01:45:00 +00:00
|
|
|
binPaths = envPaths;
|
2017-05-04 03:27:42 +00:00
|
|
|
}) + lib.optionalString (postBuild != null) postBuild;
|
2017-04-25 01:45:00 +00:00
|
|
|
|
|
|
|
meta = { platforms = ruby.meta.platforms; } // meta;
|
|
|
|
|
|
|
|
passthru = rec {
|
2017-07-29 22:03:24 +00:00
|
|
|
inherit ruby bundler gems confFiles envPaths;
|
2017-04-25 01:45:00 +00:00
|
|
|
|
2017-07-29 22:03:24 +00:00
|
|
|
wrappedRuby = stdenv.mkDerivation {
|
|
|
|
name = "wrapped-ruby-${pname'}";
|
2022-02-22 00:12:57 +00:00
|
|
|
nativeBuildInputs = [ makeBinaryWrapper ];
|
2021-10-07 10:03:26 +00:00
|
|
|
inherit (ruby) gemPath meta;
|
2017-04-25 01:45:00 +00:00
|
|
|
buildCommand = ''
|
|
|
|
mkdir -p $out/bin
|
|
|
|
for i in ${ruby}/bin/*; do
|
|
|
|
makeWrapper "$i" $out/bin/$(basename "$i") \
|
|
|
|
--set BUNDLE_GEMFILE ${confFiles}/Gemfile \
|
2020-04-03 21:52:14 +00:00
|
|
|
--unset BUNDLE_PATH \
|
2017-04-25 01:45:00 +00:00
|
|
|
--set BUNDLE_FROZEN 1 \
|
2017-05-01 16:07:42 +00:00
|
|
|
--set GEM_HOME ${basicEnv}/${ruby.gemPath} \
|
|
|
|
--set GEM_PATH ${basicEnv}/${ruby.gemPath}
|
2017-04-25 01:45:00 +00:00
|
|
|
done
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
2017-07-29 22:03:24 +00:00
|
|
|
name = "${pname'}-interactive-environment";
|
2017-05-01 16:07:42 +00:00
|
|
|
nativeBuildInputs = [ wrappedRuby basicEnv ];
|
2017-04-25 01:45:00 +00:00
|
|
|
shellHook = ''
|
2017-07-03 00:18:58 +00:00
|
|
|
export OLD_IRBRC=$IRBRC
|
2017-04-25 01:45:00 +00:00
|
|
|
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
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2017-05-01 16:07:42 +00:00
|
|
|
};
|
2020-03-03 19:07:32 +00:00
|
|
|
|
|
|
|
basicEnv =
|
|
|
|
if copyGemFiles then
|
|
|
|
runCommand name' basicEnvArgs ''
|
|
|
|
mkdir -p $out
|
|
|
|
for i in $paths; do
|
|
|
|
${rsync}/bin/rsync -a $i/lib $out/
|
|
|
|
done
|
|
|
|
eval "$postBuild"
|
|
|
|
''
|
|
|
|
else
|
|
|
|
buildEnv basicEnvArgs;
|
2017-05-01 16:07:42 +00:00
|
|
|
in
|
|
|
|
basicEnv
|