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