1 # This module defines global configuration for the zshell.
3 { config, lib, options, pkgs, ... }:
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)
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.
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.
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
32 # echo setopt no_global_rcs >> ~/.zshenv
34 # See "STARTUP/SHUTDOWN FILES" section of zsh(1) for more info.
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.
56 shellAliases = mkOption {
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.
62 type = with types; attrsOf (nullOr (either str path));
65 shellInit = mkOption {
67 description = lib.mdDoc ''
68 Shell script code called during zsh shell initialisation.
73 loginShellInit = mkOption {
75 description = lib.mdDoc ''
76 Shell script code called during zsh login shell initialisation.
81 interactiveShellInit = mkOption {
83 description = lib.mdDoc ''
84 Shell script code called during interactive zsh shell initialisation.
89 promptInit = mkOption {
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
97 description = lib.mdDoc ''
98 Shell script code used to initialise the zsh prompt.
103 histSize = mkOption {
105 description = lib.mdDoc ''
111 histFile = mkOption {
112 default = "$HOME/.zsh_history";
113 description = lib.mdDoc ''
119 setOptions = mkOption {
120 type = types.listOf types.str;
126 example = [ "EXTENDED_HISTORY" "RM_STAR_WAIT" ];
127 description = lib.mdDoc ''
128 Configure zsh options. See
129 {manpage}`zshoptions(1)`.
133 enableCompletion = mkOption {
135 description = lib.mdDoc ''
136 Enable zsh completion for all interactive zsh shells.
141 enableBashCompletion = mkOption {
143 description = lib.mdDoc ''
144 Enable compatibility with bash's programmable completion system.
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.
166 config = mkIf cfg.enable {
168 programs.zsh.shellAliases = mapAttrs (name: mkDefault) cfge.shellAliases;
170 environment.etc.zshenv.text =
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}
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)
190 # Setup custom shell init stuff.
195 # Read system-wide modifications.
196 if test -f /etc/zshenv.local; then
201 environment.etc.zprofile.text =
203 # /etc/zprofile: DO NOT EDIT -- this file has been generated automatically.
204 # This file is read for login shells.
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
223 environment.etc.zshrc.text =
225 # /etc/zshrc: DO NOT EDIT -- this file has been generated automatically.
226 # This file is read for interactive shells.
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 != []) ''
236 setopt ${concatStringsSep " " cfg.setOptions}
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.
248 ${optionalString cfg.enableGlobalCompInit ''
249 # Enable autocompletion.
250 autoload -U compinit && compinit
253 ${optionalString cfg.enableBashCompletion ''
254 # Enable compatibility with bash's completion system.
255 autoload -U bashcompinit && bashcompinit
258 # Setup custom interactive shell init stuff.
259 ${cfge.interactiveShellInit}
261 ${cfg.interactiveShellInit}
269 # Disable some features to support TRAMP.
270 if [ "$TERM" = dumb ]; then
271 unsetopt zle prompt_cr prompt_subst
277 # Read system-wide modifications.
278 if test -f /etc/zshrc.local; then
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";
297 "/run/current-system/sw/bin/zsh"
298 "${pkgs.zsh}/bin/zsh"