1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8let
9 cfg = config.programs.zsh.syntaxHighlighting;
10in
11{
12 imports = [
13 (lib.mkRenamedOptionModule
14 [ "programs" "zsh" "enableSyntaxHighlighting" ]
15 [ "programs" "zsh" "syntaxHighlighting" "enable" ]
16 )
17 (lib.mkRenamedOptionModule
18 [ "programs" "zsh" "syntax-highlighting" "enable" ]
19 [ "programs" "zsh" "syntaxHighlighting" "enable" ]
20 )
21 (lib.mkRenamedOptionModule
22 [ "programs" "zsh" "syntax-highlighting" "highlighters" ]
23 [ "programs" "zsh" "syntaxHighlighting" "highlighters" ]
24 )
25 (lib.mkRenamedOptionModule
26 [ "programs" "zsh" "syntax-highlighting" "patterns" ]
27 [ "programs" "zsh" "syntaxHighlighting" "patterns" ]
28 )
29 ];
30
31 options = {
32 programs.zsh.syntaxHighlighting = {
33 enable = lib.mkEnableOption "zsh-syntax-highlighting";
34
35 highlighters = lib.mkOption {
36 default = [ "main" ];
37
38 # https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
39 type = lib.types.listOf (
40 lib.types.enum ([
41 "main"
42 "brackets"
43 "pattern"
44 "cursor"
45 "regexp"
46 "root"
47 "line"
48 ])
49 );
50
51 description = ''
52 Specifies the highlighters to be used by zsh-syntax-highlighting.
53
54 The following defined options can be found here:
55 https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
56 '';
57 };
58
59 patterns = lib.mkOption {
60 default = { };
61 type = lib.types.attrsOf lib.types.str;
62
63 example = lib.literalExpression ''
64 {
65 "rm -rf *" = "fg=white,bold,bg=red";
66 }
67 '';
68
69 description = ''
70 Specifies custom patterns to be highlighted by zsh-syntax-highlighting.
71
72 Please refer to the docs for more information about the usage:
73 https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/pattern.md
74 '';
75 };
76 styles = lib.mkOption {
77 default = { };
78 type = lib.types.attrsOf lib.types.str;
79
80 example = lib.literalExpression ''
81 {
82 "alias" = "fg=magenta,bold";
83 }
84 '';
85
86 description = ''
87 Specifies custom styles to be highlighted by zsh-syntax-highlighting.
88
89 Please refer to the docs for more information about the usage:
90 https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/main.md
91 '';
92 };
93 };
94 };
95
96 config = lib.mkIf cfg.enable {
97 environment.systemPackages = [ pkgs.zsh-syntax-highlighting ];
98
99 assertions = [
100 {
101 assertion =
102 builtins.length (builtins.attrNames cfg.patterns) > 0 -> builtins.elem "pattern" cfg.highlighters;
103 message = ''
104 When highlighting patterns, "pattern" needs to be included in the list of highlighters.
105 '';
106 }
107 ];
108
109 programs.zsh.interactiveShellInit = lib.mkAfter (
110 lib.concatStringsSep "\n" (
111 [
112 "source ${pkgs.zsh-syntax-highlighting}/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
113 ]
114 ++ lib.optional (
115 builtins.length (cfg.highlighters) > 0
116 ) "ZSH_HIGHLIGHT_HIGHLIGHTERS=(${builtins.concatStringsSep " " cfg.highlighters})"
117 ++ lib.optionals (builtins.length (builtins.attrNames cfg.patterns) > 0) (
118 lib.mapAttrsToList (
119 pattern: design: "ZSH_HIGHLIGHT_PATTERNS+=('${pattern}' '${design}')"
120 ) cfg.patterns
121 )
122 ++ lib.optionals (builtins.length (builtins.attrNames cfg.styles) > 0) (
123 lib.mapAttrsToList (styles: design: "ZSH_HIGHLIGHT_STYLES[${styles}]='${design}'") cfg.styles
124 )
125 )
126 );
127 };
128}