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