1# This builds gems in a way that is compatible with bundler.
2#
3# Bundler installs gems from git sources _very_ differently from how RubyGems
4# installes gem packages, though they both install gem packages similarly.
5#
6# We monkey-patch Bundler to remove any impurities and then drive its internals
7# to install git gems.
8#
9# For the sake of simplicity, gem packages are installed with the standard `gem`
10# program.
11#
12# Note that bundler does not support multiple prefixes; it assumes that all
13# gems are installed in a common prefix, and has no support for specifying
14# otherwise. Therefore, if you want to be able to use the resulting derivations
15# with bundler, you need to create a symlink forrest first, which is what
16# `bundlerEnv` does for you.
17#
18# Normal gem packages can be used outside of bundler; a binstub is created in
19# $out/bin.
20
21{ lib, fetchurl, fetchgit, makeWrapper, gitMinimal, darwin
22, ruby, bundler
23} @ defs:
24
25lib.makeOverridable (
26
27{ name ? null
28, gemName
29, version ? null
30, type ? "gem"
31, document ? [] # e.g. [ "ri" "rdoc" ]
32, platform ? "ruby"
33, ruby ? defs.ruby
34, stdenv ? ruby.stdenv
35, namePrefix ? (let
36 rubyName = builtins.parseDrvName ruby.name;
37 in "${rubyName.name}${rubyName.version}-")
38, buildInputs ? []
39, meta ? {}
40, patches ? []
41, gemPath ? []
42, dontStrip ? false
43# Assume we don't have to build unless strictly necessary (e.g. the source is a
44# git checkout).
45# If you need to apply patches, make sure to set `dontBuild = false`;
46, dontBuild ? true
47, dontInstallManpages ? false
48, propagatedBuildInputs ? []
49, propagatedUserEnvPkgs ? []
50, buildFlags ? []
51, passthru ? {}
52# bundler expects gems to be stored in the cache directory for certain actions
53# such as `bundler install --redownload`.
54# At the cost of increasing the store size, you can keep the gems to have closer
55# alignment with what Bundler expects.
56, keepGemCache ? false
57, ...} @ attrs:
58
59let
60 src = attrs.src or (
61 if type == "gem" then
62 fetchurl {
63 urls = map (
64 remote: "${remote}/gems/${gemName}-${version}.gem"
65 ) (attrs.source.remotes or [ "https://rubygems.org" ]);
66 inherit (attrs.source) sha256;
67 }
68 else if type == "git" then
69 fetchgit {
70 inherit (attrs.source) url rev sha256 fetchSubmodules;
71 }
72 else if type == "url" then
73 fetchurl attrs.source
74 else
75 throw "buildRubyGem: don't know how to build a gem of type \"${type}\""
76 );
77 documentFlag =
78 if document == []
79 then "-N"
80 else "--document ${lib.concatStringsSep "," document}";
81
82in
83
84stdenv.mkDerivation ((builtins.removeAttrs attrs ["source"]) // {
85 inherit ruby;
86 inherit dontBuild;
87 inherit dontStrip;
88 inherit type;
89
90 buildInputs = [
91 ruby makeWrapper
92 ] ++ lib.optionals (type == "git") [ gitMinimal ]
93 ++ lib.optionals (type != "gem") [ bundler ]
94 ++ lib.optional stdenv.isDarwin darwin.libobjc
95 ++ buildInputs;
96
97 #name = builtins.trace (attrs.name or "no attr.name" ) "${namePrefix}${gemName}-${version}";
98 name = attrs.name or "${namePrefix}${gemName}-${version}";
99
100 inherit src;
101
102
103 unpackPhase = attrs.unpackPhase or ''
104 runHook preUnpack
105
106 if [[ -f $src && $src == *.gem ]]; then
107 if [[ -z "''${dontBuild-}" ]]; then
108 # we won't know the name of the directory that RubyGems creates,
109 # so we'll just use a glob to find it and move it over.
110 gempkg="$src"
111 sourceRoot=source
112 gem unpack $gempkg --target=container
113 cp -r container/* $sourceRoot
114 rm -r container
115
116 # copy out the original gemspec, for convenience during patching /
117 # overrides.
118 gem specification $gempkg --ruby > original.gemspec
119 gemspec=$(readlink -f .)/original.gemspec
120 else
121 gempkg="$src"
122 fi
123 else
124 # Fall back to the original thing for everything else.
125 dontBuild=""
126 preUnpack="" postUnpack="" unpackPhase
127 fi
128
129 runHook postUnpack
130 '';
131
132 # As of ruby 3.0, ruby headers require -fdeclspec when building with clang
133 # Introduced in https://github.com/ruby/ruby/commit/0958e19ffb047781fe1506760c7cbd8d7fe74e57
134 NIX_CFLAGS_COMPILE = lib.optionals (stdenv.cc.isClang && lib.versionAtLeast ruby.version.major "3") [
135 "-fdeclspec"
136 ];
137
138 buildPhase = attrs.buildPhase or ''
139 runHook preBuild
140
141 if [[ "$type" == "gem" ]]; then
142 if [[ -z "$gemspec" ]]; then
143 gemspec="$(find . -name '*.gemspec')"
144 echo "found the following gemspecs:"
145 echo "$gemspec"
146 gemspec="$(echo "$gemspec" | head -n1)"
147 fi
148
149 exec 3>&1
150 output="$(gem build $gemspec | tee >(cat - >&3))"
151 exec 3>&-
152
153 gempkg=$(echo "$output" | grep -oP 'File: \K(.*)')
154
155 echo "gem package built: $gempkg"
156 elif [[ "$type" == "git" ]]; then
157 git init
158 # remove variations to improve the likelihood of a bit-reproducible output
159 rm -rf .git/logs/ .git/hooks/ .git/index .git/FETCH_HEAD .git/ORIG_HEAD .git/refs/remotes/origin/HEAD .git/config
160 # support `git ls-files`
161 git add .
162 fi
163
164 runHook postBuild
165 '';
166
167 # Note:
168 # We really do need to keep the $out/${ruby.gemPath}/cache.
169 # This is very important in order for many parts of RubyGems/Bundler to not blow up.
170 # See https://github.com/bundler/bundler/issues/3327
171 installPhase = attrs.installPhase or ''
172 runHook preInstall
173
174 export GEM_HOME=$out/${ruby.gemPath}
175 mkdir -p $GEM_HOME
176
177 echo "buildFlags: $buildFlags"
178
179 ${lib.optionalString (type == "url") ''
180 ruby ${./nix-bundle-install.rb} \
181 "path" \
182 '${gemName}' \
183 '${version}' \
184 '${lib.escapeShellArgs buildFlags}'
185 ''}
186 ${lib.optionalString (type == "git") ''
187 ruby ${./nix-bundle-install.rb} \
188 "git" \
189 '${gemName}' \
190 '${version}' \
191 '${lib.escapeShellArgs buildFlags}' \
192 '${attrs.source.url}' \
193 '.' \
194 '${attrs.source.rev}'
195 ''}
196
197 ${lib.optionalString (type == "gem") ''
198 if [[ -z "$gempkg" ]]; then
199 echo "failure: \$gempkg path unspecified" 1>&2
200 exit 1
201 elif [[ ! -f "$gempkg" ]]; then
202 echo "failure: \$gempkg path invalid" 1>&2
203 exit 1
204 fi
205
206 gem install \
207 --local \
208 --force \
209 --http-proxy 'http://nodtd.invalid' \
210 --ignore-dependencies \
211 --install-dir "$GEM_HOME" \
212 --build-root '/' \
213 --backtrace \
214 --no-env-shebang \
215 ${documentFlag} \
216 $gempkg $gemFlags -- $buildFlags
217
218 # looks like useless files which break build repeatability and consume space
219 pushd $out/${ruby.gemPath}
220 find doc/ -iname created.rid -delete -print
221 find gems/*/ext/ extensions/ \( -iname Makefile -o -iname mkmf.log -o -iname gem_make.out \) -delete -print
222 ${if keepGemCache then "" else "rm -fvr cache"}
223 popd
224
225 # write out metadata and binstubs
226 spec=$(echo $out/${ruby.gemPath}/specifications/*.gemspec)
227 ruby ${./gem-post-build.rb} "$spec"
228 ''}
229
230 ${lib.optionalString (!dontInstallManpages) ''
231 for section in {1..9}; do
232 mandir="$out/share/man/man$section"
233 find $out/lib \( -wholename "*/man/*.$section" -o -wholename "*/man/man$section/*.$section" \) \
234 -execdir mkdir -p $mandir \; -execdir cp '{}' $mandir \;
235 done
236 ''}
237
238 runHook postInstall
239 '';
240
241 propagatedBuildInputs = gemPath ++ propagatedBuildInputs;
242 propagatedUserEnvPkgs = gemPath ++ propagatedUserEnvPkgs;
243
244 passthru = passthru // { isRubyGem = true; };
245 meta = {
246 # default to Ruby's platforms
247 platforms = ruby.meta.platforms;
248 } // meta;
249})
250
251)