1# This module defines global configuration for the zshell.
2
3{ config, lib, pkgs, ... }:
4
5with lib;
6
7let
8
9 cfge = config.environment;
10
11 cfg = config.programs.zsh;
12
13 zshAliases = concatStringsSep "\n" (
14 mapAttrsFlatten (k: v: "alias ${k}='${v}'") cfg.shellAliases
15 );
16
17in
18
19{
20
21 options = {
22
23 programs.zsh = {
24
25 enable = mkOption {
26 default = false;
27 description = ''
28 Whether to configure zsh as an interactive shell. To enable zsh for
29 a particular user, use the <option>users.users.<name?>.shell</option>
30 option for that user. To enable zsh system-wide use the
31 <option>users.defaultUserShell</option> option.
32 '';
33 type = types.bool;
34 };
35
36 shellAliases = mkOption {
37 default = config.environment.shellAliases;
38 description = ''
39 Set of aliases for zsh shell. See <option>environment.shellAliases</option>
40 for an option format description.
41 '';
42 type = types.attrs; # types.attrsOf types.stringOrPath;
43 };
44
45 shellInit = mkOption {
46 default = "";
47 description = ''
48 Shell script code called during zsh shell initialisation.
49 '';
50 type = types.lines;
51 };
52
53 loginShellInit = mkOption {
54 default = "";
55 description = ''
56 Shell script code called during zsh login shell initialisation.
57 '';
58 type = types.lines;
59 };
60
61 interactiveShellInit = mkOption {
62 default = "";
63 description = ''
64 Shell script code called during interactive zsh shell initialisation.
65 '';
66 type = types.lines;
67 };
68
69 promptInit = mkOption {
70 default = ''
71 autoload -U promptinit && promptinit && prompt walters
72 '';
73 description = ''
74 Shell script code used to initialise the zsh prompt.
75 '';
76 type = types.lines;
77 };
78
79 enableCompletion = mkOption {
80 default = true;
81 description = ''
82 Enable zsh completion for all interactive zsh shells.
83 '';
84 type = types.bool;
85 };
86
87 enableAutosuggestions = mkOption {
88 default = false;
89 description = ''
90 Enable zsh-autosuggestions
91 '';
92 };
93
94 };
95
96 };
97
98 config = mkIf cfg.enable {
99
100 environment.etc."zshenv".text =
101 ''
102 # /etc/zshenv: DO NOT EDIT -- this file has been generated automatically.
103 # This file is read for all shells.
104
105 # Only execute this file once per shell.
106 # But don't clobber the environment of interactive non-login children!
107 if [ -n "$__ETC_ZSHENV_SOURCED" ]; then return; fi
108 export __ETC_ZSHENV_SOURCED=1
109
110 . ${config.system.build.setEnvironment}
111
112 ${cfge.shellInit}
113
114 ${cfg.shellInit}
115
116 # Read system-wide modifications.
117 if test -f /etc/zshenv.local; then
118 . /etc/zshenv.local
119 fi
120 '';
121
122 environment.etc."zprofile".text =
123 ''
124 # /etc/zprofile: DO NOT EDIT -- this file has been generated automatically.
125 # This file is read for login shells.
126
127 # Only execute this file once per shell.
128 if [ -n "$__ETC_ZPROFILE_SOURCED" ]; then return; fi
129 __ETC_ZPROFILE_SOURCED=1
130
131 ${cfge.loginShellInit}
132
133 ${cfg.loginShellInit}
134
135 # Read system-wide modifications.
136 if test -f /etc/zprofile.local; then
137 . /etc/zprofile.local
138 fi
139 '';
140
141 environment.etc."zshrc".text =
142 ''
143 # /etc/zshrc: DO NOT EDIT -- this file has been generated automatically.
144 # This file is read for interactive shells.
145
146 # Only execute this file once per shell.
147 if [ -n "$__ETC_ZSHRC_SOURCED" -o -n "$NOSYSZSHRC" ]; then return; fi
148 __ETC_ZSHRC_SOURCED=1
149
150 . /etc/zinputrc
151
152 # history defaults
153 SAVEHIST=2000
154 HISTSIZE=2000
155 HISTFILE=$HOME/.zsh_history
156
157 setopt HIST_IGNORE_DUPS SHARE_HISTORY HIST_FCNTL_LOCK
158
159 HELPDIR="${pkgs.zsh}/share/zsh/$ZSH_VERSION/help"
160
161 # Tell zsh how to find installed completions
162 for p in ''${(z)NIX_PROFILES}; do
163 fpath+=($p/share/zsh/site-functions $p/share/zsh/$ZSH_VERSION/functions $p/share/zsh/vendor-completions)
164 done
165
166 ${optionalString cfg.enableCompletion "autoload -U compinit && compinit"}
167
168 ${optionalString (cfg.enableAutosuggestions)
169 "source ${pkgs.zsh-autosuggestions}/share/zsh-autosuggestions/zsh-autosuggestions.zsh"
170 }
171
172 ${cfge.interactiveShellInit}
173
174 ${cfg.interactiveShellInit}
175
176 ${zshAliases}
177
178 ${cfg.promptInit}
179
180 # Read system-wide modifications.
181 if test -f /etc/zshrc.local; then
182 . /etc/zshrc.local
183 fi
184 '';
185
186 environment.etc."zinputrc".source = ./zinputrc;
187
188 environment.systemPackages = [ pkgs.zsh ]
189 ++ optional cfg.enableCompletion pkgs.nix-zsh-completions;
190
191 environment.pathsToLink = optional cfg.enableCompletion "/share/zsh";
192
193 #users.defaultUserShell = mkDefault "/run/current-system/sw/bin/zsh";
194
195 environment.shells =
196 [ "/run/current-system/sw/bin/zsh"
197 "/var/run/current-system/sw/bin/zsh"
198 "${pkgs.zsh}/bin/zsh"
199 ];
200
201 };
202
203}