3
0
Fork 0
forked from mirrors/nixpkgs
nixpkgs/pkgs/development/ruby-modules/bundled-common/default.nix

142 lines
3.8 KiB
Nix
Raw Normal View History

2017-04-25 02:45:00 +01:00
{ stdenv, runCommand, ruby, lib
, defaultGemConfig, buildRubyGem, buildEnv
, makeWrapper
, bundler
}@defs:
{
2017-05-15 17:36:30 +01:00
name
, pname ? name
, gemdir ? null
, gemfile ? null
, lockfile ? null
, gemset ? null
2017-04-25 02:45:00 +01:00
, ruby ? defs.ruby
, gemConfig ? defaultGemConfig
, postBuild ? null
, document ? []
, meta ? {}
, groups ? ["default"]
, ignoreCollisions ? false
, ...
}@args:
with import ./functions.nix { inherit lib gemConfig; };
2017-04-25 02:45:00 +01:00
let
gemFiles = bundlerFiles args;
2017-04-25 02:45:00 +01:00
importedGemset = import gemFiles.gemset;
2017-04-25 02:45:00 +01:00
filteredGemset = filterGemset { inherit ruby groups; } importedGemset;
2017-04-25 02:45:00 +01:00
configuredGemset = lib.flip lib.mapAttrs filteredGemset (name: attrs:
applyGemConfigs (attrs // { inherit ruby; gemName = name; })
);
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);
copyIfBundledByPath = { bundledByPath ? false, ...}@main:
(if bundledByPath then
assert gemFiles.gemdir != nil; "cp -a ${gemFiles.gemdir}/* $out/"
else ""
2017-04-25 02:45:00 +01:00
);
2017-05-01 17:07:42 +01:00
maybeCopyAll = pname: if pname == null then "" else
let
mainGem = gems."${pname}" or (throw "bundlerEnv: gem ${pname} not found");
in
copyIfBundledByPath mainGem;
2017-04-25 02:45:00 +01: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-05-01 17:07:42 +01:00
${maybeCopyAll pname}
cp ${gemFiles.gemfile} $out/Gemfile || ls -l $out/Gemfile
cp ${gemFiles.lockfile} $out/Gemfile.lock || ls -l $out/Gemfile.lock
2017-04-25 02:45:00 +01:00
'';
buildGem = name: attrs: (
let
gemAttrs = composeGemAttrs ruby gems name attrs;
2017-04-25 02:45:00 +01:00
in
if gemAttrs.type == "path" then
pathDerivation gemAttrs
else
buildRubyGem gemAttrs
2017-04-25 02:45:00 +01:00
);
envPaths = lib.attrValues gems ++ lib.optional (!hasBundler) bundler;
2017-05-01 17:07:42 +01:00
basicEnv = buildEnv {
2017-04-25 02:45:00 +01:00
inherit ignoreCollisions;
2017-05-15 17:36:30 +01:00
name = if name == null then pname else name;
#name = pname;
2017-04-25 02:45:00 +01:00
paths = envPaths;
pathsToLink = [ "/lib" ];
postBuild = genStubsScript (defs // args // {
inherit confFiles bundler groups;
2017-04-25 02:45:00 +01:00
binPaths = envPaths;
}) + lib.optionalString (postBuild != null) postBuild;
2017-04-25 02:45:00 +01:00
meta = { platforms = ruby.meta.platforms; } // meta;
passthru = rec {
2017-05-29 13:27:41 +01:00
inherit ruby bundler gems mainGem confFiles envPaths;
2017-04-25 02:45:00 +01:00
2017-05-15 17:36:30 +01:00
wrappedRuby =
stdenv.mkDerivation {
2017-05-01 17:07:42 +01:00
name = "wrapped-ruby-${pname}";
2017-04-25 02:45:00 +01:00
nativeBuildInputs = [ makeWrapper ];
buildCommand = ''
mkdir -p $out/bin
for i in ${ruby}/bin/*; do
makeWrapper "$i" $out/bin/$(basename "$i") \
--set BUNDLE_GEMFILE ${confFiles}/Gemfile \
2017-05-01 17:07:42 +01:00
--set BUNDLE_PATH ${basicEnv}/${ruby.gemPath} \
2017-04-25 02:45:00 +01:00
--set BUNDLE_FROZEN 1 \
2017-05-01 17:07:42 +01:00
--set GEM_HOME ${basicEnv}/${ruby.gemPath} \
--set GEM_PATH ${basicEnv}/${ruby.gemPath}
2017-04-25 02:45:00 +01: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-05-01 17:07:42 +01:00
name = "${pname}-interactive-environment";
nativeBuildInputs = [ wrappedRuby basicEnv ];
2017-04-25 02:45:00 +01:00
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
'';
};
};
2017-05-01 17:07:42 +01:00
};
in
basicEnv