python3Packages.orjson: Disable failing tests on 32 bit
[NixPkgs.git] / nixos / modules / programs / zsh / zsh-syntax-highlighting.nix
blobbc1101349283e3dddd8ef973fecfc9507e242cc6
1 { config, lib, pkgs, ... }:
3 with lib;
5 let
6   cfg = config.programs.zsh.syntaxHighlighting;
7 in
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   ];
16   options = {
17     programs.zsh.syntaxHighlighting = {
18       enable = mkEnableOption (lib.mdDoc "zsh-syntax-highlighting");
20       highlighters = mkOption {
21         default = [ "main" ];
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           "root"
30           "line"
31         ]));
33         description = lib.mdDoc ''
34           Specifies the highlighters to be used by zsh-syntax-highlighting.
36           The following defined options can be found here:
37           https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
38         '';
39       };
41       patterns = mkOption {
42         default = {};
43         type = types.attrsOf types.str;
45         example = literalExpression ''
46           {
47             "rm -rf *" = "fg=white,bold,bg=red";
48           }
49         '';
51         description = lib.mdDoc ''
52           Specifies custom patterns to be highlighted by zsh-syntax-highlighting.
54           Please refer to the docs for more information about the usage:
55           https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/pattern.md
56         '';
57       };
58       styles = mkOption {
59         default = {};
60         type = types.attrsOf types.str;
62         example = literalExpression ''
63           {
64             "alias" = "fg=magenta,bold";
65           }
66         '';
68         description = lib.mdDoc ''
69           Specifies custom styles to be highlighted by zsh-syntax-highlighting.
71           Please refer to the docs for more information about the usage:
72           https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/main.md
73         '';
74       };
75     };
76   };
78   config = mkIf cfg.enable {
79     environment.systemPackages = with pkgs; [ zsh-syntax-highlighting ];
81     assertions = [
82       {
83         assertion = length(attrNames cfg.patterns) > 0 -> elem "pattern" cfg.highlighters;
84         message = ''
85           When highlighting patterns, "pattern" needs to be included in the list of highlighters.
86         '';
87       }
88     ];
90     programs.zsh.interactiveShellInit = with pkgs;
91       lib.mkAfter (lib.concatStringsSep "\n" ([
92         "source ${zsh-syntax-highlighting}/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
93       ] ++ optional (length(cfg.highlighters) > 0)
94         "ZSH_HIGHLIGHT_HIGHLIGHTERS=(${concatStringsSep " " cfg.highlighters})"
95         ++ optionals (length(attrNames cfg.patterns) > 0)
96           (mapAttrsToList (
97             pattern: design:
98             "ZSH_HIGHLIGHT_PATTERNS+=('${pattern}' '${design}')"
99           ) cfg.patterns)
100         ++ optionals (length(attrNames cfg.styles) > 0)
101           (mapAttrsToList (
102             styles: design:
103             "ZSH_HIGHLIGHT_STYLES[${styles}]='${design}'"
104           ) cfg.styles)
105       ));
106   };