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 autoreconfHook
151 bison
152 removeReferencesTo
153 ]
154 ++ (op docSupport groff)
155 ++ (ops (dtraceSupport && stdenv.hostPlatform.isLinux) [
156 systemtap
157 libsystemtap
158 ])
159 ++ ops yjitSupport [
160 rustPlatform.cargoSetupHook
161 cargo
162 rustc
163 ]
164 ++ op useBaseRuby baseRuby;
165 buildInputs = [
166 autoconf
167 ]
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 compilation.
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 cargoRoot;
220 hash =
221 assert cargoHash != null;
222 cargoHash;
223 }
224 else
225 null;
226
227 postUnpack = opString rubygemsSupport ''
228 rm -rf $sourceRoot/{lib,test}/rubygems*
229 cp -r ${rubygems}/lib/rubygems* $sourceRoot/lib
230 '';
231
232 postPatch = ''
233 sed -i configure.ac -e '/config.guess/d'
234 cp --remove-destination ${config}/config.guess tool/
235 cp --remove-destination ${config}/config.sub tool/
236 '';
237
238 configureFlags = [
239 (lib.enableFeature (!stdenv.hostPlatform.isStatic) "shared")
240 (lib.enableFeature true "pthread")
241 (lib.withFeatureAs true "soname" "ruby-${version}")
242 (lib.withFeatureAs useBaseRuby "baseruby" "${baseRuby}/bin/ruby")
243 (lib.enableFeature dtraceSupport "dtrace")
244 (lib.enableFeature jitSupport "jit-support")
245 (lib.enableFeature yjitSupport "yjit")
246 (lib.enableFeature docSupport "install-doc")
247 (lib.withFeature jemallocSupport "jemalloc")
248 (lib.withFeatureAs docSupport "ridir" "${placeholder "devdoc"}/share/ri")
249 # ruby enables -O3 for gcc, however our compiler hardening wrapper
250 # overrides that by enabling `-O2` which is the minimum optimization
251 # needed for `_FORTIFY_SOURCE`.
252 ]
253 ++ lib.optional stdenv.cc.isGNU "CFLAGS=-O3"
254 ++ [
255 ]
256 ++ ops stdenv.hostPlatform.isDarwin [
257 # on darwin, we have /usr/include/tk.h -- so the configure script detects
258 # that tk is installed
259 "--with-out-ext=tk"
260 # on yosemite, "generating encdb.h" will hang for a very long time without this flag
261 "--with-setjmp-type=setjmp"
262 ]
263 ++ ops stdenv.hostPlatform.isFreeBSD [
264 "rb_cv_gnu_qsort_r=no"
265 "rb_cv_bsd_qsort_r=yes"
266 ];
267
268 preConfigure = opString docSupport ''
269 # rdoc creates XDG_DATA_DIR (defaulting to $HOME/.local/share) even if
270 # it's not going to be used.
271 export HOME=$TMPDIR
272 '';
273
274 # fails with "16993 tests, 2229489 assertions, 105 failures, 14 errors, 89 skips"
275 # mostly TZ- and patch-related tests
276 # TZ- failures are caused by nix sandboxing, I didn't investigate others
277 doCheck = false;
278
279 preInstall = ''
280 # Ruby installs gems here itself now.
281 mkdir -pv "$out/${finalAttrs.passthru.gemPath}"
282 export GEM_HOME="$out/${finalAttrs.passthru.gemPath}"
283 '';
284
285 installFlags = lib.optional docSupport "install-doc";
286 # Bundler tries to create this directory
287 postInstall = ''
288 rbConfig=$(find $out/lib/ruby -name rbconfig.rb)
289 # Remove references to the build environment from the closure
290 sed -i '/^ CONFIG\["\(BASERUBY\|SHELL\|GREP\|EGREP\|MKDIR_P\|MAKEDIRS\|INSTALL\)"\]/d' $rbConfig
291 # Remove unnecessary groff reference from runtime closure, since it's big
292 sed -i '/NROFF/d' $rbConfig
293 ${lib.optionalString (!jitSupport) ''
294 # Get rid of the CC runtime dependency
295 remove-references-to \
296 -t ${stdenv.cc} \
297 $out/lib/libruby*
298 remove-references-to \
299 -t ${stdenv.cc} \
300 $rbConfig
301 sed -i '/CC_VERSION_MESSAGE/d' $rbConfig
302 ''}
303
304 # Allow to override compiler. This is important for cross compiling as
305 # we need to set a compiler that is different from the build one.
306 sed -i "$rbConfig" \
307 -e 's/CONFIG\["CC"\] = "\(.*\)"/CONFIG["CC"] = if ENV["CC"].nil? || ENV["CC"].empty? then "\1" else ENV["CC"] end/' \
308 -e 's/CONFIG\["CXX"\] = "\(.*\)"/CONFIG["CXX"] = if ENV["CXX"].nil? || ENV["CXX"].empty? then "\1" else ENV["CXX"] end/'
309
310 # Remove unnecessary external intermediate files created by gems
311 extMakefiles=$(find $out/${finalAttrs.passthru.gemPath} -name Makefile)
312 for makefile in $extMakefiles; do
313 make -C "$(dirname "$makefile")" distclean
314 done
315 find "$out/${finalAttrs.passthru.gemPath}" \( -name gem_make.out -o -name mkmf.log -o -name exts.mk \) -delete
316 # Bundler tries to create this directory
317 mkdir -p $out/nix-support
318 cat > $out/nix-support/setup-hook <<EOF
319 addGemPath() {
320 addToSearchPath GEM_PATH \$1/${finalAttrs.passthru.gemPath}
321 }
322 addRubyLibPath() {
323 addToSearchPath RUBYLIB \$1/lib/ruby/site_ruby
324 addToSearchPath RUBYLIB \$1/lib/ruby/site_ruby/${ver.libDir}
325 addToSearchPath RUBYLIB \$1/lib/ruby/site_ruby/${ver.libDir}/${stdenv.hostPlatform.system}
326 }
327
328 addEnvHooks "$hostOffset" addGemPath
329 addEnvHooks "$hostOffset" addRubyLibPath
330 EOF
331 ''
332 + opString docSupport ''
333 # Prevent the docs from being included in the closure
334 sed -i "s|\$(DESTDIR)$devdoc|\$(datarootdir)/\$(RI_BASE_NAME)|" $rbConfig
335 sed -i "s|'--with-ridir=$devdoc/share/ri'||" $rbConfig
336
337 # Add rbconfig shim so ri can find docs
338 mkdir -p $devdoc/lib/ruby/site_ruby
339 cp ${./rbconfig.rb} $devdoc/lib/ruby/site_ruby/rbconfig.rb
340 ''
341 + opString useBaseRuby ''
342 # Prevent the baseruby from being included in the closure.
343 remove-references-to \
344 -t ${baseRuby} \
345 $rbConfig $out/lib/libruby*
346 '';
347
348 installCheckPhase = ''
349 overriden_cc=$(CC=foo $out/bin/ruby -rrbconfig -e 'puts RbConfig::CONFIG["CC"]')
350 if [[ "$overriden_cc" != "foo" ]]; then
351 echo "CC cannot be overwritten: $overriden_cc != foo" >&2
352 false
353 fi
354
355 fallback_cc=$(unset CC; $out/bin/ruby -rrbconfig -e 'puts RbConfig::CONFIG["CC"]')
356 if [[ "$fallback_cc" != "$CC" ]]; then
357 echo "CC='$fallback_cc' should be '$CC' by default" >&2
358 false
359 fi
360 '';
361 doInstallCheck = true;
362
363 disallowedRequisites = op (!jitSupport) stdenv.cc ++ op useBaseRuby baseRuby;
364
365 meta = with lib; {
366 description = "Object-oriented language for quick and easy programming";
367 homepage = "https://www.ruby-lang.org/";
368 license = licenses.ruby;
369 maintainers = with maintainers; [ manveru ];
370 platforms = platforms.all;
371 mainProgram = "ruby";
372 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/.";
373 };
374
375 passthru = rec {
376 version = ver;
377 rubyEngine = "ruby";
378 libPath = "lib/${rubyEngine}/${ver.libDir}";
379 gemPath = "lib/${rubyEngine}/gems/${ver.libDir}";
380 devEnv = import ./dev.nix {
381 inherit buildEnv bundler bundix;
382 ruby = finalAttrs.finalPackage;
383 };
384
385 inherit rubygems;
386 inherit
387 (import ../../ruby-modules/with-packages {
388 inherit
389 lib
390 stdenv
391 makeBinaryWrapper
392 buildRubyGem
393 buildEnv
394 ;
395 gemConfig = defaultGemConfig;
396 ruby = finalAttrs.finalPackage;
397 })
398 withPackages
399 buildGems
400 gems
401 ;
402 }
403 // lib.optionalAttrs useBaseRuby {
404 inherit baseRuby;
405 };
406 })
407 ) args;
408 in
409 rubyDrv;
410
411in
412{
413 mkRubyVersion = rubyVersion;
414 mkRuby = generic;
415
416 ruby_3_1 = generic {
417 version = rubyVersion "3" "1" "7" "";
418 hash = "sha256-BVas1p8UHdrOA/pd2NdufqDY9SMu3wEkKVebzaqzDns=";
419 };
420
421 ruby_3_2 = generic {
422 version = rubyVersion "3" "2" "8" "";
423 hash = "sha256-d6zdjPu+H45XO15lNuA8UQPfmJ3AX6aMcPARgzw1YHU=";
424 cargoHash = "sha256-CMVx5/+ugDNEuLAvyPN0nGHwQw6RXyfRsMO9I+kyZpk=";
425 };
426
427 ruby_3_3 = generic {
428 version = rubyVersion "3" "3" "8" "";
429 hash = "sha256-WuKKh6WaPkrWa8KTHSMturlT0KqPa687xPj4CXfInKs=";
430 cargoHash = "sha256-xE7Cv+NVmOHOlXa/Mg72CTSaZRb72lOja98JBvxPvSs=";
431 };
432
433 ruby_3_4 = generic {
434 version = rubyVersion "3" "4" "5" "";
435 hash = "sha256-HYjYontEL93kqgbcmehrC78LKIlj2EMxEt1frHmP1e4=";
436 cargoHash = "sha256-5Tp8Kth0yO89/LIcU8K01z6DdZRr8MAA0DPKqDEjIt0=";
437 };
438}