3
0
Fork 0
forked from mirrors/nixpkgs

bundlerEnv: refactor

Add `pname` attribute: if passed, the derivation name defaults to the
gem name + version and only expose the gem's bin.

Add `gemdir` attribute: gives a default lookup path for the Gemfile,
Gemfile.lock and gemset.nix.

Set the `meta.platforms' to `ruby.meta.platforms' by default.
This commit is contained in:
zimbatm 2016-10-02 22:59:35 +01:00
parent 2d9148dc8a
commit 2a194746a5

View file

@ -6,7 +6,14 @@
, tree , tree
}@defs: }@defs:
{ name, gemset, gemfile, lockfile, ruby ? defs.ruby, gemConfig ? defaultGemConfig { name ? null
, pname ? null
, gemdir ? null
, gemfile ? null
, lockfile ? null
, gemset ? null
, ruby ? defs.ruby
, gemConfig ? defaultGemConfig
, postBuild ? null , postBuild ? null
, document ? [] , document ? []
, meta ? {} , meta ? {}
@ -16,54 +23,95 @@
}@args: }@args:
let let
importedGemset = import gemset; drvName =
if name != null then name
else if pname != null then "${toString pname}-${mainGem.version}"
else throw "bundlerEnv: either pname or name must be set";
mainGem =
if pname == null then null
else gems."${pname}" or (throw "bundlerEnv: gem ${pname} not found");
gemfile' =
if gemfile == null then gemdir + "/Gemfile"
else gemfile;
lockfile' =
if lockfile == null then gemdir + "/Gemfile.lock"
else lockfile;
gemset' =
if gemset == null then gemdir + "/gemset.nix"
else gemset;
importedGemset = import gemset';
filteredGemset = (lib.filterAttrs (name: attrs: filteredGemset = (lib.filterAttrs (name: attrs:
if (builtins.hasAttr "groups" attrs) if (builtins.hasAttr "groups" attrs)
then (builtins.any (gemGroup: builtins.any (group: group == gemGroup) groups) attrs.groups) then (builtins.any (gemGroup: builtins.any (group: group == gemGroup) groups) attrs.groups)
else true else true
) importedGemset); ) importedGemset);
applyGemConfigs = attrs: applyGemConfigs = attrs:
(if gemConfig ? "${attrs.gemName}" (if gemConfig ? "${attrs.gemName}"
then attrs // gemConfig."${attrs.gemName}" attrs then attrs // gemConfig."${attrs.gemName}" attrs
else attrs); else attrs);
configuredGemset = lib.flip lib.mapAttrs filteredGemset (name: attrs: configuredGemset = lib.flip lib.mapAttrs filteredGemset (name: attrs:
applyGemConfigs (attrs // { inherit ruby; gemName = name; }) applyGemConfigs (attrs // { inherit ruby; gemName = name; })
); );
hasBundler = builtins.hasAttr "bundler" filteredGemset; hasBundler = builtins.hasAttr "bundler" filteredGemset;
bundler = if hasBundler then gems.bundler else defs.bundler.override (attrs: { inherit ruby; });
bundler =
if hasBundler then gems.bundler
else defs.bundler.override (attrs: { inherit ruby; });
gems = lib.flip lib.mapAttrs configuredGemset (name: attrs: gems = lib.flip lib.mapAttrs configuredGemset (name: attrs:
buildRubyGem ((removeAttrs attrs ["source"]) // attrs.source // { buildRubyGem ((removeAttrs attrs ["source"]) // attrs.source // {
inherit ruby; inherit ruby;
gemName = name; gemName = name;
gemPath = map (gemName: gems."${gemName}") (attrs.dependencies or []); gemPath = map (gemName: gems."${gemName}") (attrs.dependencies or []);
})); }));
# We have to normalize the Gemfile.lock, otherwise bundler tries to be # We have to normalize the Gemfile.lock, otherwise bundler tries to be
# helpful by doing so at run time, causing executables to immediately bail # helpful by doing so at run time, causing executables to immediately bail
# out. Yes, I'm serious. # out. Yes, I'm serious.
confFiles = runCommand "gemfile-and-lockfile" {} '' confFiles = runCommand "gemfile-and-lockfile" {} ''
mkdir -p $out mkdir -p $out
cp ${gemfile} $out/Gemfile cp ${gemfile'} $out/Gemfile
cp ${lockfile} $out/Gemfile.lock cp ${lockfile'} $out/Gemfile.lock
''; '';
envPaths = lib.attrValues gems ++ lib.optional (!hasBundler) bundler; envPaths = lib.attrValues gems ++ lib.optional (!hasBundler) bundler;
binPaths = if mainGem != null then [ mainGem ] else envPaths;
bundlerEnv = buildEnv { bundlerEnv = buildEnv {
inherit name ignoreCollisions; inherit ignoreCollisions;
name = drvName;
paths = envPaths; paths = envPaths;
pathsToLink = [ "/lib" ]; pathsToLink = [ "/lib" ];
postBuild = '' postBuild = ''
${ruby}/bin/ruby ${./gen-bin-stubs.rb} \ ${ruby}/bin/ruby ${./gen-bin-stubs.rb} \
"${ruby}/bin/ruby" \ "${ruby}/bin/ruby" \
"${confFiles}/Gemfile" \ "${confFiles}/Gemfile" \
"$out/${ruby.gemPath}" \ "$out/${ruby.gemPath}" \
"${bundler}/${ruby.gemPath}" \ "${bundler}/${ruby.gemPath}" \
${lib.escapeShellArg envPaths} \ ${lib.escapeShellArg binPaths} \
${lib.escapeShellArg groups} ${lib.escapeShellArg groups}
'' + lib.optionalString (postBuild != null) postBuild; '' + lib.optionalString (postBuild != null) postBuild;
meta = { platforms = ruby.meta.platforms; } // meta;
passthru = rec { passthru = rec {
inherit ruby bundler meta gems; inherit ruby bundler gems;
wrappedRuby = stdenv.mkDerivation { wrappedRuby = stdenv.mkDerivation {
name = "wrapped-ruby-${name}"; name = "wrapped-ruby-${drvName}";
nativeBuildInputs = [ makeWrapper ]; nativeBuildInputs = [ makeWrapper ];
buildCommand = '' buildCommand = ''
mkdir -p $out/bin mkdir -p $out/bin
@ -87,7 +135,7 @@ let
require 'bundler/setup' require 'bundler/setup'
''; '';
in stdenv.mkDerivation { in stdenv.mkDerivation {
name = "interactive-${name}-environment"; name = "interactive-${drvName}-environment";
nativeBuildInputs = [ wrappedRuby bundlerEnv ]; nativeBuildInputs = [ wrappedRuby bundlerEnv ];
shellHook = '' shellHook = ''
export OLD_IRBRC="$IRBRC" export OLD_IRBRC="$IRBRC"
@ -102,7 +150,5 @@ let
}; };
}; };
}; };
in in
bundlerEnv
bundlerEnv