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