fork/home-manager

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

Bernardo Meurerbat: allow setting themes/syntaxes without IFD44635279

master
4.5 KiB161 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.bat;
8
9  package = pkgs.bat;
10
11  toConfigFile = generators.toKeyValue {
12    mkKeyValue = k: v: "--${k}=${lib.escapeShellArg v}";
13    listsAsDuplicateKeys = true;
14  };
15
16in {
17  meta.maintainers = [ ];
18
19  options.programs.bat = {
20    enable = mkEnableOption "bat, a cat clone with wings";
21
22    config = mkOption {
23      type = with types; attrsOf (either str (listOf str));
24      default = { };
25      example = {
26        theme = "TwoDark";
27        pager = "less -FR";
28        map-syntax = [ "*.jenkinsfile:Groovy" "*.props:Java Properties" ];
29      };
30      description = ''
31        Bat configuration.
32      '';
33    };
34
35    extraPackages = mkOption {
36      type = types.listOf types.package;
37      default = [ ];
38      example = literalExpression
39        "with pkgs.bat-extras; [ batdiff batman batgrep batwatch ];";
40      description = ''
41        Additional bat packages to install.
42      '';
43    };
44
45    themes = mkOption {
46      type = types.attrsOf (types.either types.lines (types.submodule {
47        options = {
48          src = mkOption {
49            type = types.path;
50            description = "Path to the theme folder.";
51          };
52
53          file = mkOption {
54            type = types.nullOr types.str;
55            default = null;
56            description =
57              "Subpath of the theme file within the source, if needed.";
58          };
59        };
60      }));
61      default = { };
62      example = literalExpression ''
63        {
64          dracula = {
65            src = pkgs.fetchFromGitHub {
66              owner = "dracula";
67              repo = "sublime"; # Bat uses sublime syntax for its themes
68              rev = "26c57ec282abcaa76e57e055f38432bd827ac34e";
69              sha256 = "019hfl4zbn4vm4154hh3bwk6hm7bdxbr1hdww83nabxwjn99ndhv";
70            };
71            file = "Dracula.tmTheme";
72          };
73        }
74      '';
75      description = ''
76        Additional themes to provide.
77      '';
78    };
79
80    syntaxes = mkOption {
81      type = types.attrsOf (types.either types.lines (types.submodule {
82        options = {
83          src = mkOption {
84            type = types.path;
85            description = "Path to the syntax folder.";
86          };
87          file = mkOption {
88            type = types.nullOr types.str;
89            default = null;
90            description =
91              "Subpath of the syntax file within the source, if needed.";
92          };
93        };
94      }));
95      default = { };
96      example = literalExpression ''
97        {
98          gleam = {
99            src = pkgs.fetchFromGitHub {
100              owner = "molnarmark";
101              repo = "sublime-gleam";
102              rev = "2e761cdb1a87539d827987f997a20a35efd68aa9";
103              hash = "sha256-Zj2DKTcO1t9g18qsNKtpHKElbRSc9nBRE2QBzRn9+qs=";
104            };
105            file = "syntax/gleam.sublime-syntax";
106          };
107        }
108      '';
109      description = ''
110        Additional syntaxes to provide.
111      '';
112    };
113  };
114
115  config = mkIf cfg.enable (mkMerge [
116    (mkIf (any isString (attrValues cfg.themes)) {
117      warnings = [''
118        Using programs.bat.themes as a string option is deprecated and will be
119        removed in the future. Please change to using it as an attribute set
120        instead.
121      ''];
122    })
123    (mkIf (any isString (attrValues cfg.syntaxes)) {
124      warnings = [''
125        Using programs.bat.syntaxes as a string option is deprecated and will be
126        removed in the future. Please change to using it as an attribute set
127        instead.
128      ''];
129    })
130    {
131      home.packages = [ package ] ++ cfg.extraPackages;
132
133      xdg.configFile = mkMerge ([({
134        "bat/config" =
135          mkIf (cfg.config != { }) { text = toConfigFile cfg.config; };
136      })] ++ (flip mapAttrsToList cfg.themes (name: val: {
137        "bat/themes/${name}.tmTheme" = if isString val then {
138          text = val;
139        } else {
140          source =
141            if isNull val.file then "${val.src}" else "${val.src}/${val.file}";
142        };
143      })) ++ (flip mapAttrsToList cfg.syntaxes (name: val: {
144        "bat/syntaxes/${name}.sublime-syntax" = if isString val then {
145          text = val;
146        } else {
147          source =
148            if isNull val.file then "${val.src}" else "${val.src}/${val.file}";
149        };
150      })));
151
152      home.activation.batCache = hm.dag.entryAfter [ "linkGeneration" ] ''
153        (
154          export XDG_CACHE_HOME=${escapeShellArg config.xdg.cacheHome}
155          $VERBOSE_ECHO "Rebuilding bat theme cache"
156          $DRY_RUN_CMD ${lib.getExe package} cache --build
157        )
158      '';
159    }
160  ]);
161}