@@ -1,9 +1,6 @@
11{ stdenv, runCommand, writeText, writeScript, writeScriptBin, ruby, lib
22, callPackage, defaultGemConfig, fetchurl, fetchgit, buildRubyGem, buildEnv
3−, git
4−, makeWrapper
5−, bundler
6−, tree
3+, linkFarm, git, makeWrapper, bundler, tree
74}@defs:
85
96{ name ? null
@@ -12,143 +9,54 @@
129, gemfile ? null
1310, lockfile ? null
1411, gemset ? null
12+, groups ? ["default"]
1513, ruby ? defs.ruby
1614, gemConfig ? defaultGemConfig
1715, postBuild ? null
1816, document ? []
1917, meta ? {}
20−, groups ? ["default"]
2118, ignoreCollisions ? false
2219, ...
2320}@args:
2421
2522let
26− drvName =
27− if name != null then name
28− else if pname != null then "${toString pname}-${mainGem.version}"
29− else throw "bundlerEnv: either pname or name must be set";
30−
31− mainGem =
32− if pname == null then null
33− else gems."${pname}" or (throw "bundlerEnv: gem ${pname} not found");
34−
35− gemfile' =
36− if gemfile == null then gemdir + "/Gemfile"
37− else gemfile;
38−
39− lockfile' =
40− if lockfile == null then gemdir + "/Gemfile.lock"
41− else lockfile;
42−
43− gemset' =
44− if gemset == null then gemdir + "/gemset.nix"
45− else gemset;
46−
47− importedGemset = import gemset';
48−
49− filteredGemset = (lib.filterAttrs (name: attrs:
50− if (builtins.hasAttr "groups" attrs)
51− then (builtins.any (gemGroup: builtins.any (group: group == gemGroup) groups) attrs.groups)
52− else true
53− ) importedGemset);
54−
55− applyGemConfigs = attrs:
56− (if gemConfig ? "${attrs.gemName}"
57− then attrs // gemConfig."${attrs.gemName}" attrs
58− else attrs);
59−
60− configuredGemset = lib.flip lib.mapAttrs filteredGemset (name: attrs:
61− applyGemConfigs (attrs // { inherit ruby; gemName = name; })
62− );
63−
64− hasBundler = builtins.hasAttr "bundler" filteredGemset;
23+ inherit (import ../bundled-common/functions.nix {inherit lib ruby gemConfig groups; }) genStubsScript;
6524
66− bundler =
67− if hasBundler then gems.bundler
68− else defs.bundler.override (attrs: { inherit ruby; });
25+ basicEnv = (callPackage ../bundled-common {}) (args // { inherit pname name; mainGemName = pname; });
6926
70− gems = lib.flip lib.mapAttrs configuredGemset (name: attrs:
71− buildRubyGem ((removeAttrs attrs ["source"]) // attrs.source // {
72− inherit ruby;
73− gemName = name;
74− gemPath = map (gemName: gems."${gemName}") (attrs.dependencies or []);
75− }));
27+ inherit (basicEnv) envPaths;
28+ # Idea here is a mkDerivation that gen-bin-stubs new stubs "as specified" -
29+ # either specific executables or the bin/ for certain gem(s), but
30+ # incorporates the basicEnv as a requirement so that its $out is in our path.
7631
77− # We have to normalize the Gemfile.lock, otherwise bundler tries to be
78− # helpful by doing so at run time, causing executables to immediately bail
79− # out. Yes, I'm serious.
80− confFiles = runCommand "gemfile-and-lockfile" {} ''
81− mkdir -p $out
82− cp ${gemfile'} $out/Gemfile
83− cp ${lockfile'} $out/Gemfile.lock
84− '';
32+ # When stubbing the bins for a gem, we should use the gem expression
33+ # directly, which means that basicEnv should somehow make it available.
8534
86− envPaths = lib.attrValues gems ++ lib.optional (!hasBundler) bundler;
35+ # Different use cases should use different variations on this file, rather
36+ # than the expression trying to deduce a use case.
8737
88− binPaths = if mainGem != null then [ mainGem ] else envPaths;
89−
90− bundlerEnv = buildEnv {
91− inherit ignoreCollisions;
92−
93− name = drvName;
94−
95− paths = envPaths;
96− pathsToLink = [ "/lib" ];
97−
98− postBuild = ''
99− ${ruby}/bin/ruby ${./gen-bin-stubs.rb} \
100− "${ruby}/bin/ruby" \
101− "${confFiles}/Gemfile" \
102− "$out/${ruby.gemPath}" \
103− "${bundler}/${ruby.gemPath}" \
104− ${lib.escapeShellArg binPaths} \
105− ${lib.escapeShellArg groups}
106− '' + lib.optionalString (postBuild != null) postBuild;
107−
108− meta = { platforms = ruby.meta.platforms; } // meta;
109−
110− passthru = rec {
111− inherit ruby bundler gems;
112−
113− wrappedRuby = stdenv.mkDerivation {
114− name = "wrapped-ruby-${drvName}";
115− nativeBuildInputs = [ makeWrapper ];
116− buildCommand = ''
117− mkdir -p $out/bin
118− for i in ${ruby}/bin/*; do
119− makeWrapper "$i" $out/bin/$(basename "$i") \
120− --set BUNDLE_GEMFILE ${confFiles}/Gemfile \
121− --set BUNDLE_PATH ${bundlerEnv}/${ruby.gemPath} \
122− --set BUNDLE_FROZEN 1 \
123− --set GEM_HOME ${bundlerEnv}/${ruby.gemPath} \
124− --set GEM_PATH ${bundlerEnv}/${ruby.gemPath}
125− done
126− '';
127− };
128−
129− env = let
130− irbrc = builtins.toFile "irbrc" ''
131− if !(ENV["OLD_IRBRC"].nil? || ENV["OLD_IRBRC"].empty?)
132− require ENV["OLD_IRBRC"]
133− end
134− require 'rubygems'
135− require 'bundler/setup'
136− '';
137− in stdenv.mkDerivation {
138− name = "interactive-${drvName}-environment";
139− nativeBuildInputs = [ wrappedRuby bundlerEnv ];
140− shellHook = ''
141− export OLD_IRBRC="$IRBRC"
142− export IRBRC=${irbrc}
143− '';
144− buildCommand = ''
145− echo >&2 ""
146− echo >&2 "*** Ruby 'env' attributes are intended for interactive nix-shell sessions, not for building! ***"
147− echo >&2 ""
148− exit 1
149− '';
150− };
151− };
152− };
38+ # The basicEnv should be put into passthru so that e.g. nix-shell can use it.
15339in
154− bundlerEnv
40+ if pname == null then
41+ basicEnv // { inherit name basicEnv; }
42+ else
43+ (buildEnv {
44+ inherit ignoreCollisions;
45+
46+ name = basicEnv.name;
47+
48+ paths = envPaths;
49+ pathsToLink = [ "/lib" ];
50+
51+ postBuild = genStubsScript {
52+ inherit lib ruby bundler groups;
53+ confFiles = basicEnv.confFiles;
54+ binPaths = [ basicEnv.gems."${pname}" ];
55+ } + lib.optionalString (postBuild != null) postBuild;
56+
57+ meta = { platforms = ruby.meta.platforms; } // meta;
58+ passthru = basicEnv.passthru // {
59+ inherit basicEnv;
60+ inherit (basicEnv) env;
61+ };
62+ })