1{
2 pkgs,
3 config,
4 lib,
5 ...
6}:
7let
8 inherit (builtins) isList elem;
9 inherit (lib)
10 getExe
11 literalExpression
12 maintainers
13 mapAttrs'
14 mkEnableOption
15 mkIf
16 mkOption
17 mkPackageOption
18 nameValuePair
19 optionalString
20 types
21 isBool
22 boolToString
23 ;
24 inherit (types) listOf package;
25
26 cfg = config.programs.bat;
27
28 settingsFormat = pkgs.formats.keyValue { listsAsDuplicateKeys = true; };
29 inherit (settingsFormat) generate type;
30
31 recursiveToString =
32 value:
33 if isList value then
34 map recursiveToString value
35 else if isBool value then
36 boolToString value
37 else
38 toString value;
39
40 initScript =
41 {
42 program,
43 shell,
44 flags ? [ ],
45 }:
46 if (shell != "fish") then
47 ''
48 eval "$(${getExe program} ${toString flags})"
49 ''
50 else
51 ''
52 ${getExe program} ${toString flags} | source
53 '';
54
55 shellInit =
56 shell:
57 optionalString (elem pkgs.bat-extras.batpipe cfg.extraPackages) (initScript {
58 program = pkgs.bat-extras.batpipe;
59 inherit shell;
60 })
61 + optionalString (elem pkgs.bat-extras.batman cfg.extraPackages) (initScript {
62 program = pkgs.bat-extras.batman;
63 inherit shell;
64 flags = [ "--export-env" ];
65 });
66in
67{
68 options.programs.bat = {
69 enable = mkEnableOption "`bat`, a {manpage}`cat(1)` clone with wings";
70
71 package = mkPackageOption pkgs "bat" { };
72
73 extraPackages = mkOption {
74 default = [ ];
75 example = literalExpression ''
76 with pkgs.bat-extras; [
77 batdiff
78 batman
79 prettybat
80 ];
81 '';
82 description = ''
83 Extra `bat` scripts to be added to the system configuration.
84 '';
85 type = listOf package;
86 };
87
88 settings = mkOption {
89 default = { };
90 example = {
91 theme = "TwoDark";
92 italic-text = "always";
93 paging = "never";
94 pager = "less --RAW-CONTROL-CHARS --quit-if-one-screen --mouse";
95 map-syntax = [
96 "*.ino:C++"
97 ".ignore:Git Ignore"
98 ];
99 };
100 description = ''
101 Parameters to be written to the system-wide `bat` configuration file.
102 '';
103 inherit type;
104 };
105 };
106
107 config = mkIf cfg.enable {
108 environment = {
109 systemPackages = [ cfg.package ] ++ cfg.extraPackages;
110 etc."bat/config".source = generate "bat-config" (
111 mapAttrs' (name: value: nameValuePair ("--" + name) (recursiveToString value)) cfg.settings
112 );
113 };
114
115 programs = {
116 bash = mkIf (!config.programs.fish.enable) {
117 interactiveShellInit = shellInit "bash";
118 };
119 fish = mkIf config.programs.fish.enable {
120 interactiveShellInit = shellInit "fish";
121 };
122 zsh = mkIf (!config.programs.fish.enable && config.programs.zsh.enable) {
123 interactiveShellInit = shellInit "zsh";
124 };
125 };
126 };
127 meta.maintainers = with maintainers; [ sigmasquadron ];
128}