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 # Have `configure' avoid `/usr/bin/nroff' in non-chroot builds.
133 NROFF = if docSupport then "${groff}/bin/nroff" else null;
134
135 outputs = [ "out" ] ++ lib.optional docSupport "devdoc";
136
137 strictDeps = true;
138
139 nativeBuildInputs = [
140 autoreconfHook
141 bison
142 removeReferencesTo
143 ]
144 ++ (op docSupport groff)
145 ++ (ops (dtraceSupport && stdenv.hostPlatform.isLinux) [
146 systemtap
147 libsystemtap
148 ])
149 ++ ops yjitSupport [
150 rustPlatform.cargoSetupHook
151 rustc
152 ]
153 ++ op useBaseRuby baseRuby;
154 buildInputs = [
155 autoconf
156 ]
157 ++ (op fiddleSupport libffi)
158 ++ (ops cursesSupport [
159 ncurses
160 readline
161 ])
162 ++ (op zlibSupport zlib)
163 ++ (op opensslSupport openssl)
164 ++ (op gdbmSupport gdbm)
165 ++ (op yamlSupport libyaml)
166 # Looks like ruby fails to build on darwin without readline even if curses
167 # support is not enabled, so add readline to the build inputs if curses
168 # support is disabled (if it's enabled, we already have it) and we're
169 # running on darwin
170 ++ op (!cursesSupport && stdenv.hostPlatform.isDarwin) readline
171 ++ ops stdenv.hostPlatform.isDarwin [
172 libiconv
173 libunwind
174 ];
175 propagatedBuildInputs = op jemallocSupport jemalloc;
176
177 env = lib.optionalAttrs (stdenv.hostPlatform != stdenv.buildPlatform && yjitSupport) {
178 # The ruby build system will use a bare `rust` command by default for its rust.
179 # We can use the Nixpkgs rust wrapper to work around the fact that our Rust builds
180 # for cross-compilation output for the build target by default.
181 NIX_RUSTFLAGS = "--target ${stdenv.hostPlatform.rust.rustcTargetSpec}";
182 };
183
184 enableParallelBuilding = true;
185 # /build/ruby-2.7.7/lib/fileutils.rb:882:in `chmod':
186 # No such file or directory @ apply2files - ...-ruby-2.7.7-devdoc/share/ri/2.7.0/system/ARGF/inspect-i.ri (Errno::ENOENT)
187 # make: *** [uncommon.mk:373: do-install-all] Error 1
188 enableParallelInstalling = false;
189
190 patches = op useBaseRuby ./do-not-update-gems-baseruby-3.2.patch ++ [
191 # When using a baseruby, ruby always sets "libdir" to the build
192 # directory, which nix rejects due to a reference in to /build/ in
193 # the final product. Removing this reference doesn't seem to break
194 # anything and fixes cross compilation.
195 ./dont-refer-to-build-dir.patch
196 ];
197
198 cargoRoot = opString yjitSupport "yjit";
199
200 cargoDeps =
201 if yjitSupport then
202 rustPlatform.fetchCargoVendor {
203 inherit (finalAttrs) src cargoRoot;
204 hash =
205 assert cargoHash != null;
206 cargoHash;
207 }
208 else
209 null;
210
211 postUnpack = opString rubygemsSupport ''
212 rm -rf $sourceRoot/{lib,test}/rubygems*
213 cp -r ${rubygems}/lib/rubygems* $sourceRoot/lib
214 '';
215
216 # Ruby >= 2.1.0 tries to download config.{guess,sub}; copy it from autoconf instead.
217 postPatch = ''
218 sed -i configure.ac -e '/config.guess/d'
219 cp --remove-destination ${autoconf}/share/autoconf/build-aux/config.{guess,sub} tool/
220 '';
221
222 configureFlags = [
223 (lib.enableFeature (!stdenv.hostPlatform.isStatic) "shared")
224 (lib.enableFeature true "pthread")
225 (lib.withFeatureAs true "soname" "ruby-${version}")
226 (lib.withFeatureAs useBaseRuby "baseruby" "${baseRuby}/bin/ruby")
227 (lib.enableFeature dtraceSupport "dtrace")
228 (lib.enableFeature jitSupport "jit-support")
229 (lib.enableFeature yjitSupport "yjit")
230 (lib.enableFeature docSupport "install-doc")
231 (lib.withFeature jemallocSupport "jemalloc")
232 (lib.withFeatureAs docSupport "ridir" "${placeholder "devdoc"}/share/ri")
233 # ruby enables -O3 for gcc, however our compiler hardening wrapper
234 # overrides that by enabling `-O2` which is the minimum optimization
235 # needed for `_FORTIFY_SOURCE`.
236 ]
237 ++ lib.optional stdenv.cc.isGNU "CFLAGS=-O3"
238 ++ [
239 ]
240 # on darwin, we have /usr/include/tk.h -- so the configure script detects
241 # that tk is installed
242 ++ lib.optional stdenv.hostPlatform.isDarwin "--with-out-ext=tk"
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 = {
346 description = "Object-oriented language for quick and easy programming";
347 homepage = "https://www.ruby-lang.org/";
348 license = lib.licenses.ruby;
349 platforms = lib.platforms.all;
350 mainProgram = "ruby";
351 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/.";
352 };
353
354 passthru = rec {
355 version = ver;
356 rubyEngine = "ruby";
357 libPath = "lib/${rubyEngine}/${ver.libDir}";
358 gemPath = "lib/${rubyEngine}/gems/${ver.libDir}";
359 devEnv = import ./dev.nix {
360 inherit buildEnv bundler bundix;
361 ruby = finalAttrs.finalPackage;
362 };
363
364 inherit rubygems;
365 inherit
366 (import ../../ruby-modules/with-packages {
367 inherit
368 lib
369 stdenv
370 makeBinaryWrapper
371 buildRubyGem
372 buildEnv
373 ;
374 gemConfig = defaultGemConfig;
375 ruby = finalAttrs.finalPackage;
376 })
377 withPackages
378 buildGems
379 gems
380 ;
381 }
382 // lib.optionalAttrs useBaseRuby {
383 inherit baseRuby;
384 };
385 })
386 ) args;
387 in
388 rubyDrv;
389
390in
391{
392 mkRubyVersion = rubyVersion;
393 mkRuby = generic;
394
395 ruby_3_3 = generic {
396 version = rubyVersion "3" "3" "10" "";
397 hash = "sha256-tVW6pGejBs/I5sbtJNDSeyfpob7R2R2VUJhZ6saw6Sg=";
398 cargoHash = "sha256-xE7Cv+NVmOHOlXa/Mg72CTSaZRb72lOja98JBvxPvSs=";
399 };
400
401 ruby_3_4 = generic {
402 version = rubyVersion "3" "4" "8" "";
403 hash = "sha256-U8TdrUH7thifH17g21elHVS9H4f4dVs9aGBBVqNbBFs=";
404 cargoHash = "sha256-5Tp8Kth0yO89/LIcU8K01z6DdZRr8MAA0DPKqDEjIt0=";
405 };
406
407 ruby_4_0 = generic {
408 version = rubyVersion "4" "0" "1" "";
409 hash = "sha256-OSS+LQXbMPTjX4Wb8Ci+hfS33QFxQUL9gj5K9d4vr50=";
410 cargoHash = "sha256-z7NwWc4TaR042hNx0xgRkh/BQEpEJtE53cfrN0qNiE0=";
411 };
412
413}