python3Packages.orjson: Disable failing tests on 32 bit
[NixPkgs.git] / nixos / modules / programs / zsh / zsh.nix
blob29790e0dd759bbfb76621018f314055011fb5f84
1 # This module defines global configuration for the zshell.
3 { config, lib, options, pkgs, ... }:
5 with lib;
7 let
9   cfge = config.environment;
11   cfg = config.programs.zsh;
12   opt = options.programs.zsh;
14   zshAliases = concatStringsSep "\n" (
15     mapAttrsFlatten (k: v: "alias ${k}=${escapeShellArg v}")
16       (filterAttrs (k: v: v != null) cfg.shellAliases)
17   );
19   zshStartupNotes = ''
20     # Note that generated /etc/zprofile and /etc/zshrc files do a lot of
21     # non-standard setup to make zsh usable with no configuration by default.
22     #
23     # Which means that unless you explicitly meticulously override everything
24     # generated, interactions between your ~/.zshrc and these files are likely
25     # to be rather surprising.
26     #
27     # Note however, that you can disable loading of the generated /etc/zprofile
28     # and /etc/zshrc (you can't disable loading of /etc/zshenv, but it is
29     # designed to not set anything surprising) by setting `no_global_rcs` option
30     # in ~/.zshenv:
31     #
32     #   echo setopt no_global_rcs >> ~/.zshenv
33     #
34     # See "STARTUP/SHUTDOWN FILES" section of zsh(1) for more info.
35   '';
41   options = {
43     programs.zsh = {
45       enable = mkOption {
46         default = false;
47         description = lib.mdDoc ''
48           Whether to configure zsh as an interactive shell. To enable zsh for
49           a particular user, use the {option}`users.users.<name?>.shell`
50           option for that user. To enable zsh system-wide use the
51           {option}`users.defaultUserShell` option.
52         '';
53         type = types.bool;
54       };
56       shellAliases = mkOption {
57         default = { };
58         description = lib.mdDoc ''
59           Set of aliases for zsh shell, which overrides {option}`environment.shellAliases`.
60           See {option}`environment.shellAliases` for an option format description.
61         '';
62         type = with types; attrsOf (nullOr (either str path));
63       };
65       shellInit = mkOption {
66         default = "";
67         description = lib.mdDoc ''
68           Shell script code called during zsh shell initialisation.
69         '';
70         type = types.lines;
71       };
73       loginShellInit = mkOption {
74         default = "";
75         description = lib.mdDoc ''
76           Shell script code called during zsh login shell initialisation.
77         '';
78         type = types.lines;
79       };
81       interactiveShellInit = mkOption {
82         default = "";
83         description = lib.mdDoc ''
84           Shell script code called during interactive zsh shell initialisation.
85         '';
86         type = types.lines;
87       };
89       promptInit = mkOption {
90         default = ''
91           # Note that to manually override this in ~/.zshrc you should run `prompt off`
92           # before setting your PS1 and etc. Otherwise this will likely to interact with
93           # your ~/.zshrc configuration in unexpected ways as the default prompt sets
94           # a lot of different prompt variables.
95           autoload -U promptinit && promptinit && prompt suse && setopt prompt_sp
96         '';
97         description = lib.mdDoc ''
98           Shell script code used to initialise the zsh prompt.
99         '';
100         type = types.lines;
101       };
103       histSize = mkOption {
104         default = 2000;
105         description = lib.mdDoc ''
106           Change history size.
107         '';
108         type = types.int;
109       };
111       histFile = mkOption {
112         default = "$HOME/.zsh_history";
113         description = lib.mdDoc ''
114           Change history file.
115         '';
116         type = types.str;
117       };
119       setOptions = mkOption {
120         type = types.listOf types.str;
121         default = [
122           "HIST_IGNORE_DUPS"
123           "SHARE_HISTORY"
124           "HIST_FCNTL_LOCK"
125         ];
126         example = [ "EXTENDED_HISTORY" "RM_STAR_WAIT" ];
127         description = lib.mdDoc ''
128           Configure zsh options. See
129           {manpage}`zshoptions(1)`.
130         '';
131       };
133       enableCompletion = mkOption {
134         default = true;
135         description = lib.mdDoc ''
136           Enable zsh completion for all interactive zsh shells.
137         '';
138         type = types.bool;
139       };
141       enableBashCompletion = mkOption {
142         default = false;
143         description = lib.mdDoc ''
144           Enable compatibility with bash's programmable completion system.
145         '';
146         type = types.bool;
147       };
149       enableGlobalCompInit = mkOption {
150         default = cfg.enableCompletion;
151         defaultText = literalExpression "config.${opt.enableCompletion}";
152         description = lib.mdDoc ''
153           Enable execution of compinit call for all interactive zsh shells.
155           This option can be disabled if the user wants to extend its
156           `fpath` and a custom `compinit`
157           call in the local config is required.
158         '';
159         type = types.bool;
160       };
162     };
164   };
166   config = mkIf cfg.enable {
168     programs.zsh.shellAliases = mapAttrs (name: mkDefault) cfge.shellAliases;
170     environment.etc.zshenv.text =
171       ''
172         # /etc/zshenv: DO NOT EDIT -- this file has been generated automatically.
173         # This file is read for all shells.
175         # Only execute this file once per shell.
176         if [ -n "$__ETC_ZSHENV_SOURCED" ]; then return; fi
177         __ETC_ZSHENV_SOURCED=1
179         if [ -z "$__NIXOS_SET_ENVIRONMENT_DONE" ]; then
180             . ${config.system.build.setEnvironment}
181         fi
183         HELPDIR="${pkgs.zsh}/share/zsh/$ZSH_VERSION/help"
185         # Tell zsh how to find installed completions.
186         for p in ''${(z)NIX_PROFILES}; do
187             fpath=($p/share/zsh/site-functions $p/share/zsh/$ZSH_VERSION/functions $p/share/zsh/vendor-completions $fpath)
188         done
190         # Setup custom shell init stuff.
191         ${cfge.shellInit}
193         ${cfg.shellInit}
195         # Read system-wide modifications.
196         if test -f /etc/zshenv.local; then
197             . /etc/zshenv.local
198         fi
199       '';
201     environment.etc.zprofile.text =
202       ''
203         # /etc/zprofile: DO NOT EDIT -- this file has been generated automatically.
204         # This file is read for login shells.
205         #
206         ${zshStartupNotes}
208         # Only execute this file once per shell.
209         if [ -n "$__ETC_ZPROFILE_SOURCED" ]; then return; fi
210         __ETC_ZPROFILE_SOURCED=1
212         # Setup custom login shell init stuff.
213         ${cfge.loginShellInit}
215         ${cfg.loginShellInit}
217         # Read system-wide modifications.
218         if test -f /etc/zprofile.local; then
219             . /etc/zprofile.local
220         fi
221       '';
223     environment.etc.zshrc.text =
224       ''
225         # /etc/zshrc: DO NOT EDIT -- this file has been generated automatically.
226         # This file is read for interactive shells.
227         #
228         ${zshStartupNotes}
230         # Only execute this file once per shell.
231         if [ -n "$__ETC_ZSHRC_SOURCED" -o -n "$NOSYSZSHRC" ]; then return; fi
232         __ETC_ZSHRC_SOURCED=1
234         ${optionalString (cfg.setOptions != []) ''
235           # Set zsh options.
236           setopt ${concatStringsSep " " cfg.setOptions}
237         ''}
239         # Setup command line history.
240         # Don't export these, otherwise other shells (bash) will try to use same HISTFILE.
241         SAVEHIST=${toString cfg.histSize}
242         HISTSIZE=${toString cfg.histSize}
243         HISTFILE=${cfg.histFile}
245         # Configure sane keyboard defaults.
246         . /etc/zinputrc
248         ${optionalString cfg.enableGlobalCompInit ''
249           # Enable autocompletion.
250           autoload -U compinit && compinit
251         ''}
253         ${optionalString cfg.enableBashCompletion ''
254           # Enable compatibility with bash's completion system.
255           autoload -U bashcompinit && bashcompinit
256         ''}
258         # Setup custom interactive shell init stuff.
259         ${cfge.interactiveShellInit}
261         ${cfg.interactiveShellInit}
263         # Setup aliases.
264         ${zshAliases}
266         # Setup prompt.
267         ${cfg.promptInit}
269         # Disable some features to support TRAMP.
270         if [ "$TERM" = dumb ]; then
271             unsetopt zle prompt_cr prompt_subst
272             unset RPS1 RPROMPT
273             PS1='$ '
274             PROMPT='$ '
275         fi
277         # Read system-wide modifications.
278         if test -f /etc/zshrc.local; then
279             . /etc/zshrc.local
280         fi
281       '';
283     # Bug in nix flakes:
284     # If we use `.source` here the path is garbage collected also we point to it with a symlink
285     # see https://github.com/NixOS/nixpkgs/issues/132732
286     environment.etc.zinputrc.text = builtins.readFile ./zinputrc;
288     environment.systemPackages = [ pkgs.zsh ]
289       ++ optional cfg.enableCompletion pkgs.nix-zsh-completions;
291     environment.pathsToLink = optional cfg.enableCompletion "/share/zsh";
293     #users.defaultUserShell = mkDefault "/run/current-system/sw/bin/zsh";
295     environment.shells =
296       [
297         "/run/current-system/sw/bin/zsh"
298         "${pkgs.zsh}/bin/zsh"
299       ];
301   };