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