1{
2 stdenv,
3 buildPackages,
4 lib,
5 fetchurl,
6 fetchpatch,
7 fetchFromSavannah,
8 zlib,
9 gdbm,
10 ncurses,
11 readline,
12 groff,
13 libyaml,
14 libffi,
15 jemalloc,
16 autoreconfHook,
17 bison,
18 autoconf,
19 libiconv,
20 libunwind,
21 buildEnv,
22 bundler,
23 bundix,
24 cargo,
25 rustPlatform,
26 rustc,
27 makeBinaryWrapper,
28 buildRubyGem,
29 defaultGemConfig,
30 removeReferencesTo,
31 openssl,
32 linuxPackages,
33 libsystemtap,
34 gitUpdater,
35}@args:
36
37let
38 op = lib.optional;
39 ops = lib.optionals;
40 opString = lib.optionalString;
41 config = import ./config.nix { inherit fetchFromSavannah; };
42 rubygems = import ./rubygems {
43 inherit
44 stdenv
45 lib
46 fetchurl
47 gitUpdater
48 ;
49 };
50
51 # Contains the ruby version heuristics
52 rubyVersion = import ./ruby-version.nix { inherit lib; };
53
54 generic =
55 {
56 version,
57 hash,
58 cargoHash ? null,
59 }:
60 let
61 ver = version;
62 atLeast31 = lib.versionAtLeast ver.majMin "3.1";
63 atLeast32 = lib.versionAtLeast ver.majMin "3.2";
64 # https://github.com/ruby/ruby/blob/v3_2_2/yjit.h#L21
65 yjitSupported =
66 atLeast32
67 && (
68 stdenv.hostPlatform.isx86_64 || (!stdenv.hostPlatform.isWindows && stdenv.hostPlatform.isAarch64)
69 );
70 rubyDrv = lib.makeOverridable (
71 {
72 stdenv,
73 buildPackages,
74 lib,
75 fetchurl,
76 fetchpatch,
77 fetchFromSavannah,
78 rubygemsSupport ? true,
79 zlib,
80 zlibSupport ? true,
81 openssl,
82 opensslSupport ? true,
83 gdbm,
84 gdbmSupport ? true,
85 ncurses,
86 readline,
87 cursesSupport ? true,
88 groff,
89 docSupport ? true,
90 libyaml,
91 yamlSupport ? true,
92 libffi,
93 fiddleSupport ? true,
94 jemalloc,
95 jemallocSupport ? false,
96 linuxPackages,
97 systemtap ? linuxPackages.systemtap,
98 libsystemtap,
99 dtraceSupport ? false,
100 # By default, ruby has 3 observed references to stdenv.cc:
101 #
102 # - If you run:
103 # ruby -e "puts RbConfig::CONFIG['configure_args']"
104 # - In:
105 # $out/${passthru.libPath}/${stdenv.hostPlatform.system}/rbconfig.rb
106 # Or (usually):
107 # $(nix-build -A ruby)/lib/ruby/2.6.0/x86_64-linux/rbconfig.rb
108 # - In $out/lib/libruby.so and/or $out/lib/libruby.dylib
109 removeReferencesTo,
110 jitSupport ? yjitSupport,
111 cargo,
112 rustPlatform,
113 rustc,
114 yjitSupport ? yjitSupported,
115 autoreconfHook,
116 bison,
117 autoconf,
118 buildEnv,
119 bundler,
120 bundix,
121 libiconv,
122 libunwind,
123 makeBinaryWrapper,
124 buildRubyGem,
125 defaultGemConfig,
126 baseRuby ? buildPackages.ruby.override {
127 docSupport = false;
128 rubygemsSupport = false;
129 },
130 useBaseRuby ? stdenv.hostPlatform != stdenv.buildPlatform,
131 gitUpdater,
132 }:
133 stdenv.mkDerivation (finalAttrs: {
134 pname = "ruby";
135 inherit version;
136
137 src = fetchurl {
138 url = "https://cache.ruby-lang.org/pub/ruby/${ver.majMin}/ruby-${ver}.tar.gz";
139 inherit hash;
140 };
141
142 # Have `configure' avoid `/usr/bin/nroff' in non-chroot builds.
143 NROFF = if docSupport then "${groff}/bin/nroff" else null;
144
145 outputs = [ "out" ] ++ lib.optional docSupport "devdoc";
146
147 strictDeps = true;
148
149 nativeBuildInputs =
150 [
151 autoreconfHook
152 bison
153 removeReferencesTo
154 ]
155 ++ (op docSupport groff)
156 ++ (ops (dtraceSupport && stdenv.hostPlatform.isLinux) [
157 systemtap
158 libsystemtap
159 ])
160 ++ ops yjitSupport [
161 rustPlatform.cargoSetupHook
162 cargo
163 rustc
164 ]
165 ++ op useBaseRuby baseRuby;
166 buildInputs =
167 [ autoconf ]
168 ++ (op fiddleSupport libffi)
169 ++ (ops cursesSupport [
170 ncurses
171 readline
172 ])
173 ++ (op zlibSupport zlib)
174 ++ (op opensslSupport openssl)
175 ++ (op gdbmSupport gdbm)
176 ++ (op yamlSupport libyaml)
177 # Looks like ruby fails to build on darwin without readline even if curses
178 # support is not enabled, so add readline to the build inputs if curses
179 # support is disabled (if it's enabled, we already have it) and we're
180 # running on darwin
181 ++ op (!cursesSupport && stdenv.hostPlatform.isDarwin) readline
182 ++ ops stdenv.hostPlatform.isDarwin [
183 libiconv
184 libunwind
185 ];
186 propagatedBuildInputs = op jemallocSupport jemalloc;
187
188 enableParallelBuilding = true;
189 # /build/ruby-2.7.7/lib/fileutils.rb:882:in `chmod':
190 # No such file or directory @ apply2files - ...-ruby-2.7.7-devdoc/share/ri/2.7.0/system/ARGF/inspect-i.ri (Errno::ENOENT)
191 # make: *** [uncommon.mk:373: do-install-all] Error 1
192 enableParallelInstalling = false;
193
194 patches =
195 op (lib.versionOlder ver.majMin "3.1") ./do-not-regenerate-revision.h.patch
196 ++ op useBaseRuby (
197 if atLeast32 then ./do-not-update-gems-baseruby-3.2.patch else ./do-not-update-gems-baseruby.patch
198 )
199 ++ ops (ver.majMin == "3.0") [
200 # Ruby 3.0 adds `-fdeclspec` to $CC instead of $CFLAGS. Fixed in later versions.
201 (fetchpatch {
202 url = "https://github.com/ruby/ruby/commit/0acc05caf7518cd0d63ab02bfa036455add02346.patch";
203 hash = "sha256-43hI9L6bXfeujgmgKFVmiWhg7OXvshPCCtQ4TxqK1zk=";
204 })
205 ]
206 ++ ops atLeast31 [
207 # When using a baseruby, ruby always sets "libdir" to the build
208 # directory, which nix rejects due to a reference in to /build/ in
209 # the final product. Removing this reference doesn't seem to break
210 # anything and fixes cross compliation.
211 ./dont-refer-to-build-dir.patch
212 ];
213
214 cargoRoot = opString yjitSupport "yjit";
215
216 cargoDeps =
217 if yjitSupport then
218 rustPlatform.fetchCargoVendor {
219 inherit (finalAttrs) src;
220 sourceRoot = "${finalAttrs.pname}-${version}/${finalAttrs.cargoRoot}";
221 hash =
222 assert cargoHash != null;
223 cargoHash;
224 }
225 else
226 null;
227
228 postUnpack = opString rubygemsSupport ''
229 rm -rf $sourceRoot/{lib,test}/rubygems*
230 cp -r ${rubygems}/lib/rubygems* $sourceRoot/lib
231 '';
232
233 postPatch = ''
234 sed -i configure.ac -e '/config.guess/d'
235 cp --remove-destination ${config}/config.guess tool/
236 cp --remove-destination ${config}/config.sub tool/
237 '';
238
239 configureFlags =
240 [
241 (lib.enableFeature (!stdenv.hostPlatform.isStatic) "shared")
242 (lib.enableFeature true "pthread")
243 (lib.withFeatureAs true "soname" "ruby-${version}")
244 (lib.withFeatureAs useBaseRuby "baseruby" "${baseRuby}/bin/ruby")
245 (lib.enableFeature dtraceSupport "dtrace")
246 (lib.enableFeature jitSupport "jit-support")
247 (lib.enableFeature yjitSupport "yjit")
248 (lib.enableFeature docSupport "install-doc")
249 (lib.withFeature jemallocSupport "jemalloc")
250 (lib.withFeatureAs docSupport "ridir" "${placeholder "devdoc"}/share/ri")
251 # ruby enables -O3 for gcc, however our compiler hardening wrapper
252 # overrides that by enabling `-O2` which is the minimum optimization
253 # needed for `_FORTIFY_SOURCE`.
254 ]
255 ++ lib.optional stdenv.cc.isGNU "CFLAGS=-O3"
256 ++ [
257 ]
258 ++ ops stdenv.hostPlatform.isDarwin [
259 # on darwin, we have /usr/include/tk.h -- so the configure script detects
260 # that tk is installed
261 "--with-out-ext=tk"
262 # on yosemite, "generating encdb.h" will hang for a very long time without this flag
263 "--with-setjmp-type=setjmp"
264 ]
265 ++ ops stdenv.hostPlatform.isFreeBSD [
266 "rb_cv_gnu_qsort_r=no"
267 "rb_cv_bsd_qsort_r=yes"
268 ];
269
270 preConfigure = opString docSupport ''
271 # rdoc creates XDG_DATA_DIR (defaulting to $HOME/.local/share) even if
272 # it's not going to be used.
273 export HOME=$TMPDIR
274 '';
275
276 # fails with "16993 tests, 2229489 assertions, 105 failures, 14 errors, 89 skips"
277 # mostly TZ- and patch-related tests
278 # TZ- failures are caused by nix sandboxing, I didn't investigate others
279 doCheck = false;
280
281 preInstall = ''
282 # Ruby installs gems here itself now.
283 mkdir -pv "$out/${finalAttrs.passthru.gemPath}"
284 export GEM_HOME="$out/${finalAttrs.passthru.gemPath}"
285 '';
286
287 installFlags = lib.optional docSupport "install-doc";
288 # Bundler tries to create this directory
289 postInstall =
290 ''
291 rbConfig=$(find $out/lib/ruby -name rbconfig.rb)
292 # Remove references to the build environment from the closure
293 sed -i '/^ CONFIG\["\(BASERUBY\|SHELL\|GREP\|EGREP\|MKDIR_P\|MAKEDIRS\|INSTALL\)"\]/d' $rbConfig
294 # Remove unnecessary groff reference from runtime closure, since it's big
295 sed -i '/NROFF/d' $rbConfig
296 ${lib.optionalString (!jitSupport) ''
297 # Get rid of the CC runtime dependency
298 remove-references-to \
299 -t ${stdenv.cc} \
300 $out/lib/libruby*
301 remove-references-to \
302 -t ${stdenv.cc} \
303 $rbConfig
304 sed -i '/CC_VERSION_MESSAGE/d' $rbConfig
305 ''}
306
307 # Allow to override compiler. This is important for cross compiling as
308 # we need to set a compiler that is different from the build one.
309 sed -i "$rbConfig" \
310 -e 's/CONFIG\["CC"\] = "\(.*\)"/CONFIG["CC"] = if ENV["CC"].nil? || ENV["CC"].empty? then "\1" else ENV["CC"] end/' \
311 -e 's/CONFIG\["CXX"\] = "\(.*\)"/CONFIG["CXX"] = if ENV["CXX"].nil? || ENV["CXX"].empty? then "\1" else ENV["CXX"] end/'
312
313 # Remove unnecessary external intermediate files created by gems
314 extMakefiles=$(find $out/${finalAttrs.passthru.gemPath} -name Makefile)
315 for makefile in $extMakefiles; do
316 make -C "$(dirname "$makefile")" distclean
317 done
318 find "$out/${finalAttrs.passthru.gemPath}" \( -name gem_make.out -o -name mkmf.log -o -name exts.mk \) -delete
319 # Bundler tries to create this directory
320 mkdir -p $out/nix-support
321 cat > $out/nix-support/setup-hook <<EOF
322 addGemPath() {
323 addToSearchPath GEM_PATH \$1/${finalAttrs.passthru.gemPath}
324 }
325 addRubyLibPath() {
326 addToSearchPath RUBYLIB \$1/lib/ruby/site_ruby
327 addToSearchPath RUBYLIB \$1/lib/ruby/site_ruby/${ver.libDir}
328 addToSearchPath RUBYLIB \$1/lib/ruby/site_ruby/${ver.libDir}/${stdenv.hostPlatform.system}
329 }
330
331 addEnvHooks "$hostOffset" addGemPath
332 addEnvHooks "$hostOffset" addRubyLibPath
333 EOF
334 ''
335 + opString docSupport ''
336 # Prevent the docs from being included in the closure
337 sed -i "s|\$(DESTDIR)$devdoc|\$(datarootdir)/\$(RI_BASE_NAME)|" $rbConfig
338 sed -i "s|'--with-ridir=$devdoc/share/ri'||" $rbConfig
339
340 # Add rbconfig shim so ri can find docs
341 mkdir -p $devdoc/lib/ruby/site_ruby
342 cp ${./rbconfig.rb} $devdoc/lib/ruby/site_ruby/rbconfig.rb
343 ''
344 + opString useBaseRuby ''
345 # Prevent the baseruby from being included in the closure.
346 remove-references-to \
347 -t ${baseRuby} \
348 $rbConfig $out/lib/libruby*
349 '';
350
351 installCheckPhase = ''
352 overriden_cc=$(CC=foo $out/bin/ruby -rrbconfig -e 'puts RbConfig::CONFIG["CC"]')
353 if [[ "$overriden_cc" != "foo" ]]; then
354 echo "CC cannot be overwritten: $overriden_cc != foo" >&2
355 false
356 fi
357
358 fallback_cc=$(unset CC; $out/bin/ruby -rrbconfig -e 'puts RbConfig::CONFIG["CC"]')
359 if [[ "$fallback_cc" != "$CC" ]]; then
360 echo "CC='$fallback_cc' should be '$CC' by default" >&2
361 false
362 fi
363 '';
364 doInstallCheck = true;
365
366 disallowedRequisites = op (!jitSupport) stdenv.cc ++ op useBaseRuby baseRuby;
367
368 meta = with lib; {
369 description = "Object-oriented language for quick and easy programming";
370 homepage = "https://www.ruby-lang.org/";
371 license = licenses.ruby;
372 maintainers = with maintainers; [ manveru ];
373 platforms = platforms.all;
374 mainProgram = "ruby";
375 knownVulnerabilities = op (lib.versionOlder ver.majMin "3.0") "This Ruby release has reached its end of life. See https://www.ruby-lang.org/en/downloads/branches/.";
376 };
377
378 passthru =
379 rec {
380 version = ver;
381 rubyEngine = "ruby";
382 libPath = "lib/${rubyEngine}/${ver.libDir}";
383 gemPath = "lib/${rubyEngine}/gems/${ver.libDir}";
384 devEnv = import ./dev.nix {
385 inherit buildEnv bundler bundix;
386 ruby = finalAttrs.finalPackage;
387 };
388
389 inherit rubygems;
390 inherit
391 (import ../../ruby-modules/with-packages {
392 inherit
393 lib
394 stdenv
395 makeBinaryWrapper
396 buildRubyGem
397 buildEnv
398 ;
399 gemConfig = defaultGemConfig;
400 ruby = finalAttrs.finalPackage;
401 })
402 withPackages
403 buildGems
404 gems
405 ;
406 }
407 // lib.optionalAttrs useBaseRuby {
408 inherit baseRuby;
409 };
410 })
411 ) args;
412 in
413 rubyDrv;
414
415in
416{
417 mkRubyVersion = rubyVersion;
418 mkRuby = generic;
419
420 ruby_3_1 = generic {
421 version = rubyVersion "3" "1" "7" "";
422 hash = "sha256-BVas1p8UHdrOA/pd2NdufqDY9SMu3wEkKVebzaqzDns=";
423 };
424
425 ruby_3_2 = generic {
426 version = rubyVersion "3" "2" "8" "";
427 hash = "sha256-d6zdjPu+H45XO15lNuA8UQPfmJ3AX6aMcPARgzw1YHU=";
428 cargoHash = "sha256-CMVx5/+ugDNEuLAvyPN0nGHwQw6RXyfRsMO9I+kyZpk=";
429 };
430
431 ruby_3_3 = generic {
432 version = rubyVersion "3" "3" "8" "";
433 hash = "sha256-WuKKh6WaPkrWa8KTHSMturlT0KqPa687xPj4CXfInKs=";
434 cargoHash = "sha256-xE7Cv+NVmOHOlXa/Mg72CTSaZRb72lOja98JBvxPvSs=";
435 };
436
437 ruby_3_4 = generic {
438 version = rubyVersion "3" "4" "3" "";
439 hash = "sha256-VaTNHcvlyifPZeiak1pILCuyKEgyk5JmVRwOxotDf0Y=";
440 cargoHash = "sha256-5Tp8Kth0yO89/LIcU8K01z6DdZRr8MAA0DPKqDEjIt0=";
441 };
442}