summaryrefslogtreecommitdiff
path: root/modules/latex.nix
blob: 16f001709f232f1e066ceaec6b9b17da03c645fa (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
{ config, lib, pkgs, ... }:

with lib;
let
  cfg = config.latex;
  latexmkFlags = concatStringsSep " " ([
    (if cfg.latexmk.output.dvi.enable then "-dvi" else "-dvi-")
    (if cfg.latexmk.output.ps.enable then "-ps" else "-ps-")
    (if cfg.latexmk.output.pdf.enable then
      ("-pdf" + cfg.latexmk.output.pdf.method)
    else
      "-pdf-")
    (optionalString cfg.minted.enable "-shell-escape")
  ] ++ (map (filename: "-r ${filename}") cfg.latexmk.rc)
    ++ cfg.latexmk.extraFlags);
  latexBuildInput = cfg.texlive.combine ((cfg.packages cfg.texlive)
    // (optionalAttrs cfg.latexmk.enable { inherit (cfg.texlive) latexmk; })
    // (optionalAttrs cfg.minted.enable { inherit (cfg.texlive) minted; }));
in {
  options.latex = {
    enable = mkEnableOption { name = "LaTex"; };

    texlive = mkOption {
      type = types.attrs;
      default = pkgs.texlive;
      defaultText = literalExample "pkgs.texlive";
      description = ''
        The package providing LaTex.
        Use this option to set the version of LaTex.
      '';
    };

    packages = mkOption {
      type = with types; functionTo attrs;
      default = tl: { inherit (tl) scheme-full; };
      defaultText = literalExample "tl: { inherit (tl) scheme-full; }";
      description = ''
        Collection of packages that will be made available to the environment.
      '';
      example = literalExample ''
        tl: {
          inherit (tl) scheme-full calligra;
          my-package = { pkgs = [ (pkgs.callPackage ./my-package.nix {}) ]; };
        }
      '';
    };

    latexmk = {
      enable = mkEnableOption "latexmk";
      output = {
        dvi.enable = mkEnableOption "dvi output";
        pdf = {
          enable = mkEnableOption "pdf output";
          method = mkOption {
            type = types.enum [ "" "dvi" "lua" "ps" "xe" ];
            default = "lua";
            description = ''
              Method by which to generate the pdf.
            '';
          };
        };
        ps.enable = mkEnableOption "ps output";
      };
      rc = mkOption {
        type = types.listOf types.path;
        default = [ ];
        description = ''
          List of LaTeXmk rc files to load.
        '';
        example = literalExample "[ ~/.config/latexmkrc ]";
      };
      extraFlags = mkOption {
        # TODO vulnerable to shell injection
        type = types.listOf types.str;
        default = [ ];
        description = ''
          Flags to pass to latexmk.
        '';
        example = [ "-shell-escape" "-M" ];
      };
    };

    minted.enable = mkEnableOption "minted";
  };

  config = mkIf cfg.enable {
    buildInputs = [ latexBuildInput ] ++ (optional cfg.latexmk.enable pkgs.ps);
    aliases.latexmk = mkIf cfg.latexmk.enable
      "${latexBuildInput}/bin/latexmk ${latexmkFlags} \\$@";
    python = mkIf cfg.minted.enable {
      enable = true;
      packages = ps: with ps; [ pygments ];
    };
  };
}