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