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