fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Kirill Elaginpandoc: add new modulec47c350f

master
2.7 KiB104 linesraw
1{ config, lib, pkgs, ... }:
2
3let
4
5  cfg = config.programs.pandoc;
6
7  inherit (lib) literalExpression mkEnableOption mkIf mkOption types;
8
9  jsonFormat = pkgs.formats.json { };
10
11  makeTemplateFile = name: file:
12    lib.nameValuePair "pandoc/templates/${name}" { source = file; };
13
14  getFileName = file:
15    # This is actually safe here, since it is just a file name
16    builtins.unsafeDiscardStringContext (baseNameOf file);
17
18  makeCslFile = file:
19    lib.nameValuePair "pandoc/csl/${getFileName file}" { source = file; };
20
21in {
22  meta.maintainers = [ lib.maintainers.kirelagin ];
23
24  options.programs.pandoc = {
25    enable = mkEnableOption "pandoc";
26
27    package = mkOption {
28      type = types.package;
29      default = pkgs.pandoc;
30      defaultText = literalExpression "pkgs.pandoc";
31      description = "The pandoc package to use.";
32    };
33
34    # We wrap the executable to pass some arguments
35    finalPackage = mkOption {
36      type = types.package;
37      readOnly = true;
38      description = "Resulting package.";
39    };
40
41    defaults = mkOption {
42      type = jsonFormat.type;
43      default = { };
44      example = literalExpression ''
45        {
46          metadata = {
47            author = "John Doe";
48          };
49          pdf-engine = "xelatex";
50          citeproc = true;
51        }
52      '';
53      description = ''
54        Options to set by default.
55        These will be converted to JSON and written to a defaults
56        file (see Default files in pandoc documentation).
57      '';
58    };
59
60    defaultsFile = mkOption {
61      type = types.path;
62      readOnly = true;
63      description = "Resulting defaults file.";
64    };
65
66    templates = mkOption {
67      type = types.attrsOf types.path;
68      default = { };
69      example = literalExpression ''
70        {
71          "default.latex" = path/to/your/template;
72        }
73      '';
74      description = "Custom templates.";
75    };
76
77    citationStyles = mkOption {
78      type = types.listOf types.path;
79      default = [ ];
80      example = literalExpression "[ path/to/file.csl ]";
81      description = "List of .csl files to install.";
82    };
83  };
84
85  config = mkIf cfg.enable {
86    programs.pandoc = {
87      defaultsFile = jsonFormat.generate "hm.json" cfg.defaults;
88
89      finalPackage = pkgs.symlinkJoin {
90        name = "pandoc-with-defaults";
91        paths = [ cfg.package ];
92        nativeBuildInputs = [ pkgs.makeWrapper ];
93        postBuild = ''
94          wrapProgram "$out/bin/pandoc" \
95            --add-flags '--defaults "${cfg.defaultsFile}"'
96        '';
97      };
98    };
99
100    home.packages = [ cfg.finalPackage ];
101    xdg.dataFile = lib.mapAttrs' makeTemplateFile cfg.templates
102      // lib.listToAttrs (map makeCslFile cfg.citationStyles);
103  };
104}