summaryrefslogtreecommitdiffstats
path: root/pkgs/tools/typesetting/tex/texlive/build-tex-env.nix
blob: d3a27bd9dbf8379e72be060cf8ff4bb44cfa007f (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
{
  # texlive package set
  pkgs,

  tlpdbVersion,

  lib,
  buildEnv,
  libfaketime,
  makeFontsConf,
  makeWrapper,
  runCommand,
  toTLPkgSets,
  perl,

  # common runtime dependencies
  coreutils,
  gawk,
  gnugrep,
  gnused,
  ghostscript,
}:

lib.fix (
  buildTeXEnv:
  {
    requiredTeXPackages ? ps: [ ps.scheme-infraonly ],
    ### texlive.combine backward compatibility
    __extraName ? "combined",
    __extraVersion ? "",
    # emulate the old texlive.combine (e.g. add man pages to main output)
    __combine ? false,
    # adjust behavior further if called from the texlive.combine wrapper
    __fromCombineWrapper ? false,
    # build only the formats of a package (for internal use!)
    __formatsOf ? null,
  }:
  buildEnv (
    finalAttrs:

    let
      # if necessary, convert old style { pkgs = [ ... ]; } packages to attribute sets
      isOldPkgList = p: !p.outputSpecified or false && p ? pkgs && builtins.all (p: p ? tlType) p.pkgs;
      ensurePkgSets =
        ps:
        if !finalAttrs.passthru.__fromCombineWrapper && builtins.any isOldPkgList ps then
          let
            oldPkgLists = builtins.partition isOldPkgList ps;
          in
          oldPkgLists.wrong ++ lib.concatMap toTLPkgSets oldPkgLists.right
        else
          ps;

      pkgList = rec {
        # resolve dependencies of the packages that affect the runtime
        all =
          let
            packages = ensurePkgSets (finalAttrs.passthru.requiredTeXPackages pkgs);
            runtime = builtins.partition (
              p:
              p.outputSpecified or false
              -> builtins.elem (p.tlOutputName or p.outputName) [
                "out"
                "tex"
                "tlpkg"
              ]
            ) packages;
            keySet = p: {
              key =
                p.pname or p.name
                + lib.optionalString (p.outputSpecified or false) ("-" + p.tlOutputName or p.outputName or "");
              inherit p;
              tlDeps =
                if p ? tlDeps then
                  (if builtins.isFunction p.tlDeps then p.tlDeps pkgs else ensurePkgSets p.tlDeps)
                else
                  [ ];
            };
          in
          # texlive.combine: the wrapper already resolves all dependencies
          if finalAttrs.passthru.__fromCombineWrapper then
            finalAttrs.passthru.requiredTeXPackages null
          else
            builtins.catAttrs "p" (
              builtins.genericClosure {
                startSet = map keySet runtime.right;
                operator = p: map keySet p.tlDeps;
              }
            )
            ++ runtime.wrong;

        # group the specified outputs
        specified = builtins.partition (p: p.outputSpecified or false) all;
        specifiedOutputs = lib.groupBy (p: p.tlOutputName or p.outputName) specified.right;
        otherOutputNames = builtins.catAttrs "key" (
          builtins.genericClosure {
            startSet = map (key: { inherit key; }) (
              lib.concatLists (builtins.catAttrs "outputs" specified.wrong)
            );
            operator = _: [ ];
          }
        );
        otherOutputs = lib.genAttrs otherOutputNames (n: builtins.catAttrs n specified.wrong);
        outputsToInstall = builtins.catAttrs "key" (
          builtins.genericClosure {
            startSet = map (key: { inherit key; }) (
              [ "out" ]
              ++ lib.optional (otherOutputs ? man) "man"
              ++ lib.concatLists (builtins.catAttrs "outputsToInstall" (builtins.catAttrs "meta" specified.wrong))
            );
            operator = _: [ ];
          }
        );

        # split binary and tlpkg from tex, texdoc, texsource
        bin =
          if finalAttrs.passthru.__fromCombineWrapper then
            builtins.filter (p: p.tlType == "bin") all # texlive.combine: legacy filter
          else
            otherOutputs.out or [ ] ++ specifiedOutputs.out or [ ];
        tlpkg =
          if finalAttrs.passthru.__fromCombineWrapper then
            builtins.filter (p: p.tlType == "tlpkg") all # texlive.combine: legacy filter
          else
            otherOutputs.tlpkg or [ ] ++ specifiedOutputs.tlpkg or [ ];

        nonbin =
          if finalAttrs.passthru.__fromCombineWrapper then
            builtins.filter (p: p.tlType != "bin" && p.tlType != "tlpkg") all # texlive.combine: legacy filter
          else
            (
              if finalAttrs.__combine then # texlive.combine: emulate old input ordering to avoid rebuilds
                lib.concatMap (
                  p:
                  lib.optional (p ? tex) p.tex
                  ++ lib.optional ((finalAttrs.withDocs || p ? man) && p ? texdoc) p.texdoc
                  ++ lib.optional (finalAttrs.withSources && p ? texsource) p.texsource
                ) specified.wrong
              else
                otherOutputs.tex or [ ]
                ++ lib.optionals finalAttrs.withDocs (otherOutputs.texdoc or [ ])
                ++ lib.optionals finalAttrs.withSources (otherOutputs.texsource or [ ])
            )
            ++ specifiedOutputs.tex or [ ]
            ++ specifiedOutputs.texdoc or [ ]
            ++ specifiedOutputs.texsource or [ ];

        # outputs that do not become part of the environment
        nonEnvOutputs = lib.subtractLists [ "out" "tex" "texdoc" "texsource" "tlpkg" ] otherOutputNames;

        # packages that contribute to config files and formats
        fontMaps = lib.filter (p: p ? fontMaps && (p.tlOutputName or p.outputName == "tex")) nonbin;
        sortedFontMaps = builtins.sort (a: b: a.pname < b.pname) fontMaps;
        hyphenPatterns = lib.filter (
          p: p ? hyphenPatterns && (p.tlOutputName or p.outputName == "tex")
        ) nonbin;
        sortedHyphenPatterns = builtins.sort (a: b: a.pname < b.pname) hyphenPatterns;
        formatPkgs = lib.filter (
          p:
          p ? formats
          && (p.outputSpecified or false -> p.tlOutputName or p.outputName == "tex")
          && builtins.any (f: f.enabled or true) p.formats
        ) all;
        sortedFormatPkgs =
          if __formatsOf != null then [ __formatsOf ] else builtins.sort (a: b: a.pname < b.pname) formatPkgs;
        formats = map (
          p:
          buildTeXEnv {
            requiredTeXPackages =
              ps:
              [
                ps.scheme-infraonly
                p
              ]
              ++ hyphenPatterns;
            __formatsOf = p;
          }
        ) sortedFormatPkgs;
      };

      # list generated by inspecting `grep -IR '\([^a-zA-Z]\|^\)gs\( \|$\|"\)' "$TEXMFDIST"/scripts`
      # and `grep -IR rungs "$TEXMFDIST"`
      # and ignoring luatex, perl, and shell scripts (those must be patched using postFixup)
      needsGhostscript = lib.any (
        p:
        lib.elem p.pname [
          "context"
          "dvipdfmx"
          "latex-papersize"
          "lyluatex"
        ]
      ) pkgList.bin;

      pname =
        if finalAttrs.__combine then
          "texlive-${finalAttrs.passthru.__extraName}" # texlive.combine: old name
        else
          "texlive";
      version =
        if finalAttrs.__combine then
          "${toString tlpdbVersion.year}${finalAttrs.passthru.__extraVersion}" # texlive.combine: old version
        else
          "${toString tlpdbVersion.year}-r${toString tlpdbVersion.revision}-"
          + (lib.optionalString tlpdbVersion.frozen "final-")
          + (if __formatsOf != null then "${__formatsOf.pname}-fmt" else "env");

      name = "${pname}-${version}";

      texmfdist = buildEnv {
        name = "${name}-texmfdist";

        # remove fake derivations (without 'outPath') to avoid undesired build dependencies
        paths = builtins.catAttrs "outPath" pkgList.nonbin;

        # mktexlsr
        nativeBuildInputs = [
          pkgs.texlive-scripts # for mktexlsr.pl with --sort support
          perl
        ];

        postBuild = # generate ls-R database
          ''
            perl ${pkgs.texlive-scripts.tex}/scripts/texlive/mktexlsr.pl --sort "$out"
          '';
      };

      tlpkg = buildEnv {
        name = "${name}-tlpkg";

        # remove fake derivations (without 'outPath') to avoid undesired build dependencies
        paths = builtins.catAttrs "outPath" pkgList.tlpkg;
      };

      # the 'non-relocated' packages must live in $TEXMFROOT/texmf-dist
      # and sometimes look into $TEXMFROOT/tlpkg (notably fmtutil, updmap look for perl modules in both)
      texmfroot =
        runCommand "${name}-texmfroot"
          {
            inherit texmfdist tlpkg;
          }
          ''
            mkdir -p "$out"
            ln -s "$texmfdist" "$out"/texmf-dist
            ln -s "$tlpkg" "$out"/tlpkg
          '';

      # texlive.combine: expose info and man pages in usual /share/{info,man} location
      doc = buildEnv {
        name = "${name}-doc";

        paths = [ (texmfdist.outPath + "/doc") ];
        extraPrefix = "/share";

        pathsToLink = [
          "/info"
          "/man"
        ];
      };

      meta = {
        description =
          "TeX Live environment"
          + lib.optionalString finalAttrs.withDocs " with documentation"
          + lib.optionalString (finalAttrs.withDocs && finalAttrs.withSources) " and"
          + lib.optionalString finalAttrs.withSources " with sources";
        platforms = lib.platforms.all;
        longDescription =
          "Contains the following packages and their transitive dependencies:\n - "
          + lib.concatMapStringsSep "\n - " (
            p:
            p.pname + (lib.optionalString (p.outputSpecified or false) " (${p.tlOutputName or p.outputName})")
          ) (finalAttrs.passthru.requiredTeXPackages pkgs);
      };

      # other outputs
      nonEnvOutputs = lib.genAttrs pkgList.nonEnvOutputs (
        outName:
        buildEnv {
          inherit name;
          paths = builtins.catAttrs "outPath" (
            pkgList.otherOutputs.${outName} or [ ] ++ pkgList.specifiedOutputs.${outName} or [ ]
          );
          derivationArgs = {
            outputs = [ outName ];

            # force the output to be ${outName} or nix-env will not work
            preHook = ''
              export out="''${${outName}}"
            '';

            inherit meta passthru;
          };
        }
      );

      passthru = {
        inherit
          requiredTeXPackages
          __fromCombineWrapper
          __extraName
          __extraVersion
          ;
        # This is set primarily to help find-tarballs.nix to do its job
        includedTeXPackages = builtins.filter lib.isDerivation (
          pkgList.bin
          ++ pkgList.nonbin
          ++ lib.optionals (!finalAttrs.passthru.__fromCombineWrapper) (
            lib.concatMap (
              n: (pkgList.otherOutputs.${n} or [ ] ++ pkgList.specifiedOutputs.${n} or [ ])
            ) pkgList.nonEnvOutputs
          )
        );
        # useful for inclusion in the `fonts.packages` nixos option or for use in devshells
        fonts = "${texmfroot}/texmf-dist/fonts";
        __overrideTeXConfig = lib.warn "__overrideTeXConfig is deprecated, please switch to overrideAttrs" (
          newArgs:
          let
            # arguments of buildTeXEnv before deprecation of __overrideTeXConfig
            prevArgs = {
              inherit (finalAttrs)
                __combine
                ;
              inherit (finalAttrs.passthru)
                requiredTeXPackages
                __extraName
                __extraVersion
                __fromCombineWrapper
                ;
            }
            // lib.optionalAttrs (finalAttrs ? withDocs) { inherit (finalAttrs) withDocs; }
            // lib.optionalAttrs (finalAttrs ? withSources) { inherit (finalAttrs) withSources; };
            appliedArgs = prevArgs // (if builtins.isFunction newArgs then newArgs prevArgs else newArgs);
          in
          finalAttrs.finalPackage.overrideAttrs (
            prevAttrs:
            {
              passthru = prevAttrs.passthru // {
                inherit (appliedArgs)
                  requiredTeXPackages
                  __extraName
                  __extraVersion
                  ;
                __fromCombineWrapper = false;
              };
              inherit (appliedArgs) __combine;
            }
            // lib.optionalAttrs (appliedArgs ? withDocs) { inherit (appliedArgs) withDocs; }
            // lib.optionalAttrs (appliedArgs ? withSources) { inherit (appliedArgs) withSources; }
          )
        );
        withPackages =
          reqs:
          finalAttrs.finalPackage.overrideAttrs (prevAttrs: {
            passthru = prevAttrs.passthru // {
              requiredTeXPackages = ps: reqs ps ++ prevAttrs.passthru.requiredTeXPackages ps;
              __fromCombineWrapper = false;
            };
          });
      };

      # TeXLive::TLOBJ::fmtutil_cnf_lines
      fmtutilLine =
        {
          name,
          engine,
          enabled ? true,
          patterns ? [ "-" ],
          options ? "",
          ...
        }:
        lib.optionalString (!enabled) "#! "
        + "${name} ${engine} ${lib.concatStringsSep "," patterns} ${options}";
      fmtutilLines =
        { pname, formats, ... }:
        [
          "#"
          "# from ${pname}:"
        ]
        ++ map fmtutilLine formats;

      # TeXLive::TLOBJ::language_dat_lines
      langDatLine =
        {
          name,
          file,
          synonyms ? [ ],
          ...
        }:
        [ "${name} ${file}" ] ++ map (s: "=" + s) synonyms;
      langDatLines =
        { pname, hyphenPatterns, ... }:
        [ "% from ${pname}:" ] ++ builtins.concatMap langDatLine hyphenPatterns;

      # TeXLive::TLOBJ::language_def_lines
      # see TeXLive::TLUtils::parse_AddHyphen_line for default values
      langDefLine =
        {
          name,
          file,
          lefthyphenmin ? "",
          righthyphenmin ? "",
          synonyms ? [ ],
          ...
        }:
        map (
          n:
          "\\addlanguage{${n}}{${file}}{}{${if lefthyphenmin == "" then "2" else lefthyphenmin}}{${
            if righthyphenmin == "" then "3" else righthyphenmin
          }}"
        ) ([ name ] ++ synonyms);
      langDefLines =
        { pname, hyphenPatterns, ... }:
        [ "% from ${pname}:" ] ++ builtins.concatMap langDefLine hyphenPatterns;

      # TeXLive::TLOBJ::language_lua_lines
      # see TeXLive::TLUtils::parse_AddHyphen_line for default values
      langLuaLine =
        {
          name,
          file,
          lefthyphenmin ? "",
          righthyphenmin ? "",
          synonyms ? [ ],
          ...
        }@args:
        ''
          ''\t['${name}'] = {
          ''\t''\tloader = '${file}',
          ''\t''\tlefthyphenmin = ${if lefthyphenmin == "" then "2" else lefthyphenmin},
          ''\t''\trighthyphenmin = ${if righthyphenmin == "" then "3" else righthyphenmin},
          ''\t''\tsynonyms = { ${lib.concatStringsSep ", " (map (s: "'${s}'") synonyms)} },
        ''
        + lib.optionalString (args ? file_patterns) "\t\tpatterns = '${args.file_patterns}',\n"
        + lib.optionalString (args ? file_exceptions) "\t\thyphenation = '${args.file_exceptions}',\n"
        + lib.optionalString (args ? luaspecial) "\t\tspecial = '${args.luaspecial}',\n"
        + "\t},";
      langLuaLines =
        { pname, hyphenPatterns, ... }: [ "-- from ${pname}:" ] ++ map langLuaLine hyphenPatterns;

      assembleConfigLines = f: packages: builtins.concatStringsSep "\n" (builtins.concatMap f packages);

      updmapLines = { pname, fontMaps, ... }: [ "# from ${pname}:" ] ++ fontMaps;

    in
    {

      inherit pname version;

      # remove fake derivations (without 'outPath') to avoid undesired build dependencies
      paths =
        builtins.catAttrs "outPath" pkgList.bin
        ++ lib.optionals (!finalAttrs.__combine && __formatsOf == null) pkgList.formats
        ++ lib.optional finalAttrs.__combine doc;
      pathsToLink = [
        "/"
        "/share/texmf-var/scripts"
        "/share/texmf-var/tex/generic/config"
        "/share/texmf-var/web2c"
        "/share/texmf-config"
        "/bin" # ensure these are writeable directories
      ];

      postBuild = ''
        . "${./build-tex-env.sh}"
      '';

      derivationArgs = {
        # use attrNames, attrValues to ensure the two lists are sorted in the same way
        outputs = [
          "out"
        ]
        ++ lib.optionals (!finalAttrs.__combine && __formatsOf == null) (builtins.attrNames nonEnvOutputs);
        otherOutputs = lib.optionals (!finalAttrs.__combine && __formatsOf == null) (
          builtins.attrValues nonEnvOutputs
        );

        strictDeps = true;

        nativeBuildInputs = [
          makeWrapper
          libfaketime
          pkgs."texlive.infra" # mktexlsr
          pkgs.texlive-scripts # fmtutil, updmap
          pkgs.texlive-scripts-extra # texlinks
          perl
        ];

        buildInputs = [
          coreutils
          gawk
          gnugrep
          gnused
        ]
        ++ lib.optional needsGhostscript ghostscript;

        inherit passthru __combine;
        __formatsOf = __formatsOf.pname or null;

        inherit texmfdist texmfroot;

        fontconfigFile = makeFontsConf { fontDirectories = [ "${texmfroot}/texmf-dist/fonts" ]; };

        fmtutilCnf = assembleConfigLines fmtutilLines pkgList.sortedFormatPkgs;
        updmapCfg = assembleConfigLines updmapLines pkgList.sortedFontMaps;

        languageDat = assembleConfigLines langDatLines pkgList.sortedHyphenPatterns;
        languageDef = assembleConfigLines langDefLines pkgList.sortedHyphenPatterns;
        languageLua = assembleConfigLines langLuaLines pkgList.sortedHyphenPatterns;

        postactionScripts = builtins.catAttrs "postactionScript" pkgList.tlpkg;

        # whether to include doc, source containers
        withDocs = false;
        withSources = false;

        allowSubstitutes = true;
        preferLocalBuild = false;

        meta =
          meta
          // lib.optionalAttrs (!finalAttrs.__combine && __formatsOf == null) {
            inherit (pkgList) outputsToInstall;
          };
      };
    }
  )
)