summaryrefslogtreecommitdiffstats
path: root/pkgs/tools/typesetting/tex/texlive/bin.nix
blob: b13dd6c3eec32252643ba5e92d29c3f9781d4ce0 (plain)
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
{
  lib,
  stdenv,
  fetchurl,
  fetchFromGitHub,
  fetchpatch,
  unzip,
  buildPackages,
  texlive,
  gnum4,
  jdk_headless,
  zlib,
  libiconv,
  libpng,
  libx11,
  freetype,
  ttfautohint,
  gd,
  libxaw,
  icu,
  ghostscript_headless,
  libxpm,
  libxmu,
  libxext,
  perl,
  perlPackages,
  python3Packages,
  pkg-config,
  autoconf,
  automake,
  libtool,
  cmake,
  ninja,
  libpaper,
  graphite2,
  zziplib,
  harfbuzzFull,
  potrace,
  gmp,
  mpfr,
  mupdf-headless,
  brotli,
  cairo,
  pixman,
  libxi,
  libxfixes,
  clisp,
  biber,
  woff2,
  xxhash,
  makeWrapper,
  useFixedHashes ? true,
  asymptote,
  biber-ms,
  tlpdb,
  luajit,
  ...
}@args:

# Useful resource covering build options:
# http://tug.org/texlive/doc/tlbuild.html

let
  withSystemLibs = map (libname: "--with-system-${libname}");

  year = toString ((import ./tlpdb.nix)."00texlive.config").year;
  version = year; # keep names simple for now

  # detect and stop redundant rebuilds that may occur when building new fixed hashes
  assertFixedHash =
    name: src:
    if !useFixedHashes || src ? outputHash then
      src
    else
      throw "The TeX Live package '${src.pname}' must have a fixed hash before building '${name}'.";

  # list of packages whose binaries are built in core, core-big
  # generated manually by inspecting ${core}/bin
  corePackages = [
    "afm2pl"
    "aleph"
    "autosp"
    "axodraw2"
    "bibtex"
    "cjkutils"
    "ctie"
    "cweb"
    "detex"
    "dtl"
    "dvi2tty"
    "dvicopy"
    "dvidvi"
    "dviljk"
    "dviout-util"
    "dvipdfmx"
    "dvipos"
    "dvips"
    "fontware"
    "gregoriotex"
    "gsftopk"
    "hitex"
    "kpathsea"
    "lacheck"
    "lcdftypetools"
    "m-tx"
    "makeindex"
    "mfware"
    "musixtnt"
    "omegaware"
    "patgen"
    "pdftex"
    "pdftosrc"
    "pmx"
    "ps2eps"
    "ps2pk"
    "psutils"
    "ptex"
    "seetexk"
    "synctex"
    "t1utils"
    "tex"
    "tex4ht"
    "texlive-scripts-extra"
    "texware"
    "tie"
    "tpic2pdftex"
    "ttfutils"
    "uptex"
    "velthuis"
    "vlna"
    "web"
    "xml2pmx"
  ];
  coreBigPackages = [
    "metafont"
    "mflua"
    "metapost"
    "luatex"
    "luahbtex"
    "upmendex"
    "xetex"
  ]
  ++ lib.optional withLuaJIT "luajittex";
  binPackages = lib.getAttrs (corePackages ++ coreBigPackages) tlpdb;

  common = {
    # initial TeX Live 2025 release
    # src = fetchurl {
    #   url = "mirror://texhistoric/systems/texlive/${year}/texlive-${year}0308-source.tar.xz";
    #   hash = "sha256-//2xo9FDwXekOYoiKaQNaojxgJjl9tz9V2SMnyQXSQ8=";
    # };

    # 2025.2 update
    src = fetchurl {
      url = "https://github.com/TeX-Live/texlive-source/archive/refs/tags/svn74917.tar.gz";
      hash = "sha256-QgUN5LOFeD6Jt0ENF6Uwi516D8PH+TXZ+MCO8bCTHqE=";
    };

    prePatch = ''
      for i in texk/kpathsea/mktex*; do
        sed -i '/^mydir=/d' "$i"
      done

      # ST_NLINK_TRICK causes kpathsea to treat folders with no real subfolders
      # as leaves, even if they contain symlinks to other folders; must be
      # disabled to work correctly with the nix store", see section 5.3.6
      # “Subdirectory expansion” of the kpathsea manual
      # http://mirrors.ctan.org/systems/doc/kpathsea/kpathsea.pdf for more
      # details
      sed -i '/^#define ST_NLINK_TRICK/d' texk/kpathsea/config.h
    ''
    +
      # when cross compiling, we must use himktables from PATH
      # (i.e. from buildPackages.texlive.bin.core.dev)
      lib.optionalString (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) ''
        sed -i 's|\./himktables|himktables|' texk/web2c/Makefile.in
      '';

    configureFlags = [
      "--with-banner-add=/nixos.org"
      "--disable-missing"
      # terminate if a requested program or feature must be
      # disabled, e.g., due to missing libraries
      "--disable-native-texlive-build" # do not build for the TeX Live binary distribution
      "--enable-shared" # "--enable-cxx-runtime-hack" # static runtime
      "--enable-tex-synctex"
      "--disable-texlive" # do not build the texlive (TeX Live scripts) package
      "--disable-linked-scripts" # do not install the linked scripts
      "-C" # use configure cache to speed up
      "CFLAGS=-std=gnu17" # fix build with gcc15
    ]
    ++ withSystemLibs [
      # see "from TL tree" vs. "Using installed"  in configure output
      "zziplib"
      "mpfr"
      "gmp"
      "pixman"
      "potrace"
      "gd"
      "freetype2"
      "libpng"
      "libpaper"
      "zlib"
    ]
    ++ lib.optional (
      stdenv.hostPlatform != stdenv.buildPlatform
    ) "BUILDCC=${buildPackages.stdenv.cc.targetPrefix}cc";

    # move binaries to corresponding split outputs, based on content of texlive.tlpdb
    binToOutput = lib.listToAttrs (
      lib.concatMap (
        n:
        map (v: {
          name = v;
          value = builtins.replaceStrings [ "-" ] [ "_" ] n;
        }) binPackages.${n}.binfiles or [ ]
      ) (builtins.attrNames binPackages)
    );

    moveBins = ''
      for bin in "$out/bin"/* ; do
        bin="''${bin##*/}"
        package="''${binToOutput[$bin]}"
        if [[ -n "$package" ]] ; then
          if [[ -z "''${!package}" ]] ; then
            echo "WARNING: missing output '$package' for binary '$bin', leaving in 'out'"
          else
            mkdir -p "''${!package}"/bin
            mv "$out/bin/$bin" "''${!package}"/bin/
            if [[ -e "$out/share/texmf-dist/scripts/$bin" ]] ; then
              mkdir -p "''${!package}"/share/texmf-dist/scripts
              mv "$out/share/texmf-dist/scripts/$bin" "''${!package}"/share/texmf-dist/scripts/
            fi
          fi
        else
          echo "WARNING: no output known for binary '$bin', leaving in 'out'"
        fi
      done
    '';
  };

  # RISC-V: https://github.com/LuaJIT/LuaJIT/issues/628
  withLuaJIT = lib.meta.availableOn stdenv.hostPlatform luajit;
in
rec {
  # un-indented

  texliveYear = year;

  core = stdenv.mkDerivation rec {
    pname = "texlive-bin";
    inherit version;

    __structuredAttrs = true;

    inherit (common) binToOutput src prePatch;

    outputs = [
      "out"
      "dev"
      "man"
      "info"
    ]
    ++ (map (builtins.replaceStrings [ "-" ] [ "_" ]) corePackages);

    nativeBuildInputs = [
      pkg-config
    ]
    ++ lib.optionals (!stdenv.buildPlatform.canExecute stdenv.hostPlatform) (
      with texlive.bin.core;
      [
        # configure: error: tangle was not found but is required when cross-compiling.
        # dev (himktables) is used when building hitex to generate the additional source file hitables.c
        web # tangle
        cweb # ctangle
        omegaware # otangle
        tie # tie see "Building TeX Live" 6.4.2 Cross problems
        dev # himktables
      ]
    );

    buildInputs = [
      # teckit
      zziplib
      mpfr
      gmp
      pixman
      gd
      freetype
      libpng
      libpaper
      zlib
      perl
    ];

    hardeningDisable = [ "format" ];

    preConfigure = ''
      rm -r libs/{cairo,freetype2,gd,gmp,graphite2,harfbuzz,icu,libpaper,libpng} \
        libs/{lua53,luajit,mpfr,pixman,pplib,teckit,zlib,zziplib} \
        texk/{bibtex-x,chktex,dvipng,dvisvgm,upmendex,xdvik} \
        utils/{asymptote,texdoctk,xindy,xpdfopen}
      mkdir WorkDir
      cd WorkDir
    '';
    configureScript = "../configure";

    depsBuildBuild = [ buildPackages.stdenv.cc ];

    configureFlags =
      common.configureFlags
      ++ [ "--without-x" ] # disable xdvik and xpdfopen
      ++ map (what: "--disable-${what}") [
        "chktex"
        "dvisvgm"
        "dvipng" # ghostscript dependency
        "luatex"
        "luajittex"
        "luahbtex"
        "luajithbtex"
        "mp"
        "pmp"
        "upmp"
        "mf"
        "mflua"
        "mfluajit" # cairo would bring in X and more
        "xetex"
        "bibtexu"
        "bibtex8"
        "bibtex-x"
        "upmendex" # ICU isn't small
      ];

    enableParallelBuilding = true;

    doCheck = false; # triptest fails, likely due to missing TEXMF tree
    preCheck = "patchShebangs ../texk/web2c";

    installTargets = [ "install" ];

    # TODO: perhaps improve texmf.cnf search locations
    postInstall =
      # install himktables in separate output for use in cross compilation
      ''
        mkdir -p $dev/bin
        cp texk/web2c/.libs/himktables $dev/bin/himktables
      ''
      + common.moveBins
      # remove redundant texmf-dist (content provided by TeX Live packages)
      + ''
        rm -frv "$out"/share/texmf-dist

        mkdir -p "${placeholder "psutils"}"/share/texmf-dist/scripts/psutils
        cp texk/psutils/{extractres.pl,includeres.pl,psjoin.pl} "${placeholder "psutils"}"/share/texmf-dist/scripts/psutils
      ''
      # remove broken symlinks
      + ''
        rm "$out"/bin/{eptex,ptex,uptex}
        rm "${placeholder "ptex"}"/bin/{pbibtex,pdvitype,ppltotf,ptftopl}
      '';

    passthru = { inherit version buildInputs; };

    meta = {
      description = "Basic binaries for TeX Live";
      homepage = "http://www.tug.org/texlive";
      license = lib.licenses.gpl2Plus;
      maintainers = with lib.maintainers; [
        veprbl
        raskin
        jwiegley
        xworld21
      ];
      platforms = lib.platforms.all;
    };
  };

  inherit (core-big)
    metafont
    mflua
    metapost
    luatex
    luahbtex
    xetex
    ;
  luajittex = core.big.luajittex or null;
  core-big = stdenv.mkDerivation {
    pname = "texlive-bin-big";
    inherit version;

    __structuredAttrs = true;

    inherit (common) binToOutput src prePatch;

    patches = [
      # improves reproducibility of fmt files
      # see discussion at https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1009196
      (fetchpatch {
        name = "lua_fixed_hash.patch";
        url = "https://bugs.debian.org/cgi-bin/bugreport.cgi?att=1;bug=1009196;filename=lua_fixed_hash.patch;msg=45";
        sha256 = "sha256-FTu1eRd3AUU7IRs2/7e7uwHuvZsrzTBPypbcEZkU7y4=";
      })
      # The original LuaJIT version number used here is 2.1.1736781742.
      # The patch number in this is the unix epoch timestamp of the commit used.
      # TexLive already truncates the patch number to the last 5 digits (81742
      # in this case), however, this number will roll over every 1.1 days (1e5
      # seconds), making it non-monotonic.
      # Furthermore, the nix-darwin linker requires version numbers to be <=
      # 1023.
      # We therefore opt to choose a 3-digit sequence from the unix epoch that
      # gives a good tradeoff between when it will roll over, and how often it
      # will actually change: digits 9-7 (counting from the right, i.e., 736 in
      # this case) yields a number that changes every 11.6 days (1e6 seconds,
      # it is unlikely texlive will be updated on a shorter interval), and will
      # stay stable for 31.7 years (1e9 seconds).
      ./truncate-luajit-version-number.patch
    ];

    hardeningDisable = [ "format" ];

    inherit (core) depsBuildBuild;
    nativeBuildInputs = core.nativeBuildInputs ++ [
      autoconf
      automake
      libtool
    ];
    buildInputs = core.buildInputs ++ [
      core
      cairo
      harfbuzzFull
      icu
      graphite2
      libx11
      potrace
    ];

    # autoconf 2.72 has a bug where AC_PROG_CXX would reject c++20 compilers,
    # and attempts to switch to c++98 or c++11 instead of using the compiler's
    # default. when using gcc 16, which defaults to c++20, this causes
    # texlive-bin-big to be built with c++11, leading to issues with some of
    # the included icu4c headers. this bug was fixed in autoconf 2.73, so we
    # regenerate the autoconf declarations with the newest version of autconf.
    # once the configure scripts distributed with the source are regenerated
    # with autoconf 2.73+, the `reautoconf` call should be removed.
    #
    # deleting the unused packages speeds up configure by a considerable margin
    # and ensures we do not rebuild existing libraries by mistake
    preConfigure = ''
      substituteInPlace ./reautoconf --replace-fail "/bin/pwd" "pwd"
      ./reautoconf

      rm -r libs/{cairo,freetype2,gd,gmp,graphite2,harfbuzz,icu,libpaper,libpng} \
        libs/{mpfr,pixman,xpdf,zlib,zziplib} \
        texk/{afm2pl,bibtex-x,chktex,cjkutils,detex,dtl,dvi2tty,dvidvi,dviljk,dviout-util} \
        texk/{dvipdfm-x,dvipng,dvipos,dvipsk,dvisvgm,gregorio,gsftopk,kpathsea} \
        texk/{lcdf-typetools,makeindexk,makejvf,mendexk,musixtnt,ps2pk,psutils,ptexenc} \
        texk/{seetexk,tex4htk,texlive,ttf2pk2,ttfdump,xdvik} \
        utils/{asymptote,autosp,axodraw2,devnag,lacheck,m-tx,pmx,ps2eps,t1utils,texdoctk} \
        utils/{tpic2pdftex,vlna,xindy,xml2pmx,xpdfopen}
      mkdir WorkDir
      cd WorkDir
    ''
    # force XeTeX to use fontconfig instead of Core Text, so that fonts can be made available via FONTCONFIG_FILE,
    # by tricking configure into thinking that the relevant test result is already in the config cache
    + lib.optionalString stdenv.hostPlatform.isDarwin ''
      export kpse_cv_have_ApplicationServices=no
    '';

    configureFlags =
      common.configureFlags
      ++ withSystemLibs [
        "kpathsea"
        "ptexenc"
        "cairo"
        "harfbuzz"
        "icu"
        "graphite2"
      ]
      ++
        map (prog: "--disable-${prog}") # don't build things we already have
          # list from texk/web2c/configure
          (
            [
              "tex"
              "ptex"
              "eptex"
              "uptex"
              "euptex"
              "aleph"
              "hitex"
              "pdftex"
              "web-progs"
              "synctex"
            ]
            ++ lib.optionals (!withLuaJIT) [
              "luajittex"
              "luajithbtex"
              "mfluajit"
            ]
          )
      # disable all packages, re-enable upmendex, web2c packages
      ++ [
        "--disable-all-pkgs"
        "--enable-upmendex"
        "--enable-web2c"
      ]
      # kpathsea requires specifying the kpathsea location manually
      ++ [ "--with-kpathsea-includes=${core.dev}/include" ];

    configureScript = "../configure";

    enableParallelBuilding = true;

    doCheck = false; # fails

    outputs = [
      "out"
      "dev"
      "man"
      "info"
    ]
    ++ (map (builtins.replaceStrings [ "-" ] [ "_" ]) coreBigPackages)
    # some outputs of metapost, omegaware are for ptex/uptex
    ++ [
      "ptex"
      "uptex"
    ]
    # unavoidable duplicates from core
    ++ [
      "ctie"
      "cweb"
      "omegaware"
      "texlive_scripts_extra"
      "tie"
      "web"
    ];
    postInstall = common.moveBins;
  };

  chktex = stdenv.mkDerivation {
    pname = "chktex";
    inherit (texlive.pkgs.chktex) version;

    inherit (common) src;

    nativeBuildInputs = [ pkg-config ];
    # perl used in shebang of script bin/deweb
    buildInputs = [
      core # kpathsea
      perl
    ];

    preConfigure = "cd texk/chktex";

    configureFlags = common.configureFlags ++ [ "--with-system-kpathsea" ];

    enableParallelBuilding = true;
  };

  # The LuaMetaTeX engine (distributed since TeX Live 2023) must be built separately.
  # For details on how TeX Live packages ConTeXt, see
  # https://github.com/gucci-on-fleek/context-packaging
  context =
    let
      version = "2.11.08";
      level = "20260217";
    in
    stdenv.mkDerivation {
      pname = "luametatex";
      version = "${version}-${level}";

      src = texlive.pkgs.context.texsource + "/source/context/base/luametatex-${level}.src.zip";
      sourceRoot = ".";

      enableParallelBuilding = true;
      nativeBuildInputs = [
        cmake
        ninja
        unzip
      ];

      meta = {
        description = "LUAMETATEX engine is a follow up on LUATEX and is again part of CONTEXT development";
        homepage = "https://www.pragma-ade.nl/luametatex-1.htm";
        license = lib.licenses.gpl2Plus;
        maintainers = with lib.maintainers; [
          apfelkuchen6
          xworld21
        ];
      };
    };

  dvisvgm = stdenv.mkDerivation rec {
    pname = "dvisvgm";
    version = "3.6";

    src =
      assert lib.assertMsg (version == texlive.pkgs.dvisvgm.version)
        "dvisvgm: TeX Live version (${texlive.pkgs.dvisvgm.version}) different from source (${version}), please update dvisvgm";
      fetchurl {
        url = "https://github.com/mgieseki/dvisvgm/releases/download/${version}/dvisvgm-${version}.tar.gz";
        hash = "sha256-JkRrs7EHOf8JJcnkFrdtLSIgdcnV3Pr+biFGCdBy7Ro=";
      };

    configureFlags = [
      "--disable-manpage" # man pages are provided by the doc container
      "--with-ttfautohint"
    ];

    # PDF handling requires mutool (from mupdf) since Ghostscript 10.01
    postPatch = ''
      substituteInPlace src/PDFHandler.cpp \
        --replace-fail 'Process("mutool"' "Process(\"$(PATH="$HOST_PATH" command -v mutool)\""
    '';

    nativeBuildInputs = [ pkg-config ];
    buildInputs = [
      core
      brotli
      ghostscript_headless
      zlib
      freetype
      ttfautohint
      woff2
      potrace
      xxhash
      mupdf-headless
    ];

    enableParallelBuilding = true;
  };

  dvipng = stdenv.mkDerivation {
    pname = "dvipng";
    inherit (texlive.pkgs.dvipng) version;

    inherit (common) src;

    nativeBuildInputs = [
      perl
      pkg-config
      makeWrapper
    ];
    buildInputs = [
      core # kpathsea
      zlib
      libpng
      freetype
      gd
      ghostscript_headless
    ];

    preConfigure = ''
      cd texk/dvipng
      patchShebangs doc/texi2pod.pl
    '';

    configureFlags = common.configureFlags ++ [
      "--with-system-kpathsea"
      "--with-gs=yes"
      "--disable-debug"
    ];

    GS = lib.getExe ghostscript_headless;

    enableParallelBuilding = true;
  };

  asymptote =
    let
      version = "3.09";
    in
    args.asymptote.overrideAttrs (
      finalAttrs: prevAttrs: {
        version =
          assert lib.assertMsg (version == texlive.pkgs.asymptote.version)
            "asymptote: TeX Live version (${texlive.pkgs.asymptote.version}) different from source in bin.nix (${version}), please update it";
          version;

        # keep local src and patches even if duplicated in the top level asymptote
        # so that top level updates do not break texlive
        src = fetchurl {
          url = "mirror://sourceforge/asymptote/${finalAttrs.version}/asymptote-${finalAttrs.version}.src.tgz";
          hash = "sha256-unM6mfyq8MCajo8wtG/ksr4E6mQNK/A03gGIa9Fxeuc=";
        };

        texContainer = texlive.pkgs.asymptote.tex;
        texdocContainer = texlive.pkgs.asymptote.texdoc;

        preConfigure = prevAttrs.preConfigure + ''
          substituteInPlace Makefile.in --replace-fail '/bin/ls' 'ls'
        '';
      }
    );

  inherit biber;
  inherit biber-ms;
  bibtexu = bibtex8;
  bibtex8 = stdenv.mkDerivation {
    pname = "bibtex-x";
    inherit (texlive.pkgs.bibtexu) version;

    inherit (common) src;

    nativeBuildInputs = [ pkg-config ];
    buildInputs = [
      core # kpathsea
      icu
    ];

    preConfigure = "cd texk/bibtex-x";

    configureFlags = common.configureFlags ++ [
      "--with-system-kpathsea"
      "--with-system-icu"
    ];

    enableParallelBuilding = true;
  };

  xdvi = stdenv.mkDerivation {
    pname = "xdvi";
    inherit (texlive.pkgs.xdvi) version;

    inherit (common) src;

    nativeBuildInputs = [ pkg-config ];
    buildInputs = [
      core # kpathsea
      freetype
      ghostscript_headless
      libx11
      libxaw
      libxi
      libxpm
      libxmu
      libxaw
      libxext
      libxfixes
    ];

    preConfigure = "cd texk/xdvik";

    configureFlags = common.configureFlags ++ [
      "--with-system-kpathsea"
      "--with-system-libgs"
    ];

    enableParallelBuilding = true;

    postInstall = ''
      substituteInPlace "$out/bin/xdvi" \
        --replace-fail "exec xdvi-xaw" "exec '$out/bin/xdvi-xaw'"
    '';
    # TODO: it's suspicious that mktexpk generates fonts into ~/.texlive2014
  };

  xpdfopen = stdenv.mkDerivation {
    pname = "xpdfopen";
    inherit (texlive.pkgs.xpdfopen) version;

    inherit (common) src;

    buildInputs = [ libx11 ];

    preConfigure = "cd utils/xpdfopen";

    enableParallelBuilding = true;
  };

  # tex4ht.jar
  # we build this as a TeX package, but under texlive.bin to avoid exposing it in texlivePackages
  tex4htJar = stdenv.mkDerivation {
    pname = "tex4ht-jar";
    inherit (texlive.pkgs.tex4ht) meta version;

    outputs = [ "tex" ];

    src = texlive.pkgs.tex4ht.texsource + "/source/generic/tex4ht";

    nativeBuildInputs = [
      gnum4
      jdk_headless
      (texlive.schemes.texliveBasic.withPackages (ps: [
        # override tex4ht-jar with an empty package to avoid a self dependency
        { pname = "tex4ht-jar"; }
        ps.protex
        ps.tex4ht
      ]))
    ];

    preHook = ''
      export out="$tex"
    '';

    # the current Makefile is broken, so we build the artifact by hand
    # we also use latex instead of htlatex as the latter is orders of magnitude slower
    buildPhase = ''
      make tex4ht-dir.tex

      mkdir -p work.dir/src/tex4ht
      for f in *-xtpipes.tex ; do
        latex -output-directory=work.dir/src/tex4ht "\\RequirePackage{tex4ht}\\input $f"
      done

      mkdir -p work.dir/src/xtpipes
      latex -output-directory=work.dir/src/xtpipes "\\RequirePackage{tex4ht}\\input xtpipes.tex"

      mkdir -p work.dir/src/xtpipes/util
      mv work.dir/src/xtpipes/xtpipes.java.java work.dir/src/xtpipes.java
      mv work.dir/src/xtpipes/ScriptsManager*.java work.dir/src/xtpipes/util

      mkdir -p xtpipes.dir/xtpipes/lib
      cp work.dir/src/xtpipes/xtpipes*.{4xt,dtd} xtpipes.dir/xtpipes/lib

      javac -d xtpipes.dir work.dir/src/{*,*/*,*/*/*}.java
      jar cf tex4ht.dir/texmf/tex4ht/bin/tex4ht.jar -C xtpipes.dir .
    '';

    installPhase = ''
      install -D -t "$tex"/tex4ht/bin tex4ht.dir/texmf/tex4ht/bin/tex4ht.jar
    '';
  };

} # un-indented

//
  lib.optionalAttrs (!clisp.meta.broken) # broken on aarch64 and darwin (#20062)
    {

      xindy = stdenv.mkDerivation {
        pname = "xindy";
        inherit (texlive.pkgs.xindy) version;

        inherit (common) src;

        # If unset, xindy will try to mkdir /homeless-shelter
        HOME = ".";

        prePatch = "cd utils/xindy";
        # hardcode clisp location
        postPatch = ''
          substituteInPlace xindy-*/user-commands/xindy.in \
            --replace-fail "our \$clisp = ( \$is_windows ? 'clisp.exe' : 'clisp' ) ;" \
                           "our \$clisp = '$(type -P clisp)';" \
            --replace-fail 'die "$cmd: Cannot locate xindy modules directory";' \
                           '$modules_dir = "${texlive.pkgs.xindy.tex}/xindy/modules"; die "$cmd: Cannot locate xindy modules directory" unless -d $modules_dir;'
        '';

        nativeBuildInputs = [
          pkg-config
          perl
        ];
        buildInputs = [
          clisp
          libiconv
          perl
        ];

        configureFlags = [
          "--with-clisp-runtime=system"
          "--disable-xindy-docs"
          "--disable-xindy-rules"
        ];

        preInstall = ''mkdir -p "$out/bin" '';
        # fixup various file-location errors of: lib/xindy/{xindy.mem,modules/}
        postInstall = ''
          mkdir -p "$out/lib/xindy"
          mv "$out"/{bin/xindy.mem,lib/xindy/}
        '';
      };

    }