at 23.11-pre 9.0 kB view raw
1# This module defines global configuration for the zshell. 2 3{ config, lib, options, pkgs, ... }: 4 5with lib; 6 7let 8 9 cfge = config.environment; 10 11 cfg = config.programs.zsh; 12 opt = options.programs.zsh; 13 14 zshAliases = concatStringsSep "\n" ( 15 mapAttrsFlatten (k: v: "alias -- ${k}=${escapeShellArg v}") 16 (filterAttrs (k: v: v != null) cfg.shellAliases) 17 ); 18 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 ''; 36 37in 38 39{ 40 41 options = { 42 43 programs.zsh = { 44 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 }; 55 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 }; 64 65 shellInit = mkOption { 66 default = ""; 67 description = lib.mdDoc '' 68 Shell script code called during zsh shell initialisation. 69 ''; 70 type = types.lines; 71 }; 72 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 }; 80 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 }; 88 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 }; 102 103 histSize = mkOption { 104 default = 2000; 105 description = lib.mdDoc '' 106 Change history size. 107 ''; 108 type = types.int; 109 }; 110 111 histFile = mkOption { 112 default = "$HOME/.zsh_history"; 113 description = lib.mdDoc '' 114 Change history file. 115 ''; 116 type = types.str; 117 }; 118 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 }; 132 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 }; 140 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 }; 148 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. 154 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 }; 161 162 }; 163 164 }; 165 166 config = mkIf cfg.enable { 167 168 programs.zsh.shellAliases = mapAttrs (name: mkDefault) cfge.shellAliases; 169 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. 174 175 # Only execute this file once per shell. 176 if [ -n "''${__ETC_ZSHENV_SOURCED-}" ]; then return; fi 177 __ETC_ZSHENV_SOURCED=1 178 179 if [ -z "''${__NIXOS_SET_ENVIRONMENT_DONE-}" ]; then 180 . ${config.system.build.setEnvironment} 181 fi 182 183 HELPDIR="${pkgs.zsh}/share/zsh/$ZSH_VERSION/help" 184 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 189 190 # Setup custom shell init stuff. 191 ${cfge.shellInit} 192 193 ${cfg.shellInit} 194 195 # Read system-wide modifications. 196 if test -f /etc/zshenv.local; then 197 . /etc/zshenv.local 198 fi 199 ''; 200 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} 207 208 # Only execute this file once per shell. 209 if [ -n "''${__ETC_ZPROFILE_SOURCED-}" ]; then return; fi 210 __ETC_ZPROFILE_SOURCED=1 211 212 # Setup custom login shell init stuff. 213 ${cfge.loginShellInit} 214 215 ${cfg.loginShellInit} 216 217 # Read system-wide modifications. 218 if test -f /etc/zprofile.local; then 219 . /etc/zprofile.local 220 fi 221 ''; 222 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} 229 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 233 234 ${optionalString (cfg.setOptions != []) '' 235 # Set zsh options. 236 setopt ${concatStringsSep " " cfg.setOptions} 237 ''} 238 239 # Alternative method of determining short and full hostname. 240 HOST=${config.networking.fqdnOrHostName} 241 242 # Setup command line history. 243 # Don't export these, otherwise other shells (bash) will try to use same HISTFILE. 244 SAVEHIST=${toString cfg.histSize} 245 HISTSIZE=${toString cfg.histSize} 246 HISTFILE=${cfg.histFile} 247 248 # Configure sane keyboard defaults. 249 . /etc/zinputrc 250 251 ${optionalString cfg.enableGlobalCompInit '' 252 # Enable autocompletion. 253 autoload -U compinit && compinit 254 ''} 255 256 ${optionalString cfg.enableBashCompletion '' 257 # Enable compatibility with bash's completion system. 258 autoload -U bashcompinit && bashcompinit 259 ''} 260 261 # Setup custom interactive shell init stuff. 262 ${cfge.interactiveShellInit} 263 264 ${cfg.interactiveShellInit} 265 266 # Setup aliases. 267 ${zshAliases} 268 269 # Setup prompt. 270 ${cfg.promptInit} 271 272 # Disable some features to support TRAMP. 273 if [ "$TERM" = dumb ]; then 274 unsetopt zle prompt_cr prompt_subst 275 unset RPS1 RPROMPT 276 PS1='$ ' 277 PROMPT='$ ' 278 fi 279 280 # Read system-wide modifications. 281 if test -f /etc/zshrc.local; then 282 . /etc/zshrc.local 283 fi 284 ''; 285 286 # Bug in nix flakes: 287 # If we use `.source` here the path is garbage collected also we point to it with a symlink 288 # see https://github.com/NixOS/nixpkgs/issues/132732 289 environment.etc.zinputrc.text = builtins.readFile ./zinputrc; 290 291 environment.systemPackages = [ pkgs.zsh ] 292 ++ optional cfg.enableCompletion pkgs.nix-zsh-completions; 293 294 environment.pathsToLink = optional cfg.enableCompletion "/share/zsh"; 295 296 #users.defaultUserShell = mkDefault "/run/current-system/sw/bin/zsh"; 297 298 environment.shells = 299 [ 300 "/run/current-system/sw/bin/zsh" 301 "${pkgs.zsh}/bin/zsh" 302 ]; 303 304 }; 305 306}