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