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