1 # This module defines global configuration for the zshell.
3 { config, lib, options, pkgs, ... }:
7 cfge = config.environment;
9 cfg = config.programs.zsh;
10 opt = options.programs.zsh;
12 zshAliases = builtins.concatStringsSep "\n" (
13 lib.mapAttrsToList (k: v: "alias -- ${k}=${lib.escapeShellArg v}")
14 (lib.filterAttrs (k: v: v != null) cfg.shellAliases)
18 # Note that generated /etc/zprofile and /etc/zshrc files do a lot of
19 # non-standard setup to make zsh usable with no configuration by default.
21 # Which means that unless you explicitly meticulously override everything
22 # generated, interactions between your ~/.zshrc and these files are likely
23 # to be rather surprising.
25 # Note however, that you can disable loading of the generated /etc/zprofile
26 # and /etc/zshrc (you can't disable loading of /etc/zshenv, but it is
27 # designed to not set anything surprising) by setting `no_global_rcs` option
30 # echo setopt no_global_rcs >> ~/.zshenv
32 # See "STARTUP/SHUTDOWN FILES" section of zsh(1) for more info.
43 enable = lib.mkOption {
46 Whether to configure zsh as an interactive shell. To enable zsh for
47 a particular user, use the {option}`users.users.<name?>.shell`
48 option for that user. To enable zsh system-wide use the
49 {option}`users.defaultUserShell` option.
51 type = lib.types.bool;
54 shellAliases = lib.mkOption {
57 Set of aliases for zsh shell, which overrides {option}`environment.shellAliases`.
58 See {option}`environment.shellAliases` for an option format description.
60 type = with lib.types; attrsOf (nullOr (either str path));
63 shellInit = lib.mkOption {
66 Shell script code called during zsh shell initialisation.
68 type = lib.types.lines;
71 loginShellInit = lib.mkOption {
74 Shell script code called during zsh login shell initialisation.
76 type = lib.types.lines;
79 interactiveShellInit = lib.mkOption {
82 Shell script code called during interactive zsh shell initialisation.
84 type = lib.types.lines;
87 promptInit = lib.mkOption {
89 # Note that to manually override this in ~/.zshrc you should run `prompt off`
90 # before setting your PS1 and etc. Otherwise this will likely to interact with
91 # your ~/.zshrc configuration in unexpected ways as the default prompt sets
92 # a lot of different prompt variables.
93 autoload -U promptinit && promptinit && prompt suse && setopt prompt_sp
96 Shell script code used to initialise the zsh prompt.
98 type = lib.types.lines;
101 histSize = lib.mkOption {
106 type = lib.types.int;
109 histFile = lib.mkOption {
110 default = "$HOME/.zsh_history";
114 type = lib.types.str;
117 setOptions = lib.mkOption {
118 type = lib.types.listOf lib.types.str;
124 example = [ "EXTENDED_HISTORY" "RM_STAR_WAIT" ];
126 Configure zsh options. See
127 {manpage}`zshoptions(1)`.
131 enableCompletion = lib.mkOption {
134 Enable zsh completion for all interactive zsh shells.
136 type = lib.types.bool;
139 enableBashCompletion = lib.mkOption {
142 Enable compatibility with bash's programmable completion system.
144 type = lib.types.bool;
147 enableGlobalCompInit = lib.mkOption {
148 default = cfg.enableCompletion;
149 defaultText = lib.literalExpression "config.${opt.enableCompletion}";
151 Enable execution of compinit call for all interactive zsh shells.
153 This option can be disabled if the user wants to extend its
154 `fpath` and a custom `compinit`
155 call in the local config is required.
157 type = lib.types.bool;
160 enableLsColors = lib.mkOption {
163 Enable extra colors in directory listings (used by `ls` and `tree`).
165 type = lib.types.bool;
172 config = lib.mkIf cfg.enable {
174 programs.zsh.shellAliases = builtins.mapAttrs (name: lib.mkDefault) cfge.shellAliases;
176 environment.etc.zshenv.text =
178 # /etc/zshenv: DO NOT EDIT -- this file has been generated automatically.
179 # This file is read for all shells.
181 # Only execute this file once per shell.
182 if [ -n "''${__ETC_ZSHENV_SOURCED-}" ]; then return; fi
183 __ETC_ZSHENV_SOURCED=1
185 if [ -z "''${__NIXOS_SET_ENVIRONMENT_DONE-}" ]; then
186 . ${config.system.build.setEnvironment}
189 HELPDIR="${pkgs.zsh}/share/zsh/$ZSH_VERSION/help"
191 # Tell zsh how to find installed completions.
192 for p in ''${(z)NIX_PROFILES}; do
193 fpath=($p/share/zsh/site-functions $p/share/zsh/$ZSH_VERSION/functions $p/share/zsh/vendor-completions $fpath)
196 # Setup custom shell init stuff.
201 # Read system-wide modifications.
202 if test -f /etc/zshenv.local; then
207 environment.etc.zprofile.text =
209 # /etc/zprofile: DO NOT EDIT -- this file has been generated automatically.
210 # This file is read for login shells.
214 # Only execute this file once per shell.
215 if [ -n "''${__ETC_ZPROFILE_SOURCED-}" ]; then return; fi
216 __ETC_ZPROFILE_SOURCED=1
218 # Setup custom login shell init stuff.
219 ${cfge.loginShellInit}
221 ${cfg.loginShellInit}
223 # Read system-wide modifications.
224 if test -f /etc/zprofile.local; then
225 . /etc/zprofile.local
229 environment.etc.zshrc.text =
231 # /etc/zshrc: DO NOT EDIT -- this file has been generated automatically.
232 # This file is read for interactive shells.
236 # Only execute this file once per shell.
237 if [ -n "$__ETC_ZSHRC_SOURCED" -o -n "$NOSYSZSHRC" ]; then return; fi
238 __ETC_ZSHRC_SOURCED=1
240 ${lib.optionalString (cfg.setOptions != []) ''
242 setopt ${builtins.concatStringsSep " " cfg.setOptions}
245 # Alternative method of determining short and full hostname.
246 HOST=${config.networking.fqdnOrHostName}
248 # Setup command line history.
249 # Don't export these, otherwise other shells (bash) will try to use same HISTFILE.
250 SAVEHIST=${builtins.toString cfg.histSize}
251 HISTSIZE=${builtins.toString cfg.histSize}
252 HISTFILE=${cfg.histFile}
254 # Configure sane keyboard defaults.
257 ${lib.optionalString cfg.enableGlobalCompInit ''
258 # Enable autocompletion.
259 autoload -U compinit && compinit
262 ${lib.optionalString cfg.enableBashCompletion ''
263 # Enable compatibility with bash's completion system.
264 autoload -U bashcompinit && bashcompinit
267 # Setup custom interactive shell init stuff.
268 ${cfge.interactiveShellInit}
270 ${cfg.interactiveShellInit}
272 ${lib.optionalString cfg.enableLsColors ''
273 # Extra colors for directory listings.
274 eval "$(${pkgs.coreutils}/bin/dircolors -b)"
283 # Disable some features to support TRAMP.
284 if [ "$TERM" = dumb ]; then
285 unsetopt zle prompt_cr prompt_subst
291 # Read system-wide modifications.
292 if test -f /etc/zshrc.local; then
298 # If we use `.source` here the path is garbage collected also we point to it with a symlink
299 # see https://github.com/NixOS/nixpkgs/issues/132732
300 environment.etc.zinputrc.text = builtins.readFile ./zinputrc;
302 environment.systemPackages = [ pkgs.zsh ]
303 ++ lib.optional cfg.enableCompletion pkgs.nix-zsh-completions;
305 environment.pathsToLink = lib.optional cfg.enableCompletion "/share/zsh";
307 #users.defaultUserShell = lib.mkDefault "/run/current-system/sw/bin/zsh";
311 "/run/current-system/sw/bin/zsh"
312 "${pkgs.zsh}/bin/zsh"