about summary refs log tree commit diff
path: root/pkgs/development/ruby-modules/gem-config/default.nix
blob: 45de779fcdaf9c40e4a64c01130082e735f2d833 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
# The standard set of gems in nixpkgs including potential fixes.
#
# The gemset is derived from two points of entry:
# - An attrset describing a gem, including version, source and dependencies
#   This is just meta data, most probably automatically generated by a tool
#   like Bundix (https://github.com/aflatter/bundix).
#   {
#     name = "bundler";
#     version = "1.6.5";
#     sha256 = "1s4x0f5by9xs2y24jk6krq5ky7ffkzmxgr4z1nhdykdmpsi2zd0l";
#     dependencies = [ "rake" ];
#   }
# - An optional derivation that may override how the gem is built. For popular
#   gems that don't behave correctly, fixes are already provided in the form of
#   derivations.
#
# This separates "what to build" (the exact gem versions) from "how to build"
# (to make gems behave if necessary).

{ lib, fetchurl, writeScript, ruby, libkrb5, libxml2, libxslt, python2, stdenv, which
, libiconv, postgresql, v8, clang, sqlite, zlib, imagemagick, lasem
, pkg-config , ncurses, xapian, gpgme, util-linux, tzdata, icu, libffi
, cmake, libssh2, openssl, openssl_1_1, libmysqlclient, git, perl, pcre, pcre2, gecode_3, curl
, libsodium, snappy, libossp_uuid, lxc, libpcap, xorg, gtk2, gtk3, buildRubyGem
, cairo, expat, re2, rake, gobject-introspection, gdk-pixbuf, zeromq, czmq, graphicsmagick, libcxx
, file, libvirt, glib, vips, taglib, libopus, linux-pam, libidn, protobuf, fribidi, harfbuzz
, bison, flex, pango, python3, patchelf, binutils, freetds, wrapGAppsHook, atk
, bundler, libsass, dart-sass, libexif, libselinux, libsepol, shared-mime-info, libthai, libdatrie
, CoreServices, DarwinTools, cctools, libtool, discount, exiv2, libepoxy, libxkbcommon, libmaxminddb, libyaml
, cargo, rustc, rustPlatform
, autoSignDarwinBinariesHook, fetchpatch
}@args:

let
  rainbow_rake = buildRubyGem {
    pname = "rake";
    gemName = "rake";
    source.sha256 = "01j8fc9bqjnrsxbppncai05h43315vmz9fwg28qdsgcjw9ck1d7n";
    type = "gem";
    version = "12.0.0";
  };
in

{
  ZenTest = attrs: {
    meta.mainProgram = "zentest";
  };

  atk = attrs: {
    dependencies = attrs.dependencies ++ [ "gobject-introspection" ];
    nativeBuildInputs = [ rake bundler pkg-config ]
      ++ lib.optionals stdenv.isDarwin [ DarwinTools ];
    propagatedBuildInputs = [ gobject-introspection wrapGAppsHook atk ];
  };

  bundler = attrs:
    let
      templates = "${attrs.ruby.gemPath}/gems/${attrs.gemName}-${attrs.version}/lib/bundler/templates/";
    in {
      # patching shebangs would fail on the templates/Executable file, so we
      # temporarily remove the executable flag.
      preFixup  = "chmod -x $out/${templates}/Executable";
      postFixup = ''
        chmod +x $out/${templates}/Executable

        # Allows to load another bundler version
        sed -i -e "s/activate_bin_path/bin_path/g" $out/bin/bundle
      '';
    };

  cairo = attrs: {
    nativeBuildInputs = [ pkg-config ]
      ++ lib.optionals stdenv.isDarwin [ DarwinTools ];
    buildInputs = [ gtk2 pcre2 xorg.libpthreadstubs xorg.libXdmcp];
  };

  cairo-gobject = attrs: {
    nativeBuildInputs = [ pkg-config ]
      ++ lib.optionals stdenv.isDarwin [ DarwinTools ];
    buildInputs = [ cairo expat pcre2 xorg.libpthreadstubs xorg.libXdmcp ];
  };

  charlock_holmes = attrs: {
    buildInputs = [ which icu zlib ];
  };

  cld3 = attrs: {
    nativeBuildInputs = [ pkg-config ];
    buildInputs = [ protobuf ];
  };

  cocoapods-acknowledgements = attrs: {
    dependencies = attrs.dependencies ++ [ "cocoapods" ];
  };

  cocoapods-deploy = attrs: {
    dependencies = [ "cocoapods" ];
  };

  cocoapods-disable-podfile-validations = attrs: {
    dependencies = [ "cocoapods" ];
  };

  cocoapods-generate = attrs: {
    dependencies = attrs.dependencies ++ [ "cocoapods" ];
  };

  cocoapods-git_url_rewriter = attrs: {
    dependencies = [ "cocoapods" ];
  };

  cocoapods-keys = attrs: {
    dependencies = attrs.dependencies ++ [ "cocoapods" ];
  };

  cocoapods-open = attrs: {
    dependencies = [ "cocoapods" ];
  };

  cocoapods-try-release-fix = attrs: {
    dependencies = [ "cocoapods" ];
  };

  curb = attrs: {
    buildInputs = [ curl ];
  };

  curses = attrs: {
    buildInputs = [ ncurses ];
  };

  dep-selector-libgecode = attrs: {
    USE_SYSTEM_GECODE = true;
    postInstall = ''
      installPath=$(cat $out/nix-support/gem-meta/install-path)
      sed -i $installPath/lib/dep-selector-libgecode.rb -e 's@VENDORED_GECODE_DIR =.*@VENDORED_GECODE_DIR = "${gecode_3}"@'
    '';
  };

  digest-sha3 = attrs: {
    hardeningDisable = [ "format" ];
  };

  rdiscount = attrs: {
    # Use discount from nixpkgs instead of vendored version
    dontBuild = false;
    buildInputs = [ discount ];
    patches = [
      # Adapted from Debian:
      # https://sources.debian.org/data/main/r/ruby-rdiscount/2.1.8-1/debian/patches/01_use-system-libmarkdown.patch
      ./rdiscount-use-nixpkgs-libmarkdown.patch
    ];
  };

  ethon = attrs: {
    dontBuild = false;
    postPatch = ''
      substituteInPlace lib/ethon/curls/settings.rb \
        --replace "libcurl" "${curl.out}/lib/libcurl${stdenv.hostPlatform.extensions.sharedLibrary}"
    '';
  };

  exiv2 = attrs: {
    buildFlags = [ "--with-exiv2-lib=${exiv2}/lib" "--with-exiv2-include=${exiv2.dev}/include" ];
  };

  fog-dnsimple = attrs:
    lib.optionalAttrs (lib.versionOlder attrs.version "1.0.1") {
      postInstall = ''
        cd $(cat $out/nix-support/gem-meta/install-path)
        rm {$out/bin,bin,../../bin}/{setup,console}
      '';
    };

  redis-rack = attrs: {
    dontBuild = false;
    preBuild = ''
      exec 3>&1
      output="$(gem build $gemspec | tee >(cat - >&3))"
      exec 3>&-
      sed -i 's!"rake".freeze!!' $gemspec
    '';
  };

  ffi-rzmq-core = attrs: {
    postInstall = ''
      installPath=$(cat $out/nix-support/gem-meta/install-path)
      sed -i $installPath/lib/ffi-rzmq-core/libzmq.rb -e 's@inside_gem =.*@inside_gem = "${zeromq}/lib"@'
    '';
  };

  mimemagic = attrs: {
    FREEDESKTOP_MIME_TYPES_PATH = "${shared-mime-info}/share/mime/packages/freedesktop.org.xml";
  };

  mini_magick = attrs: {
    postInstall = ''
      installPath=$(cat $out/nix-support/gem-meta/install-path)
      echo -e "\nENV['PATH'] += ':${graphicsmagick}/bin'\n" >> $installPath/lib/mini_magick/configuration.rb
    '';
  };

  do_sqlite3 = attrs: {
    buildInputs = [ sqlite ];
  };

  eventmachine = attrs: {
    dontBuild = false;
    buildInputs = [ openssl ];
    postPatch = ''
      substituteInPlace ext/em.cpp \
        --replace 'if (bind (' 'if (::bind ('
    '';
  };

  exif = attrs: {
    buildFlags = [ "--with-exif-dir=${libexif}" ];
    buildInputs = [ libexif ];
  };

  ffi = attrs: {
    nativeBuildInputs = [ pkg-config ];
    buildInputs = [ libffi ];
  };

  fiddle = attrs: {
    buildInputs = [ libffi ];
  };

  gdk_pixbuf2 = attrs: {
    nativeBuildInputs = [ pkg-config bundler rake ]
      ++ lib.optionals stdenv.isDarwin [ DarwinTools ];
    propagatedBuildInputs = [ gobject-introspection wrapGAppsHook gdk-pixbuf ];
  };

  gdk3 = attrs: {
    nativeBuildInputs = [ pkg-config bundler rake ]
      ++ lib.optionals stdenv.isDarwin [ DarwinTools ];
    propagatedBuildInputs = [ gobject-introspection wrapGAppsHook gdk-pixbuf cairo ];
  };

  gpgme = attrs: {
    buildInputs = [ gpgme ];
    nativeBuildInputs = [ pkg-config ];
    buildFlags = [ "--use-system-libraries" ];
  };

  gio2 = attrs: {
    nativeBuildInputs = [ pkg-config gobject-introspection ]
      ++ lib.optionals stdenv.isDarwin [ DarwinTools ];
    buildInputs = [ gtk2 pcre pcre2 ] ++ lib.optionals stdenv.isLinux [ util-linux libselinux libsepol ];
  };

  gitlab-markup = attrs: { meta.priority = 1; };

  gitlab-pg_query = attrs: lib.optionalAttrs (attrs.version == "1.3.1") {
    dontBuild = false;
    postPatch = ''
      sed -i "s;'https://codeload.github.com.*';'${fetchurl {
        url = "https://codeload.github.com/lfittl/libpg_query/tar.gz/10-1.0.3";
        sha256 = "0jfij8apzxsdabl70j42xgd5f3ka1gdcrk764nccp66164gpcchk";
      }}';" ext/pg_query/extconf.rb
    '';
  };

  parser = attrs: {
    meta.mainProgram = "ruby-parse";
  };

  pg_query = attrs: lib.optionalAttrs (attrs.version == "2.0.2") {
    dontBuild = false;
    postPatch = ''
      sed -i "s;'https://codeload.github.com.*';'${fetchurl {
        url = "https://codeload.github.com/lfittl/libpg_query/tar.gz/13-2.0.2";
        sha256 = "0ms2s6hmy8qyzv4g1hj4i2p5fws1v8lrj73b2knwbp2ipd45yj7y";
      }}';" ext/pg_query/extconf.rb
    '';
  } // lib.optionalAttrs (attrs.version == "1.3.0") {
    # Needed for gitlab
    dontBuild = false;
    postPatch = ''
      sed -i "s;'https://codeload.github.com.*';'${fetchurl {
        url = "https://codeload.github.com/lfittl/libpg_query/tar.gz/10-1.0.4";
        sha256 = "0f0kshhai0pnkqj0w4kgz3fssnvwidllc31n1fysxjjzdqlr1k48";
      }}';" ext/pg_query/extconf.rb
    '';
  };

  prettier = attrs: {
    meta.mainProgram = "rbprettier";
  };

  prometheus-client-mmap = attrs: {
    dontBuild = false;
    postPatch = let
      getconf = if stdenv.hostPlatform.isGnu then stdenv.cc.libc else getconf;
    in ''
      substituteInPlace lib/prometheus/client/page_size.rb --replace "getconf" "${lib.getBin getconf}/bin/getconf"
    '';
  } // lib.optionalAttrs (lib.versionAtLeast attrs.version "1.0") {
    cargoRoot = "ext/fast_mmaped_file_rs";
    cargoDeps = rustPlatform.fetchCargoTarball {
      src = stdenv.mkDerivation {
        inherit (buildRubyGem { inherit (attrs) gemName version source; })
          name
          src
          unpackPhase
          nativeBuildInputs
        ;
        dontBuilt = true;
        installPhase = ''
          cp -R ext/fast_mmaped_file_rs $out
        '';
      };
      hash = if lib.versionAtLeast attrs.version "1.1.1"
        then "sha256-RsN5XWX7Mj2ORccM0eczY+44WXsbXNTnJVcCMvnOATk="
        else "sha256-XuQZPbFWqPHlrJvllkvLl1FjKeoAUbi8oKDrS2rY1KM=";
    };
    nativeBuildInputs = [
      cargo
      rustc
      rustPlatform.cargoSetupHook
      rustPlatform.bindgenHook
    ];
    disallowedReferences = [
      rustc.unwrapped
    ];
    preBuild = ''
      cat ../.cargo/config > ext/fast_mmaped_file_rs/.cargo/config.toml
      sed -i "s|cargo-vendor-dir|$PWD/../cargo-vendor-dir|" ext/fast_mmaped_file_rs/.cargo/config.toml
    '';
    postInstall = ''
      find $out -type f -name .rustc_info.json -delete
    '';
  };

  glib2 = attrs: {
    nativeBuildInputs = [ pkg-config ]
      ++ lib.optionals stdenv.isDarwin [ DarwinTools ];
    buildInputs = [ gtk2 pcre2 ];
  };

  gtk2 = attrs: {
    nativeBuildInputs = [
      binutils pkg-config
    ] ++ lib.optionals stdenv.isLinux [
      util-linux libselinux libsepol
    ] ++ lib.optionals stdenv.isDarwin [ DarwinTools ];
    propagatedBuildInputs = [
      atk
      gdk-pixbuf
      fribidi
      gobject-introspection
      gtk2
      harfbuzz
      libdatrie
      libthai
      pcre pcre2
      xorg.libpthreadstubs
      xorg.libXdmcp
    ];
    dontStrip = stdenv.isDarwin;
  };

  gtk3 = attrs: {
    nativeBuildInputs = [
      binutils
      pkg-config
    ] ++ lib.optionals stdenv.isLinux [
      util-linux
      libselinux
      libsepol
    ] ++ lib.optionals stdenv.isDarwin [ DarwinTools ];
    propagatedBuildInputs = [
      atk
      gdk-pixbuf
      fribidi
      gobject-introspection
      gtk3
      cairo
      harfbuzz
      libdatrie
      libthai
      pcre
      pcre2
      xorg.libpthreadstubs
      xorg.libXdmcp
      xorg.libXtst
      libxkbcommon
      libepoxy
    ];
    dontStrip = stdenv.isDarwin;
  };

  gobject-introspection = attrs: {
    nativeBuildInputs = [ pkg-config pcre2 ]
      ++ lib.optionals stdenv.isDarwin [ DarwinTools ];
    propagatedBuildInputs = [ gobject-introspection wrapGAppsHook glib ];
  };

  gollum = attrs: {
    dontBuild = false;
    postPatch = ''
      substituteInPlace bin/gollum \
        --replace "/usr/bin/env -S ruby" "${ruby}/bin/ruby"
    '';
  };

  google-protobuf = attrs:
    lib.optionalAttrs (lib.versionAtLeast attrs.version "3.25.0") {
    # Fails on 3.25.0 with:
    #   convert.c:312:32: error: format string is not a string literal (potentially insecure) [-Werror,-Wformat-security]
    hardeningDisable = [ "format" ];
  };

  grpc = attrs: {
    nativeBuildInputs = [ pkg-config ]
      ++ lib.optional stdenv.isDarwin cctools
      ++ lib.optional (lib.versionAtLeast attrs.version "1.53.0" && stdenv.isDarwin && stdenv.isAarch64) autoSignDarwinBinariesHook;
    buildInputs = [ openssl ];
    hardeningDisable = [ "format" ];
    env.NIX_CFLAGS_COMPILE = toString [
      "-Wno-error=stringop-overflow"
      "-Wno-error=implicit-fallthrough"
      "-Wno-error=sizeof-pointer-memaccess"
      "-Wno-error=cast-function-type"
      "-Wno-error=class-memaccess"
      "-Wno-error=ignored-qualifiers"
      "-Wno-error=tautological-compare"
      "-Wno-error=stringop-truncation"
    ];
    dontBuild = false;
    postPatch = ''
      substituteInPlace Makefile \
        --replace '-Wno-invalid-source-encoding' ""
    '' + lib.optionalString (lib.versionOlder attrs.version "1.53.0" && stdenv.isDarwin) ''
      # For < v1.48.0
      substituteInPlace src/ruby/ext/grpc/extconf.rb \
        --replace "ENV['AR'] = 'libtool -o' if RUBY_PLATFORM =~ /darwin/" ""
      # For >= v1.48.0
      substituteInPlace src/ruby/ext/grpc/extconf.rb \
        --replace 'apple_toolchain = ' 'apple_toolchain = false && '
    '';
  };

  hitimes = attrs: {
    buildInputs = lib.optionals stdenv.isDarwin [ CoreServices ];
  };

  iconv = attrs: {
    dontBuild = false;
    buildFlags = lib.optional stdenv.isDarwin "--with-iconv-dir=${libiconv}";
    patches = [
      # Fix incompatible function pointer conversion errors with clang 16
      ./iconv-fix-incompatible-function-pointer-conversions.patch
    ];
  };

  idn-ruby = attrs: {
    buildInputs = [ libidn ];
  };

  # disable bundle install as it can't install anything in addition to what is
  # specified in pkgs/applications/misc/jekyll/Gemfile anyway. Also do chmod_R
  # to compensate for read-only files in site_template in nix store.
  jekyll = attrs: {
    postInstall = ''
      installPath=$(cat $out/nix-support/gem-meta/install-path)
      sed -i $installPath/lib/jekyll/commands/new.rb \
          -e 's@Exec.run("bundle", "install"@Exec.run("true"@' \
          -e 's@FileUtils.cp_r site_template + "/.", path@FileUtils.cp_r site_template + "/.", path; FileUtils.chmod_R "u+w", path@'
    '';
  };

  # note that you need version >= v3.16.14.8,
  # otherwise the gem will fail to link to the libv8 binary.
  # see: https://github.com/cowboyd/libv8/pull/161
  libv8 = attrs: {
    buildInputs = [ which v8 python2 ];
    buildFlags = [ "--with-system-v8=true" ];
    dontBuild = false;
    # The gem includes broken symlinks which are ignored during unpacking, but
    # then fail during build. Since the content is missing anyway, touching the
    # files is enough to unblock the build.
    preBuild = ''
      touch vendor/depot_tools/cbuildbot vendor/depot_tools/chrome_set_ver vendor/depot_tools/cros_sdk
    '';
    postPatch = ''
      substituteInPlace ext/libv8/extconf.rb \
        --replace "location = Libv8::Location::Vendor.new" \
                  "location = Libv8::Location::System.new"
    '';
    meta.broken = true; # At 2023-01-20, errors as:
                        #   "Failed to build gem native extension."
                        # Requires Python 2. Project is abandoned.
  };

  execjs = attrs: {
    propagatedBuildInputs = [ v8 ];
  };

  libxml-ruby = attrs: {
    buildFlags = [
      "--with-xml2-lib=${libxml2.out}/lib"
      "--with-xml2-include=${libxml2.dev}/include/libxml2"
    ] ++ lib.optionals stdenv.isDarwin [
      "--with-iconv-dir=${libiconv}"
      "--with-opt-include=${libiconv}/include"
    ];
  };

  mathematical = attrs: {
    nativeBuildInputs = [
      ruby
      cmake
      bison
      flex
      pkg-config
      python3
      patchelf
    ];

    buildInputs = [
      cairo
      fribidi
      gdk-pixbuf
      glib
      libxml2
      pango
    ];

    strictDeps = true;

    # The ruby build script takes care of this
    dontUseCmakeConfigure = true;

    postInstall = ''
      # Reduce output size by a lot, and remove some unnecessary references.
      # The ext directory should only be required at build time, so
      # can be deleted now.
      rm -r $out/${ruby.gemPath}/gems/mathematical-${attrs.version}/ext \
            $out/${ruby.gemPath}/extensions/*/*/mathematical-${attrs.version}/gem_make.out
    '';

    # For some reason 'mathematical.so' is missing cairo, glib, and
    # lasem in its RPATH, add them explicitly here
    postFixup = lib.optionalString stdenv.isLinux ''
      soPath="$out/${ruby.gemPath}/gems/mathematical-${attrs.version}/lib/mathematical/mathematical.so"
      rpath="$(patchelf --print-rpath "$soPath")"
      patchelf --set-rpath "${lib.makeLibraryPath [ lasem glib cairo ]}:$rpath" "$soPath"
      patchelf --replace-needed liblasem.so liblasem-0.4.so "$soPath"
    '';
  };

  magic = attrs: {
    buildInputs = [ file ];
    postInstall = ''
      installPath=$(cat $out/nix-support/gem-meta/install-path)
      sed -e 's@ENV\["MAGIC_LIB"\] ||@ENV\["MAGIC_LIB"\] || "${file}/lib/libmagic.so" ||@' -i $installPath/lib/magic/api.rb
    '';
  };

  maxmind_geoip2 = attrs: {
    buildFlags = [ "--with-maxminddb-lib=${libmaxminddb}/lib" "--with-maxminddb-include=${libmaxminddb}/include" ];
  };

  metasploit-framework = attrs: {
    preInstall = ''
      export HOME=$TMPDIR
    '';
  };

  mysql = attrs: {
    buildInputs = [ libmysqlclient zlib openssl ];
  };

  mysql2 = attrs: {
    buildInputs = [ libmysqlclient zlib openssl ];
  };

  ncursesw = attrs: {
    buildInputs = [ ncurses ];
    buildFlags = [
      "--with-cflags=-I${ncurses.dev}/include"
      "--with-ldflags=-L${ncurses.out}/lib"
    ];
  };

  nokogiri = attrs: {
    buildFlags = [
      "--use-system-libraries"
      "--with-zlib-lib=${zlib.out}/lib"
      "--with-zlib-include=${zlib.dev}/include"
      "--with-xml2-lib=${libxml2.out}/lib"
      "--with-xml2-include=${libxml2.dev}/include/libxml2"
      "--with-xslt-lib=${libxslt.out}/lib"
      "--with-xslt-include=${libxslt.dev}/include"
      "--with-exslt-lib=${libxslt.out}/lib"
      "--with-exslt-include=${libxslt.dev}/include"
    ] ++ lib.optionals stdenv.isDarwin [
      "--with-iconv-dir=${libiconv}"
      "--with-opt-include=${libiconv}/include"
    ];
  };

  openssl = attrs: {
    # https://github.com/ruby/openssl/issues/369
    buildInputs = [ (if (lib.versionAtLeast attrs.version "3.0.0") then openssl else openssl_1_1) ];
  };

  opus-ruby = attrs: {
    dontBuild = false;
    postPatch = ''
      substituteInPlace lib/opus-ruby.rb \
        --replace "ffi_lib 'opus'" \
                  "ffi_lib '${libopus}/lib/libopus${stdenv.hostPlatform.extensions.sharedLibrary}'"
    '';
  };

  ovirt-engine-sdk = attrs: {
    buildInputs = [ curl libxml2 ];
    dontBuild = false;
  };

  pango = attrs: {
    nativeBuildInputs = [
      pkg-config
      fribidi
      harfbuzz
      pcre pcre2
      xorg.libpthreadstubs
      xorg.libXdmcp
    ] ++ lib.optionals stdenv.isDarwin [ DarwinTools ];
    buildInputs = [ libdatrie libthai ]
      ++ lib.optionals stdenv.isLinux [ libselinux libsepol util-linux ];
    propagatedBuildInputs = [ gobject-introspection wrapGAppsHook gtk2 ];
  };

  patron = attrs: {
    buildInputs = [ curl ];
  };

  pcaprub = attrs: {
    buildInputs = [ libpcap ];
  };

  pg = attrs: {
    # Force pkg-config lookup for libpq.
    # See https://github.com/ged/ruby-pg/blob/6629dec6656f7ca27619e4675b45225d9e422112/ext/extconf.rb#L34-L55
    #
    # Note that setting --with-pg-config=${postgresql}/bin/pg_config would add
    # an unnecessary reference to the entire postgresql package.
    buildFlags = [ "--with-pg-config=ignore" ];
    nativeBuildInputs = [ pkg-config ];
    buildInputs = [ postgresql ];
  };

  psych = attrs: {
    buildInputs = [ libyaml ];
  };

  puma = attrs: {
    buildInputs = [ openssl ];
  };

  "pygments.rb" = attrs: {
    buildInputs = [ python3 ];
  };

  rack = attrs: {
    meta.mainProgram = "rackup";
  };

  railties = attrs: {
    meta.mainProgram = "rails";
  };

  rainbow = attrs: {
    buildInputs = [ rainbow_rake ];
  };

  rbczmq = { ... }: {
    buildInputs = [ zeromq czmq ];
    buildFlags = [ "--with-system-libs" ];
  };

  rbnacl = spec:
    if lib.versionOlder spec.version "6.0.0" then {
      postInstall = ''
        sed -i $(cat $out/nix-support/gem-meta/install-path)/lib/rbnacl.rb -e "2a \
        RBNACL_LIBSODIUM_GEM_LIB_PATH = '${libsodium.out}/lib/libsodium${stdenv.hostPlatform.extensions.sharedLibrary}'
        "
      '';
    } else {
      dontBuild = false;
      postPatch = ''
        substituteInPlace lib/rbnacl/sodium.rb \
          --replace 'ffi_lib ["sodium"' \
                    'ffi_lib ["${libsodium}/lib/libsodium${stdenv.hostPlatform.extensions.sharedLibrary}"'
      '';
    };

  re2 = attrs: {
    buildInputs = [ re2 ];
    buildFlags = [
      "--enable-system-libraries"
    ];
  };

  rest-client = attrs: {
    meta.mainProgram = "restclient";
  };

  rmagick = attrs: {
    nativeBuildInputs = [ pkg-config ];
    buildInputs = [ imagemagick which ];
  };

  rouge = attrs: {
    meta.mainProgram = "rougify";
  };

  rpam2 = attrs: {
    buildInputs = [ linux-pam ];
  };

  rspec-core = attrs: {
    meta.mainProgram = "rspec";
  };

  ruby-libvirt = attrs: {
    nativeBuildInputs = [ pkg-config ];
    buildInputs = [ libvirt ];
    buildFlags = [
      "--with-libvirt-include=${libvirt}/include"
      "--with-libvirt-lib=${libvirt}/lib"
    ];
  };

  ruby-lxc = attrs: {
    buildInputs = [ lxc ];
  };

  ruby-terminfo = attrs: {
    buildInputs = [ ncurses ];
    buildFlags = [
      "--with-cflags=-I${ncurses.dev}/include"
      "--with-ldflags=-L${ncurses.out}/lib"
    ];
    dontBuild = false;
    postPatch = ''
      substituteInPlace extconf.rb --replace 'rubyio.h' 'ruby/io.h'
      substituteInPlace terminfo.c \
        --replace 'rubyio.h' 'ruby/io.h' \
        --replace 'rb_cData' 'rb_cObject'
    '';
  };

  ruby-vips = attrs: {
    postInstall = ''
      cd "$(cat $out/nix-support/gem-meta/install-path)"

      substituteInPlace lib/vips.rb \
        --replace 'library_name("vips", 42)' '"${lib.getLib vips}/lib/libvips${stdenv.hostPlatform.extensions.sharedLibrary}"' \
        --replace 'library_name("glib-2.0", 0)' '"${glib.out}/lib/libglib-2.0${stdenv.hostPlatform.extensions.sharedLibrary}"' \
        --replace 'library_name("gobject-2.0", 0)' '"${glib.out}/lib/libgobject-2.0${stdenv.hostPlatform.extensions.sharedLibrary}"'
    '';
  };

  rugged = attrs: {
    nativeBuildInputs = [ cmake pkg-config which ] ++ lib.optional stdenv.isDarwin libiconv;
    buildInputs = [ openssl libssh2 zlib ];
    dontUseCmakeConfigure = true;
  };

  sassc = attrs: {
    nativeBuildInputs = [ rake ];
    dontBuild = false;
    SASS_LIBSASS_PATH = toString libsass;
    postPatch = ''
      substituteInPlace lib/sassc/native.rb \
        --replace 'gem_root = spec.gem_dir' 'gem_root = File.join(__dir__, "../../")'
    '';
  } // (lib.optionalAttrs stdenv.isDarwin {
    # https://github.com/NixOS/nixpkgs/issues/19098
    buildFlags = [ "--disable-lto" ];
  });

  sass-embedded = attrs: {
    # Patch the Rakefile to use our dart-sass and not try to fetch anything.
    dontBuild = false;
    postPatch = ''
      substituteInPlace ext/sass/Rakefile \
        --replace \'dart-sass/sass\' \'${dart-sass}/bin/sass\' \
        --replace ' => %w[dart-sass]' ""
    '';
  };

  scrypt = attrs: lib.optionalAttrs stdenv.isDarwin {
    dontBuild = false;
    postPatch = ''
      sed -i -e "s/-arch i386//" Rakefile ext/scrypt/Rakefile
    '';
  };

  semian = attrs: {
    buildInputs = [ openssl ];
  };

  sequel_pg = attrs: {
    buildInputs = [ postgresql ];
  };

  snappy = attrs: {
    buildInputs = [ args.snappy ];
  };

  sqlite3 = attrs: if lib.versionAtLeast attrs.version "1.5.0"
  then {
    nativeBuildInputs = [ pkg-config ];
    buildInputs = [ sqlite ];
    buildFlags = [
      "--enable-system-libraries"
    ];
  }
  else {
    buildFlags = [
      "--with-sqlite3-include=${sqlite.dev}/include"
      "--with-sqlite3-lib=${sqlite.out}/lib"
    ];
  };

  rb-readline = attrs: {
    dontBuild = false;
    postPatch = ''
      substituteInPlace lib/rbreadline.rb \
        --replace 'infocmp' '${ncurses}/bin/infocmp'
    '';
  };

  taglib-ruby = attrs: {
    buildInputs = [ taglib ];
  };

  timfel-krb5-auth = attrs: {
    buildInputs = [ libkrb5 ];
  };

  tiny_tds = attrs: {
    nativeBuildInputs = [ pkg-config openssl ];
    buildInputs = [ freetds ];
  };

  treetop = attrs: {
    meta.mainProgram = "tt";
  };

  typhoeus = attrs: {
    buildInputs = [ curl ];
  };

  tzinfo = attrs: lib.optionalAttrs (lib.versionAtLeast attrs.version "1.0") {
    dontBuild = false;
    postPatch =
      let
        path = if lib.versionAtLeast attrs.version "2.0"
               then "lib/tzinfo/data_sources/zoneinfo_data_source.rb"
               else "lib/tzinfo/zoneinfo_data_source.rb";
      in
        ''
          substituteInPlace ${path} \
            --replace "/usr/share/zoneinfo" "${tzdata}/share/zoneinfo"
        '';
  };

  uuid4r = attrs: {
    buildInputs = [ which libossp_uuid ];
  };

  whois = attrs: {
    meta.mainProgram = "whoisrb";
  };

  xapian-ruby = attrs: {
    # use the system xapian
    dontBuild = false;
    nativeBuildInputs = [ rake pkg-config bundler ];
    buildInputs = [ xapian zlib ];
    postPatch = ''
      cp ${./xapian-Rakefile} Rakefile
    '';
    preInstall = ''
      export XAPIAN_CONFIG=${xapian}/bin/xapian-config
    '';
  };

  zlib = attrs: {
    buildInputs = [ zlib ];
  };

  zookeeper = attrs: {
    buildInputs = lib.optionals stdenv.isDarwin [ cctools ];
  };
}