]> git.sev.monster Git - dotfiles.git/blame_incremental - etc/zsh/.zshenv
zshrc: add more git aliases, dotfiles update alias
[dotfiles.git] / etc / zsh / .zshenv
... / ...
CommitLineData
1### functions
2## cleanup
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
5function _sev_zcleanup {
6 # gpg forwarding
7 if [[ -d $_sev_gpg_forward_dir && ( -z $1 || $1 == 'gpg-forward' ) ]] {
8 # clean up forward dirs if its session is dead or we ask for it
9 find $_sev_gpg_forward_dir -mindepth 1 -maxdepth 1 -type d |
10 while {read -r x} {
11 # NOTE: the only way we can get here is if we have not been
12 # forwarded before, if the user asks for it, or during
13 # logout. if our own pid already has a dir, it is most likely
14 # stale, the user wants it removed, or something is very
15 # broken—in all 3 of these cases the best choice is remove it.
16 p=$(basename $x)
17 if {[[ -v _sev_gpg_forward_clean || $$ == $p ]] ||
18 ! kill -0 $p 2>/dev/null} {
19 find $x -mindepth 1 -maxdepth 1 | while {read -r y} {
20 # XXX: real dirs will stop unlink, consider it a feature
21 unlink $y
22 }
23 # don't force in case something important is still there
24 rmdir $x
25 }
26 }
27 # reset GNUPGHOME if we removed our own dir
28 if [[ $GNUPGHOME =~ '/.ssh_forward/\d+/*$' && ! -e $GNUPGHOME ]]
29 GNUPGHOME=${GNUPGHOME%$MATCH}
30 }
31
32 # custom tmp
33 # NOTE: _sev_tmp is not unset so session dirs will not be recreated if
34 # called during runtime; unset _sev_tmp and re-source to fix
35 # NOTE: XDG dirs that use our tmp are not unset here, they are in zlogout
36 # after this function is called
37 if [[ -d $_sev_tmp && ( -z $1 || $1 == 'tmp' ) ]] {
38 # clean up tmp dirs if its session is dead or we ask for it
39 find $_sev_tmp -mindepth 1 -maxdepth 1 -name '.session.*' -type d |
40 while {read -r x} {
41 # NOTE: same rationale as above
42 p=${$(basename $x)#.session.}
43 if {[[ -v _sev_tmp_clean || $$ == $p ]] ||
44 ! kill -0 $p 2>/dev/null} {
45 rm -rf $x
46 }
47 }
48 }
49
50 unset x p y
51}
52
53function _sev_setpath {
54 # NOTE: we utilize the fact that unique arrays keep the first occurrence and
55 # remove any further occurences to capture elements from the old PATH
56 # that we did not anticipate and shift them to the front, since they are
57 # probably important to the system
58 typeset -gU path fpath
59 # add as many generic paths as possible to keep the order we want
60 # NOTE: /usr/{local,pkg,games} are unix/bsdisms
61 # XXX: PREFIX not validated, non-posix but Termux uses it, maybe others
62 # XXX: XDG specifies ~/.local/bin as the only user-writable dir for
63 # executables, but we specify more; technically this is against spec
64 syspath=("$path[@]")
65 path=({{${_sev_home:-~},~}{/.local,},{$PREFIX,}{,/opt,/usr{,/local,/pkg}}}/{s,}bin
66 {$PREFIX,}/usr/{X11R{7,6}/bin,games}
67 # emulate Arch Linux flatpak-bindir.sh for use on other systems
68 {${XDG_DATA_HOME:-~/.local/share},{$PREFIX,}/var/lib}/flatpak/exports/bin)
69 ((len=$#path))
70 path=("$path[@]" "$syspath[@]")
71 # remove bad paths
72 for (( i = 1; i <= $#path; i++ )) {
73 if [[ ! -d $path[$i] ]] {
74 path[$i]=()
75 ((i <= len)) && ((len--))
76 ((i--))
77 continue
78 }
79 }
80 # shift valid system paths to the front if there are any left
81 ((len > 0 && len < $#path)) && path=("${(@)path[len + 1, -1]}" "${(@)path[1, len]}")
82 # include our zsh dir in fpath. unlike above, we always prefer our paths
83 fpath=({${ZDOTDIR:-~/.zsh},{${_sev_home:-~},~}/.zsh}/functions/**/*(/N) "$fpath[@]")
84 # remove bad paths
85 for (( i = 1; i <= $#fpath; i++ )) {
86 if [[ ! -d $fpath[$i] ]] {
87 fpath[$i]=()
88 ((i--))
89 continue
90 }
91 }
92 # FPATH is not exported by default
93 export FPATH
94 typeset +U path fpath
95}
96
97### common exports
98export CHARSET=${CHARSET:-UTF-8}
99export LANG=${LANG:-en_US.UTF-8}
100
101## alternative home for pulling in bin & config, used for zsu
102[[ -v _sev_home ]] || export _sev_home=$HOME
103
104## fix broken term
105# NOTE: we do this here instead of .zshrc since we might echo stuff
106if [[ -t 1 ]] { # only if stdout is tty
107 [[ ! -v TERM ]] && export TERM=xterm-256color >/dev/null 2>&1
108 if [[ $#terminfo -eq 0 ]] {
109 _oldterm=$TERM
110 export TERM=xterm >/dev/null 2>&1
111 [[ -o interactive ]] &&
112 print -P "%F{red}!!! Can't find terminfo for $_oldterm, using $TERM%f"
113 unset _oldterm
114 }
115}
116
117## path
118if [[ ! -v _sev_setup_path || -o login ]] {
119 _sev_setpath
120 # NOTE: do not set _sev_setup_path, it is set in zprofile
121}
122
123### home dir setup & exports
124# XXX: traditionally, zshenv should just contain exports, and not touch the
125# filesystem. however, some system profile scripts that are sourced in the
126# system zprofile may attempt to do things that rely on some of these
127# vars. for example, `flatpak-bindir.sh` in the Arch Linux flatpak package
128# references $XDG_DATA_HOME with no fallback. since we do special handling
129# for these vars before we export them, we're forced to do it all here
130# instead of zprofile.
131
132## xdg local dir
133# NOTE: need this for tmp, so confirm it exists.
134# XXX: perms are not specified for XDG dirs except runtime, but I think 760
135# makes the most sense. shouldn't break anything since no one else should
136# be poking around in our dir.
137[[ -e ~/.local ]] || mkdir -m760 ~/.local
138
139## tmp
140# NOTE: specs say that POSIX tmp and XDG runtime directories should exist
141# until the last session is logged out (POSIX can exist for longer).
142# since we can't reliably keep track of sessions in a cross-platform
143# manner, the current implementation should use a separate directory per
144# toplevel session (i.e. SHLVL=1). this should placate most applications,
145# though it is not expressly spec compliant. this may also cause problems
146# with disowned applications that still try to use the directories after
147# the toplevel shell has already logged out and the dirs removed, but the
148# chances of that are slim.
149if [[ ! -v _sev_tmp ]] {
150 # create personal TMPDIR under system tmp
151 t=${TMPDIR:-${TEMPDIR:-${TEMP:-${TMP:-/tmp}}}}/.home-$LOGNAME
152 [[ -e $t ]] || mkdir -m700 $t 2>/dev/null
153 _sev_tmp=~/.local/tmp
154 if [[ ! -d $t ]] {
155 # fallback TMPDIR to bare local directory or existing softlink
156 [[ -o interactive ]] &&
157 print -P "%F{orange}*** Can't create tmp dir $t, using $_sev_tmp%f"
158 [[ -h $_sev_tmp && ! -d _sev_tmp ]] && unlink $_sev_tmp 2>/dev/null
159 [[ ! -e $_sev_tmp ]] && mkdir -m700 $_sev_tmp 2>/dev/null
160 if [[ ! -d $_sev_tmp ]] {
161 _sev_tmp=${$(mktemp 2>/dev/null):/tmp}
162 [[ -o interactive ]] &&
163 print -P "%F{red}!!! Can't create tmp dir, using $_sev_tmp%f"
164 }
165 } elif [[ -f $_sev_tmp || ( -d $_sev_tmp && ! -h $_sev_tmp ) ]] {
166 # file or non-softlink directory is on our local dir
167 [[ -o interactive ]] &&
168 print -P "%F{orange}*** $_sev_tmp exists, can't link to tmp dir $t, ignoring it%f"
169 _sev_tmp=$t
170 } else {
171 # link local dir to tmp dir
172 if [[ -h $_sev_tmp && $_sev_tmp:P != $t:P ]] {
173 [[ -o interactive ]] &&
174 print -P "%F{orange}*** $_sev_tmp links to ${_sev_tmp:P} and not ${t:P}, unlinking it%f"
175 # XXX: race condition for existing sessions still using this dir
176 unlink $_sev_tmp 2>/dev/null
177 }
178 ln -s $t $_sev_tmp 2>/dev/null
179 }
180 if [[ -v _sev_tmp ]] {
181 # ensure dir is clean
182 _sev_zcleanup tmp
183 # finally create our subdir for this session
184 t=$_sev_tmp/.session.$$
185 if ! mkdir -m700 $t 2>/dev/null; then
186 [[ -o interactive ]] &&
187 print -P "%F{red}!!! Can't create session tmp subdir $t, using $_sev_tmp%f"
188 t=$_sev_tmp
189 fi
190 export _sev_tmp TMPDIR=$t TEMPDIR=$t TEMP=$t TMP=$t
191 }
192 unset t
193}
194
195## xdg
196if [[ ! -v _sev_setup_xdg ]] {
197 ## merge with any existing dirs and remove duplicates using unique arrays
198 # NOTE: we are accepting whatever value might be set for CONFIG and DATA;
199 # if it wasn't set, we just use default and leave it unset
200 # NOTE: include and then remove CONFIG_HOME and DATA_HOME to ensure they
201 # are not present in the array if it was added before we got to it
202
203 # source user dirs before other vars; technically it is against spec to
204 # include any of the below dirs there, but you never know what crazy shit
205 # people will do. I rather handle them sanely with our own code than let
206 # them override after the fact.
207 [[ -e $XDG_CONFIG_HOME/user-dirs.dirs ]] &&
208 emulate sh -c "source $XDG_CONFIG_HOME/user-dirs.dirs"
209
210 typeset -UT XDG_DATA_DIRS xdg_data_dirs
211 if [[ -v XDG_DATA_HOME ]] {
212 export XDG_DATA_HOME
213 } elif [[ ! -e ~/.local/share ]] {
214 mkdir -m760 ~/.local/share
215 }
216 xdg_data_dirs=($XDG_DATA_HOME /{opt,usr/local,usr/pkg,usr}/share
217 ${XDG_DATA_DIRS:+${xdg_data_dirs%%/}})
218 export XDG_DATA_DIRS
219
220 typeset -UT XDG_CONFIG_DIRS xdg_config_dirs
221 if [[ -v XDG_CONFIG_HOME ]] {
222 export XDG_CONFIG_HOME
223 } elif [[ ! -e ~/.config ]] {
224 mkdir -m760 ~/.config
225 }
226 # I am of the belief .local should follow FHS /usr/local...
227 [[ -e ~/.local/etc ]] || ln -s ~/.config ~/.local/etc
228 xdg_config_dirs=($XDG_CONFIG_HOME
229 ${XDG_CONFIG_DIRS:+${xdg_config_dirs%%/}}
230 {/opt,/usr/local,/usr/pkg,}/etc/xdg)
231 export XDG_CONFIG_DIRS
232
233 if [[ -v XDG_STATE_HOME ]] {
234 export XDG_STATE_HOME
235 } elif [[ ! -e ~/.local/state ]] {
236 mkdir -m760 ~/.local/state
237 }
238
239 if [[ -v XDG_CACHE_HOME ]] {
240 export XDG_CACHE_HOME
241 } else {
242 if [[ -v _sev_tmp ]] {
243 export XDG_CACHE_HOME=$_sev_tmp/.xdg.cache
244 [[ -e $XDG_CACHE_HOME ]] || mkdir -m700 $XDG_CACHE_HOME
245 } elif [[ ! -e ~/.cache ]] {
246 mkdir -m700 ~/.cache
247 }
248 }
249
250 if [[ -v XDG_RUNTIME_DIR ]] {
251 # NOTE: this can be set by systemd or other pre-shell supervisor, and
252 # if any services were started such as pipewire, we need to use
253 # the existing runtime dir so that we can get any existing
254 # program sockets or other data
255 export XDG_RUNTIME_DIR
256 } elif [[ -v TMPDIR ]] {
257 # make runtime dir in our session-specific tmpdir
258 export XDG_RUNTIME_DIR=$TMPDIR/.xdg.runtime
259 # same as in tmpdir creation, ensure dir doesn't exist
260 if [[ -h $XDG_RUNTIME_DIR ]] {
261 unlink $XDG_RUNTIME_DIR 2>/dev/null
262 } elif [[ -e $XDG_RUNTIME_DIR ]] {
263 rm -rf $XDG_RUNTIME_DIR 2>/dev/null
264 }
265 mkdir -m700 $XDG_RUNTIME_DIR 2>/dev/null
266 }
267
268 export _sev_setup_xdg=
269}
270
271### app setup & exports
272# NOTE: we set these up here since some scripts might need them
273## gpg home
274if [[ ! -v GNUPGHOME ]] {
275 export GNUPGHOME=${XDG_CONFIG_HOME:-~/.config}/gnupg
276 # move existing gnupg dir to our new home
277 if [[ -d ~/.gnupg && ! -d $GNUPGHOME ]] {
278 mv ~/.gnupg $GNUPGHOME
279 }
280}
281
282## perl local lib
283[[ ! -v PERL_LOCAL_LIB_ROOT && -v commands[perl] &&
284 -d $XDG_DATA_HOME/perl5/lib/perl5 ]] &&
285 eval $(perl -I$XDG_DATA_HOME/perl5/lib/perl5 \
286 -Mlocal::lib=$XDG_DATA_HOME/perl5 2>/dev/null)
287
288### load zshenv site-specific
289autoload -Uz load-site-dotfile
290load-site-dotfile zshenv
291
292### source .zprofile early for non-login shells that should be
293if [[ ! -v _sev_first_display && ( -v DISPLAY || -v WAYLAND_DISPLAY ) ]] {
294 # most graphical login/session managers will spawn the user's shell as a
295 # parent of all child processes for that session. however, if the parent shell
296 # isn't a login shell for some reason, our .zprofile won't be run, and the
297 # environment won't be configured for child processes.
298 #
299 # XXX: .zprofile will be sourced by every new child shell if zsh is not
300 # used to start the graphical session and the _sev_first_display var
301 # isn't exported; for example, this previously happened when using
302 # sway without a display manager in front of it to run a login shell.
303 #
304 # this issue is not mitigated by .zprofile only loading what has not
305 # already been loaded if the env vars preventing the load are not set;
306 # in that case, every shell will think it is a fresh login shell.
307
308 # update gpgagent to use graphical pinentry
309 # XXX: will steal display from any other logged in graphical sessions, but
310 # I consider this to be an unlikely scenario
311 _sev_refresh_gpgagent=
312
313 export _sev_first_display=
314 [[ ! -o login ]] && source ${ZDOTDIR:-~}/.zprofile
315} elif [[ ${+TERMUX_VERSION} -eq 0 && ! -o login && $SHLVL -eq 1 ]] {
316 # Termux first process isn't login shell, so source early
317 source ${ZDOTDIR:-~}/.zprofile
318}
This page took 0.039318 seconds and 4 git commands to generate.