1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.programs.zsh.syntaxHighlighting;
7in
8 {
9 options = {
10 programs.zsh.syntaxHighlighting = {
11 enable = mkEnableOption "zsh-syntax-highlighting";
12
13 highlighters = mkOption {
14 default = [ "main" ];
15
16 # https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
17 type = types.listOf(types.enum([
18 "main"
19 "brackets"
20 "pattern"
21 "cursor"
22 "root"
23 "line"
24 ]));
25
26 description = ''
27 Specifies the highlighters to be used by zsh-syntax-highlighting.
28
29 The following defined options can be found here:
30 https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
31 '';
32 };
33
34 patterns = mkOption {
35 default = {};
36 type = types.attrsOf types.string;
37
38 example = literalExample ''
39 {
40 "rm -rf *" = "fg=white,bold,bg=red";
41 }
42 '';
43
44 description = ''
45 Specifies custom patterns to be highlighted by zsh-syntax-highlighting.
46
47 Please refer to the docs for more information about the usage:
48 https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/pattern.md
49 '';
50 };
51 };
52 };
53
54 config = mkIf cfg.enable {
55 environment.systemPackages = with pkgs; [ zsh-syntax-highlighting ];
56
57 programs.zsh.interactiveShellInit = with pkgs; with builtins; ''
58 source ${zsh-syntax-highlighting}/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
59
60 ${optionalString (length(cfg.highlighters) > 0)
61 "ZSH_HIGHLIGHT_HIGHLIGHTERS=(${concatStringsSep " " cfg.highlighters})"
62 }
63
64 ${let
65 n = attrNames cfg.patterns;
66 in
67 optionalString (length(n) > 0)
68 (assert(elem "pattern" cfg.highlighters); (foldl (
69 a: b:
70 ''
71 ${a}
72 ZSH_HIGHLIGHT_PATTERNS+=('${b}' '${attrByPath [b] "" cfg.patterns}')
73 ''
74 ) "") n)
75 }
76 '';
77 };
78 }