From 6bb027dbeb3dedb6c65cb27274465105bccdfb18 Mon Sep 17 00:00:00 2001 From: Quentin Aristote Date: Fri, 3 Dec 2021 11:48:19 +0100 Subject: initial commit --- modules/latex.nix | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 modules/latex.nix (limited to 'modules/latex.nix') diff --git a/modules/latex.nix b/modules/latex.nix new file mode 100644 index 0000000..0c06ec7 --- /dev/null +++ b/modules/latex.nix @@ -0,0 +1,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 ]; + }; + }; +} -- cgit v1.2.3