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 {
9 if [[ -d $_sev_gpg_forward_dir && ( -z $1 || $1 == 'gpg-forward' ) ]] {
10 # clean up forward dirs if its session is dead or we ask for it
11 find $_sev_gpg_forward_dir -mindepth 1 -maxdepth 1 -type d |
13 # NOTE: the only way we can get here is if we have not been
14 # forwarded before, if the user asks for it, or during
15 # logout. if our own pid already has a dir, it is most likely
16 # stale, the user wants it removed, or something is very
17 # broken—in all 3 of these cases the best choice is remove it.
19 if {[[ -v _sev_gpg_forward_clean || $$ == $p ]] ||
20 ! kill -0 $p 2>/dev/null} {
21 find $x -mindepth 1 -maxdepth 1 | while {read -r y} {
22 # XXX: real dirs will stop unlink, consider it a feature
25 # don't force in case something important is still there
29 # reset GNUPGHOME if we removed our own dir
30 if [[ $GNUPGHOME =~ '/.ssh_forward/\d+/*$' && ! -d $GNUPGHOME ]]
31 GNUPGHOME=${GNUPGHOME%$MATCH}
35 # NOTE: _sev_tmp is not unset so session dirs will not be recreated if
36 # called during runtime; unset _sev_tmp and re-source to fix
37 # NOTE: XDG dirs that use our tmp are not unset here, they are in zlogout
38 # after this function is called
39 if [[ -d $_sev_tmp && ( -z $1 || $1 == 'tmp' ) ]] {
40 # clean up tmp dirs if its session is dead or we ask for it
41 find $_sev_tmp -mindepth 1 -maxdepth 1 -name '.session.*' -type d |
43 # NOTE: same rationale as above
44 p=${$(basename $x)#.session.}
45 if {[[ -v _sev_tmp_clean || $$ == $p ]] ||
46 ! kill -0 $p 2>/dev/null} {
53 function _sev_setpath {
54 # add as many generic paths as possible to keep the order we want
55 # NOTE: tied arrays path and fpath already exist, but are not unique (-U);
56 # we utilize the fact that unique arrays keep the first occurrence
57 # and remove any further occurences to check for elements from the
58 # old PATH that we did not anticipate and shift them to the front,
59 # since they are probably important to the system
60 typeset -gU path fpath
61 local -a syspath=("$path[@]")
62 # NOTE: /usr/{pkg,games} are unix/bsdisms
63 # NOTE: some systems (esp. research machines) may have multiple versions of
64 # packages installed in /opt/[pkg]/[ver]/bin or other dirs, managed
65 # with something like Environment Modules. this code does not account
66 # for this type of usage and will add all valid paths. any undesired
67 # paths can be removed using .zshenv.local.
68 # XXX: PREFIX not validated, non-POSIX but Termux uses it, maybe others
69 # XXX: XDG specifies ~/.local/bin as the only user-writable dir for
70 # executables, but we specify more; technically this is against spec
71 path=({{${_sev_home:-~},~}{/.local,},{$PREFIX,}{,/usr{,/local,/pkg},/opt{,/*{/*,}}}}/{s,}bin(N)
72 {$PREFIX,}/usr/{X11R{7,6}/bin,games}
73 # emulate Arch Linux flatpak-bindir.sh for use on other systems
74 {${XDG_DATA_HOME:-~/.local/share},{$PREFIX,}/var/lib}/flatpak/exports/bin)
76 path=("$path[@]" "$syspath[@]")
78 for (( i = 1; i <= $#path; i++ )) {
79 if [[ ! -d $path[$i] ]] {
81 ((i <= len)) && ((len--))
86 # shift valid system paths to the front if there are any left
87 ((len > 0 && len < $#path)) && path=("${(@)path[len + 1, -1]}" "${(@)path[1, len]}")
88 # include our zsh dir in fpath. unlike above, we always prefer our paths
89 fpath=({${ZDOTDIR:-{${_sev_home:-~},~}/.zsh},{${_sev_home:-~},~}/.zsh}/functions/**/*(/N) "$fpath[@]")
91 for (( i = 1; i <= $#fpath; i++ )) {
92 if [[ ! -d $fpath[$i] ]] {
98 # FPATH is not exported by default
100 # un-unique system arrays for consistency
101 typeset +U path fpath
105 export CHARSET=${CHARSET:-UTF-8}
106 export LANG=${LANG:-en_US.UTF-8}
108 ## alternative home for pulling in bin & config, used for zsu
109 [[ -v _sev_home ]] || export _sev_home=$HOME
112 # NOTE: we do this here instead of .zshrc since we might print stuff
113 if [[ -t 1 ]] { # only if stdout is tty
114 [[ ! -v TERM ]] && export TERM=xterm-256color >/dev/null 2>&1
115 if [[ $#terminfo -eq 0 ]] {
117 export TERM=xterm >/dev/null 2>&1
118 [[ -o interactive ]] &&
119 print -P "%F{red}!!! Can't find terminfo for $_oldterm, using $TERM%f"
125 if [[ ! -v _sev_setup_path || -o login ]] {
127 # NOTE: do not set _sev_setup_path, it is set in zprofile
130 ### home dir setup & additional exports
131 # XXX: traditionally, zshenv should just contain exports, and not touch the
132 # filesystem. however, our TMPDIR and XDG vars rely on mutable user paths
133 # that may not exist, and as such need to be set up before the rest of the
134 # system can use them. this is important as some environments include code
135 # in the global zprofile, or source scripts of other shells in the global
136 # zprofile, that may rely on our desired dir structure and vars pointing
137 # to it. for example, `flatpak-bindir.sh` in the Arch Linux flatpak
138 # package references $XDG_DATA_HOME with no fallback. since we do special
139 # handling for these vars before we export them, we're forced to do it all
140 # here instead of at the top of the zprofile.
143 # NOTE: need this for tmp, so confirm it exists.
144 # XXX: perms are not specified for XDG dirs except runtime. 760 makes the most
145 # sense, but we need to be a bit more permissive for zsu.
146 [[ -e ~/.local ]] && chmod 755 ~/.local || mkdir -pm766 ~/.local
149 # NOTE: specs say that POSIX tmp and XDG runtime directories should exist
150 # until the last session is logged out (POSIX can exist for longer).
151 # since we can't reliably keep track of sessions in a cross-platform
152 # manner, the current implementation should use a separate directory per
153 # toplevel session (i.e. SHLVL=1). this should placate most applications,
154 # though it is not expressly spec compliant. this may also cause problems
155 # with disowned applications that still try to use the directories after
156 # the toplevel shell has already logged out and the dirs removed, but the
157 # chances of that are slim. this also needs to be adjusted for usermode
158 # Xorg, as it requires $PREFIX/tmp/.X11-unix on most installs.
159 if [[ ! -v _sev_tmp ]] {
160 _sev_tmp=~/.local/tmp
161 # create personal TMPDIR under system tmp
162 # NOTE: under proot with uid remapping and shared /tmp, we can reuse old
163 # dir, without worrying about permission issues; intended for termux.
164 t=${TMPDIR:-${TEMPDIR:-${TEMP:-${TMP:-${${TMPPREFIX%/zsh}:-/tmp}}}}}/.home-${_sev_proot_real_user:-$LOGNAME}
165 [[ -e $t ]] || mkdir -m700 $t 2>/dev/null
167 # fallback TMPDIR to bare local directory or existing softlink
168 [[ -o interactive ]] &&
169 print -P "%F{orange}*** Can't create tmp dir $t, using $_sev_tmp%f"
170 [[ -h $_sev_tmp && ! -d _sev_tmp ]] && unlink $_sev_tmp 2>/dev/null
171 [[ ! -e $_sev_tmp ]] && mkdir -m700 $_sev_tmp 2>/dev/null
172 if [[ ! -d $_sev_tmp ]] {
173 _sev_tmp=${$(mktemp 2>/dev/null):-/tmp}
174 [[ -o interactive ]] &&
175 print -P "%F{red}!!! Can't create tmp dir, using $_sev_tmp%f"
177 } elif [[ -e $_sev_tmp && ! -h $_sev_tmp ]] {
178 # non-softlink node is on our local dir
179 [[ -o interactive ]] &&
180 print -P "%F{orange}*** $_sev_tmp exists, can't link to tmp dir $t, ignoring it%f"
183 if [[ ! -v $_sev_tmp_keep_link && -h $_sev_tmp && $_sev_tmp:P != $t:P ]] {
184 [[ -o interactive ]] &&
185 print -P "%F{orange}*** $_sev_tmp links to ${_sev_tmp:P} and not ${t:P}, unlinking it%f"
186 # NOTE: ln -f doesn't seem to work reliably with softlink
187 # directories, so explicitly remove the target if it exists
188 # XXX: potential race condition
189 # TODO: handle cleanup of old dir if it doesn't match?
190 unlink $_sev_tmp 2>/dev/null
192 if [[ ! -e $_sev_tmp ]] {
193 # link local dir to tmp dir
194 ln -s $t $_sev_tmp 2>/dev/null
197 # ensure dir is clean
199 # finally create our subdir for this session
200 t=$_sev_tmp/.session.$$
201 if ! mkdir -m700 $t 2>/dev/null; then
202 [[ -o interactive ]] &&
203 print -P "%F{red}!!! Can't create session tmp subdir $t, using $_sev_tmp%f"
206 export _sev_tmp TMPDIR=$t TEMPDIR=$t TEMP=$t TMP=$t TMPPREFIX=$t/zsh
211 if [[ ! -v _sev_setup_xdg ]] {
212 ## merge with any existing dirs and remove duplicates using unique arrays
213 # NOTE: we are accepting whatever value might be set for CONFIG and DATA;
214 # if it wasn't set, we just use default and leave it unset
215 # NOTE: include and then remove CONFIG_HOME and DATA_HOME to ensure they
216 # are not present in the array if it was added before we got to it
218 # source user dirs before other vars; technically it is against spec to
219 # include any of the below dirs there, but you never know what crazy shit
220 # people will do. I rather handle them sanely with our own code than let
221 # them override after the fact.
222 [[ -f $XDG_CONFIG_HOME/user-dirs.dirs ]] &&
223 emulate sh -c "source $XDG_CONFIG_HOME/user-dirs.dirs"
225 typeset -UT XDG_DATA_DIRS xdg_data_dirs
226 [[ -v XDG_DATA_HOME ]] && export XDG_DATA_HOME
227 [[ -e ${XDG_DATA_HOME:-~/.local/share} ]] ||
228 mkdir -m760 ${XDG_DATA_HOME:-~/.local/share}
229 xdg_data_dirs=($XDG_DATA_HOME ${XDG_DATA_DIRS:+${xdg_data_dirs%%/}}
230 /{usr{,/local,/pkg},opt{,/*{/*,}}}/share(N))
231 xdg_data_dirs=($xdg_data_dirs(/N))
234 typeset -UT XDG_CONFIG_DIRS xdg_config_dirs
235 [[ -v XDG_CONFIG_HOME ]] && export XDG_CONFIG_HOME
236 [[ -e ${XDG_CONFIG_HOME:-~/.config} ]] ||
237 mkdir -m760 ${XDG_CONFIG_HOME:-~/.config}
238 # I am of the belief .local should follow FHS /usr/local...
239 [[ -e ~/.local/etc ]] || ln -s ${XDG_CONFIG_HOME:-~/.config} ~/.local/etc
240 xdg_config_dirs=($XDG_CONFIG_HOME ${XDG_CONFIG_DIRS:+${xdg_config_dirs%%/}}
241 {,/usr{,/local,/pkg},opt{,/*{/*,}}}/etc/xdg(N))
242 xdg_config_dirs=($xdg_config_dirs(/N))
243 export XDG_CONFIG_DIRS
245 [[ -v XDG_STATE_HOME ]] && export XDG_STATE_HOME
246 [[ -e ${XDG_STATE_HOME:-~/.local/state} ]] ||
247 mkdir -m760 ${XDG_STATE_HOME:-~/.local/state}
249 if [[ -v XDG_CACHE_HOME ]] {
250 export XDG_CACHE_HOME
252 export XDG_CACHE_HOME=$_sev_tmp/.xdg.cache
254 [[ -e ${XDG_CACHE_HOME:-~/.cache} ]] ||
255 mkdir -m700 ${XDG_CACHE_HOME:-~/.cache}
257 # NOTE: this can be set by systemd or other pre-shell supervisor, and if
258 # any services were started such as pipewire, we need to use the
259 # existing runtime dir to preserve runtime state
260 if [[ -v XDG_RUNTIME_DIR ]] {
261 export XDG_RUNTIME_DIR
263 # make runtime dir in our session-specific tmpdir
264 export XDG_RUNTIME_DIR=$TMPDIR/.xdg.runtime
265 # same as in tmpdir creation, ensure dir doesn't exist
266 if [[ -h $XDG_RUNTIME_DIR ]] {
267 unlink $XDG_RUNTIME_DIR 2>/dev/null
268 } elif [[ -e $XDG_RUNTIME_DIR ]] {
269 rm -rf $XDG_RUNTIME_DIR 2>/dev/null
272 [[ ! -e $XDG_RUNTIME_DIR ]] &&
273 mkdir -m700 $XDG_RUNTIME_DIR 2>/dev/null
275 export _sev_setup_xdg=
278 ### app setup & exports
279 # NOTE: we set these up here since some scripts might need them
281 if [[ ! -v GNUPGHOME ]] {
282 export GNUPGHOME=${XDG_CONFIG_HOME:-~/.config}/gnupg
283 # move existing gnupg dir to our new home
284 if [[ -d ~/.gnupg && ! -d $GNUPGHOME ]] {
285 mv ~/.gnupg $GNUPGHOME
290 [[ ! -v PERL_LOCAL_LIB_ROOT && -v commands[perl] &&
291 -d $XDG_DATA_HOME/perl5/lib/perl5 ]] &&
292 eval $(perl -I$XDG_DATA_HOME/perl5/lib/perl5 \
293 -Mlocal::lib=$XDG_DATA_HOME/perl5 2>/dev/null)
296 if [[ -v commands[go] ]] {
297 [[ ! -v GOPATH ]] && export GOPATH=${XDG_DATA_HOME:-~/.local/share}/go:~/go
298 [[ ! -v GOBIN ]] && export GOBIN=~/.local/bin
301 ### load zshenv site-specific
302 autoload -Uz load-site-dotfile
303 load-site-dotfile zshenv
305 ### source .zprofile early for non-login shells that should be
306 if [[ ! -v _sev_first_display && ( -v DISPLAY || -v WAYLAND_DISPLAY ) ]] {
307 # most graphical login/session managers will spawn the user's shell as a
308 # parent of all child processes for that session. however, if the parent shell
309 # isn't a login shell for some reason, our .zprofile won't be run, and the
310 # environment won't be configured for child processes.
312 # XXX: .zprofile will be sourced by every new child shell if zsh is not
313 # used to start the graphical session and the _sev_first_display var
314 # isn't exported; for example, this previously happened when using
315 # sway without a display manager in front of it to run a login shell.
317 # this issue is not mitigated by .zprofile only loading what has not
318 # already been loaded if the env vars preventing the load are not set;
319 # in that case, every shell will think it is a fresh login shell.
321 # update gpgagent to use graphical pinentry
322 # XXX: will steal display from any other logged in graphical sessions, but
323 # I consider this to be an unlikely scenario
324 _sev_refresh_gpgagent=
326 export _sev_first_display=
327 [[ ! -o login ]] && source ${ZDOTDIR:-~}/.zprofile
328 } elif [[ ${+TERMUX_VERSION} -eq 0 && ! -o login && $SHLVL -eq 1 ]] {
329 # Termux first process isn't login shell, so source early
330 source ${ZDOTDIR:-~}/.zprofile