3 # XXX: only call after relevant vars have been set up, defined early so that
4 # below code can utilize it after they do so
5 function _sev_zcleanup {
8 function _sev_checkpid {
9 # return 1 if pid is a zsh process that isn't us
10 [[ $$ -eq $1 ]] || { ! kill -0 $1 2>/dev/null ||
11 [[ $(ps -aopid=,comm= | awk "\$1 == $1 { print \$2 }") != zsh ]] }
14 if [[ -d $_sev_gpg_forward_dir && ( -z $1 || $1 == 'gpg-forward' ) ]] {
15 # clean up forward dirs if its session is dead or we ask for it
16 find $_sev_gpg_forward_dir -mindepth 1 -maxdepth 1 -type d |
18 # NOTE: called before setup and on logout: remove the dir we
19 # will be using (it's stale) or the dir we did use, and any
20 # dead sessions if present
21 if (_sev_checkpid ${x:t}) {
22 find $x -mindepth 1 -maxdepth 1 | while {read -r y} {
23 # XXX: real dirs will stop unlink, consider it a feature
26 # don't force in case something important is still there
30 # reset GNUPGHOME if we removed our own dir
31 if [[ $GNUPGHOME =~ '/.ssh_forward/\d+/*$' && ! -d $GNUPGHOME ]]
32 GNUPGHOME=${GNUPGHOME%$MATCH}
36 # NOTE: _sev_tmp is not unset so session dirs will not be recreated if
37 # called during runtime; unset _sev_tmp and re-source to fix
38 # NOTE: XDG dirs that use our tmp are not unset here, they are in zlogout
39 # after this function is called
40 if [[ -d $_sev_tmp && ( -z $1 || $1 == 'tmp' ) ]] {
41 # clean up tmp dirs if its session is dead or we ask for it
42 find -L $_sev_tmp -mindepth 1 -maxdepth 1 -name '.session.*' -type d |
44 # NOTE: same rationale as above
45 if (_sev_checkpid ${${x:t}#.session.}) {
51 unfunction _sev_checkpid
54 function _sev_setpath {
55 # add as many generic paths as possible to keep the order we want
56 # NOTE: tied arrays path and fpath already exist, but are not unique (-U);
57 # we utilize the fact that unique arrays keep the first occurrence
58 # and remove any further occurrences to check for elements from the
59 # old PATH that we did not anticipate and shift them to the front,
60 # since they are probably important to the system
61 typeset -gU path fpath
62 local -a syspath=("$path[@]")
63 # NOTE: /usr/{pkg,games} are unix/bsdisms
64 # NOTE: some systems (esp. research machines) may have multiple versions of
65 # packages installed in /opt/[pkg]/[ver]/bin or other dirs, managed
66 # with something like Environment Modules. this code does not account
67 # for this type of usage and will add all valid paths. any undesired
68 # paths can be removed using .zshenv.local.
69 # NOTE: fun non-conformant systems like Android may have fun dirs that
70 # contain binaries intended solely for the base system, and their use
71 # by other users/subsystems may cause problems. for example,
72 # /system/bin/bc on Android may be a different version of or
73 # completely different codebase compared to the Termux-installed bc.
74 # modern Termux does not add these such systems to PATH by default,
75 # but older versions/configurations might.
76 # XXX: PREFIX not validated, non-POSIX but Termux uses it, maybe others
77 # XXX: XDG specifies ~/.local/bin as the only user-writable dir for
78 # executables, but we specify more; technically this is against spec
79 path=({{${_sev_home:-~},~}{/.local,},{$PREFIX,}{,/usr{,/local,/pkg},/opt{,/*{/*,}}}}/{s,}bin(N)
80 {$PREFIX,}/usr/{X11R{7,6}/bin,games}
81 # emulate Arch Linux flatpak-bindir.sh for use on other systems
82 {${XDG_DATA_HOME:-~/.local/share},{$PREFIX,}/var/lib}/flatpak/exports/bin)
85 # remove bad paths... after having combined the arrays to remove duplicates
86 for (( i = 1; i <= $#path; i++ )) {
87 if [[ ! -d $path[$i] ]] {
89 ((i <= len)) && ((len--))
94 # shift valid system paths to the front if there are any left
95 ((len > 0 && len < $#path)) && path=("${(@)path[len + 1, -1]}" "${(@)path[1, len]}")
96 # include our zsh dir in fpath. unlike above, we always prefer our paths
97 fpath=({${ZDOTDIR:-{${_sev_home:-~},~}/.zsh},{${_sev_home:-~},~}/.zsh}/functions/**/*(/N) "$fpath[@]")
99 for (( i = 1; i <= $#fpath; i++ )) {
100 if [[ ! -d $fpath[$i] ]] {
106 # FPATH is not exported by default
108 # un-unique system arrays for consistency
109 typeset +U path fpath
113 export CHARSET=${CHARSET:-UTF-8}
114 export LANG=${LANG:-en_US.UTF-8}
116 ## alternative home for pulling in bin & config, used for zsu
117 [[ -v _sev_home ]] || export _sev_home=$HOME
120 # NOTE: we do this here instead of .zshrc since we might print stuff
121 if [[ -t 1 ]] { # only if stdout is tty
122 [[ ! -v TERM ]] && export TERM=xterm-256color >/dev/null 2>&1
123 if [[ $#terminfo -eq 0 ]] {
125 export TERM=xterm >/dev/null 2>&1
126 [[ -o interactive ]] &&
127 print -P "%F{red}!!! Can't find terminfo for $_oldterm, using $TERM%f"
133 if [[ ! -v _sev_setup_path || -o login ]] {
135 # NOTE: do not set _sev_setup_path, it is set in zprofile
138 ### home dir setup & additional exports
139 # XXX: traditionally, zshenv should just contain exports, and not touch the
140 # filesystem. however, our TMPDIR and XDG vars rely on mutable user paths
141 # that may not exist, and as such need to be set up before the rest of the
142 # system can use them. this is important as some environments include code
143 # in the global zprofile, or source scripts of other shells in the global
144 # zprofile, that may rely on our desired dir structure and vars pointing
145 # to it. for example, `flatpak-bindir.sh` in the Arch Linux flatpak
146 # package references $XDG_DATA_HOME with no fallback. since we do special
147 # handling for these vars before we export them, we're forced to do it all
148 # here instead of at the top of the zprofile.
151 # NOTE: need this for tmp, so confirm it exists.
152 # XXX: perms are not specified for XDG dirs except runtime. 760 makes the most
153 # sense, but we need to be a bit more permissive for zsu.
154 [[ -e ~/.local ]] && chmod 755 ~/.local || mkdir -pm766 ~/.local
157 # NOTE: specs say that POSIX tmp and XDG runtime directories should exist
158 # until the last session is logged out (POSIX can exist for longer).
159 # since we can't reliably keep track of sessions in a cross-platform
160 # manner, the current implementation should use a separate directory per
161 # toplevel session (i.e. SHLVL=1). this should placate most applications,
162 # though it is not expressly spec compliant. this may also cause problems
163 # with disowned applications that still try to use the directories after
164 # the toplevel shell has already logged out and the dirs removed, but the
165 # chances of that are slim. this also needs to be adjusted for usermode
166 # Xorg, as it requires $PREFIX/tmp/.X11-unix on most installs.
167 if [[ ! -v _sev_tmp ]] {
168 _sev_tmp=~/.local/tmp
169 # create personal TMPDIR under system tmp
170 # NOTE: under proot with uid remapping and shared /tmp, we can reuse old
171 # dir, without worrying about permission issues; intended for termux.
172 _t=${TMPDIR:-${TEMPDIR:-${TEMP:-${TMP:-${${TMPPREFIX%/zsh}:-/tmp}}}}}/.home-${_sev_proot_real_user:-$LOGNAME}
173 [[ -e $_t ]] || mkdir -m700 $_t 2>/dev/null
175 # fallback TMPDIR to bare local directory or existing softlink
176 [[ -o interactive ]] &&
177 print -P "%F{orange}*** Can't create tmp dir $_t, using $_sev_tmp%f"
178 [[ -h $_sev_tmp && ! -d _sev_tmp ]] && unlink $_sev_tmp 2>/dev/null
179 [[ ! -e $_sev_tmp ]] && mkdir -m700 $_sev_tmp 2>/dev/null
180 if [[ ! -d $_sev_tmp ]] {
181 _sev_tmp=${$(mktemp 2>/dev/null):-/tmp}
182 [[ -o interactive ]] &&
183 print -P "%F{red}!!! Can't create tmp dir, using $_sev_tmp%f"
185 } elif [[ -e $_sev_tmp && ! -h $_sev_tmp ]] {
186 # non-softlink node is on our local dir
187 [[ -o interactive ]] &&
188 print -P "%F{orange}*** $_sev_tmp exists, can't link to tmp dir $_t, ignoring it%f"
191 if [[ ! -v $_sev_tmp_keep_link && -h $_sev_tmp && $_sev_tmp:P != $_t:P ]] {
192 [[ -o interactive ]] &&
193 print -P "%F{orange}*** $_sev_tmp links to ${_sev_tmp:P} and not ${t:P}, unlinking it%f"
194 # NOTE: ln -f doesn't seem to work reliably with softlink
195 # directories, so explicitly remove the target if it exists
196 # XXX: potential race condition
197 # TODO: handle cleanup of old dir if it doesn't match?
198 unlink $_sev_tmp 2>/dev/null
200 if [[ ! -e $_sev_tmp ]] {
201 # link local dir to tmp dir
202 ln -s $_t $_sev_tmp 2>/dev/null
205 # ensure dir is clean
207 # finally create our subdir for this session
208 _t=$_sev_tmp/.session.$$
209 # XXX: we probably shouldn't allow the use of an existing dir—tmp dir for
210 # current pid should have been removed by zcleanup
211 if ([[ ! -d $_t ]] && ! mkdir -m700 $_t 2>/dev/null) {
212 [[ -o interactive ]] &&
213 print -P "%F{red}!!! Can't create session tmp subdir $_t, using $_sev_tmp%f"
216 export _sev_tmp TMPDIR=$_t TEMPDIR=$_t TEMP=$_t TMP=$_t TMPPREFIX=$_t/zsh
221 if [[ ! -v _sev_setup_xdg ]] {
222 ## merge with any existing dirs and remove duplicates using unique arrays
223 # NOTE: we are accepting whatever value might be set for CONFIG and DATA;
224 # if it wasn't set, we just use default and leave it unset
225 # NOTE: include and then remove CONFIG_HOME and DATA_HOME to ensure they
226 # are not present in the array if it was added before we got to it
228 # source user dirs before other vars; technically it is against spec to
229 # include any of the below dirs there, but you never know what crazy shit
230 # people will do. I rather handle them sanely with our own code than let
231 # them override after the fact.
232 [[ -f $XDG_CONFIG_HOME/user-dirs.dirs ]] &&
233 emulate sh -c "source $XDG_CONFIG_HOME/user-dirs.dirs"
235 typeset -UT XDG_DATA_DIRS xdg_data_dirs
236 [[ -v XDG_DATA_HOME ]] && export XDG_DATA_HOME
237 [[ -e ${XDG_DATA_HOME:-~/.local/share} ]] ||
238 mkdir -m760 ${XDG_DATA_HOME:-~/.local/share}
239 xdg_data_dirs=($XDG_DATA_HOME ${XDG_DATA_DIRS:+${xdg_data_dirs%%/}}
240 /{usr{,/local,/pkg},opt{,/*{/*,}}}/share(N))
241 xdg_data_dirs=($xdg_data_dirs(/N))
244 typeset -UT XDG_CONFIG_DIRS xdg_config_dirs
245 [[ -v XDG_CONFIG_HOME ]] && export XDG_CONFIG_HOME
246 [[ -e ${XDG_CONFIG_HOME:-~/.config} ]] ||
247 mkdir -m760 ${XDG_CONFIG_HOME:-~/.config}
248 # I am of the belief .local should follow FHS /usr/local...
249 [[ -e ~/.local/etc ]] || ln -s ${XDG_CONFIG_HOME:-~/.config} ~/.local/etc
250 xdg_config_dirs=($XDG_CONFIG_HOME ${XDG_CONFIG_DIRS:+${xdg_config_dirs%%/}}
251 {,/usr{,/local,/pkg},opt{,/*{/*,}}}/etc/xdg(N))
252 xdg_config_dirs=($xdg_config_dirs(/N))
253 export XDG_CONFIG_DIRS
255 [[ -v XDG_STATE_HOME ]] && export XDG_STATE_HOME
256 [[ -e ${XDG_STATE_HOME:-~/.local/state} ]] ||
257 mkdir -m760 ${XDG_STATE_HOME:-~/.local/state}
259 if [[ -v XDG_CACHE_HOME ]] {
260 export XDG_CACHE_HOME
262 export XDG_CACHE_HOME=$_sev_tmp/.xdg.cache
264 [[ -e ${XDG_CACHE_HOME:-~/.cache} ]] ||
265 mkdir -m700 ${XDG_CACHE_HOME:-~/.cache}
267 # NOTE: this can be set by systemd or other pre-shell supervisor, and if
268 # any services were started such as pipewire, we need to use the
269 # existing runtime dir to preserve runtime state
270 if [[ -v XDG_RUNTIME_DIR ]] {
271 export XDG_RUNTIME_DIR
273 # make runtime dir in our session-specific tmpdir
274 export XDG_RUNTIME_DIR=$TMPDIR/.xdg.runtime
275 # same as in tmpdir creation, ensure dir doesn't exist
276 if [[ -h $XDG_RUNTIME_DIR ]] {
277 unlink $XDG_RUNTIME_DIR 2>/dev/null
278 } elif [[ -e $XDG_RUNTIME_DIR ]] {
279 rm -rf $XDG_RUNTIME_DIR 2>/dev/null
282 [[ ! -e $XDG_RUNTIME_DIR ]] &&
283 mkdir -m700 $XDG_RUNTIME_DIR 2>/dev/null
285 export _sev_setup_xdg=
288 ### app setup & exports
289 # NOTE: we set these up here since some scripts might need them
291 if [[ ! -v GNUPGHOME ]] {
292 export GNUPGHOME=${XDG_CONFIG_HOME:-~/.config}/gnupg
293 # move existing gnupg dir to our new home
294 if [[ -d ~/.gnupg && ! -d $GNUPGHOME ]] {
295 mv ~/.gnupg $GNUPGHOME
300 if [[ ! -v PERL_LOCAL_LIB_ROOT && -v commands[perl] ]] {
301 _p5=${XDG_DATA_HOME:-~/.local/share}/perl5
302 [[ -d $_p5 ]] || mkdir -p $_p5
303 if [[ -f $_p5/lib/perl5/local/lib.pm ]] {
304 eval $(perl -I$_p5/lib/perl5 -Mlocal::lib=$_p5 2>/dev/null)
306 # emulate local::lib if not installed
307 path=($_p5/bin "${path[@]}")
309 PERL_MB_OPT="--install_base '$_p5'" \
310 PERL_MM_OPT=INSTALL_BASE=$_p5 \
311 PERL5LIB=$_p5/lib/perl5 \
312 PERL_LOCAL_LIB_ROOT=$_p5${PERL_LOCAL_LIB_ROOT:+:$PERL_LOCAL_LIB_ROOT}
317 if [[ -v commands[go] ]] {
318 [[ ! -v GOPATH ]] && export GOPATH=${XDG_DATA_HOME:-~/.local/share}/go:~/go
319 [[ ! -v GOBIN ]] && export GOBIN=~/.local/bin
323 autoload -Uz load-plugins
326 ### load zshenv site-specific
327 autoload -Uz load-site-dotfile
328 load-site-dotfile zshenv
330 ### source .zprofile early for non-login shells that should be
331 if [[ ! -v _sev_first_display && ( -v DISPLAY || -v WAYLAND_DISPLAY ) ]] {
332 # most graphical login/session managers will spawn the user's shell as a
333 # parent of all child processes for that session. however, if the parent
334 # shell isn't a login shell for some reason, our .zprofile won't be run,
335 # and the environment won't be configured for child processes.
337 # XXX: .zprofile will be sourced by every new child shell if zsh is not
338 # used to start the graphical session and the _sev_first_display var
339 # isn't exported; for example, this previously happened when using
340 # sway without a display manager in front of it to run a login shell.
342 # this issue is not mitigated by .zprofile only loading what has not
343 # already been loaded if the env vars preventing the load are not set;
344 # in that case, every shell will think it is a fresh login shell.
346 # update gpgagent to use graphical pinentry
347 # XXX: will steal display from any other logged in graphical sessions, but
348 # I consider this to be an unlikely scenario
349 _sev_refresh_gpgagent=
351 export _sev_first_display=
352 [[ ! -o login ]] && source ${ZDOTDIR:-~}/.zprofile
353 } elif [[ ${+TERMUX_VERSION} -eq 0 && ! -o login && $SHLVL -eq 1 ]] {
354 # Termux first process isn't login shell, so source early
355 source ${ZDOTDIR:-~}/.zprofile