]>
Commit | Line | Data |
---|---|---|
8a92a2c0 | 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 | |
5 | function _sev_zcleanup { | |
176b807b | 6 | local x y |
6d4ce4aa | 7 | |
176b807b | 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 ]] } | |
12 | } | |
8a92a2c0 | 13 | # gpg forwarding |
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 | | |
17 | while {read -r x} { | |
176b807b | 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}) { | |
8a92a2c0 | 22 | find $x -mindepth 1 -maxdepth 1 | while {read -r y} { |
23 | # XXX: real dirs will stop unlink, consider it a feature | |
24 | unlink $y | |
25 | } | |
26 | # don't force in case something important is still there | |
27 | rmdir $x | |
28 | } | |
29 | } | |
30 | # reset GNUPGHOME if we removed our own dir | |
6d4ce4aa | 31 | if [[ $GNUPGHOME =~ '/.ssh_forward/\d+/*$' && ! -d $GNUPGHOME ]] |
8a92a2c0 | 32 | GNUPGHOME=${GNUPGHOME%$MATCH} |
33 | } | |
b31a3fb1 | 34 | |
8a92a2c0 | 35 | # custom tmp |
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 | |
176b807b | 42 | find -L $_sev_tmp -mindepth 1 -maxdepth 1 -name '.session.*' -type d | |
8a92a2c0 | 43 | while {read -r x} { |
44 | # NOTE: same rationale as above | |
176b807b | 45 | if (_sev_checkpid ${${x:t}#.session.}) { |
8a92a2c0 | 46 | rm -rf $x |
47 | } | |
48 | } | |
b31a3fb1 | 49 | } |
176b807b | 50 | |
51 | unfunction _sev_checkpid | |
b31a3fb1 | 52 | } |
53 | ||
8a92a2c0 | 54 | function _sev_setpath { |
b31a3fb1 | 55 | # add as many generic paths as possible to keep the order we want |
e7e78648 | 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 | |
caa1dd46 | 58 | # and remove any further occurrences to check for elements from the |
e7e78648 | 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. | |
caa1dd46 | 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. | |
e7e78648 | 76 | # XXX: PREFIX not validated, non-POSIX but Termux uses it, maybe others |
b31a3fb1 | 77 | # XXX: XDG specifies ~/.local/bin as the only user-writable dir for |
78 | # executables, but we specify more; technically this is against spec | |
e7e78648 | 79 | path=({{${_sev_home:-~},~}{/.local,},{$PREFIX,}{,/usr{,/local,/pkg},/opt{,/*{/*,}}}}/{s,}bin(N) |
8a92a2c0 | 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) | |
6d4ce4aa | 83 | local -i i len=$#path |
6cdc3a16 | 84 | path+=("$syspath[@]") |
85 | # remove bad paths... after having combined the arrays to remove duplicates | |
b31a3fb1 | 86 | for (( i = 1; i <= $#path; i++ )) { |
87 | if [[ ! -d $path[$i] ]] { | |
88 | path[$i]=() | |
89 | ((i <= len)) && ((len--)) | |
90 | ((i--)) | |
91 | continue | |
92 | } | |
93 | } | |
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]}") | |
b31a3fb1 | 96 | # include our zsh dir in fpath. unlike above, we always prefer our paths |
e71b6dc4 | 97 | fpath=({${ZDOTDIR:-{${_sev_home:-~},~}/.zsh},{${_sev_home:-~},~}/.zsh}/functions/**/*(/N) "$fpath[@]") |
8a92a2c0 | 98 | # remove bad paths |
99 | for (( i = 1; i <= $#fpath; i++ )) { | |
100 | if [[ ! -d $fpath[$i] ]] { | |
101 | fpath[$i]=() | |
102 | ((i--)) | |
103 | continue | |
104 | } | |
105 | } | |
b31a3fb1 | 106 | # FPATH is not exported by default |
107 | export FPATH | |
e7e78648 | 108 | # un-unique system arrays for consistency |
b31a3fb1 | 109 | typeset +U path fpath |
8a92a2c0 | 110 | } |
111 | ||
112 | ### common exports | |
113 | export CHARSET=${CHARSET:-UTF-8} | |
114 | export LANG=${LANG:-en_US.UTF-8} | |
115 | ||
116 | ## alternative home for pulling in bin & config, used for zsu | |
117 | [[ -v _sev_home ]] || export _sev_home=$HOME | |
118 | ||
119 | ## fix broken term | |
294ed44e | 120 | # NOTE: we do this here instead of .zshrc since we might print stuff |
8a92a2c0 | 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 ]] { | |
124 | _oldterm=$TERM | |
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" | |
128 | unset _oldterm | |
129 | } | |
130 | } | |
131 | ||
132 | ## path | |
133 | if [[ ! -v _sev_setup_path || -o login ]] { | |
134 | _sev_setpath | |
135 | # NOTE: do not set _sev_setup_path, it is set in zprofile | |
136 | } | |
137 | ||
7c1bd0ff | 138 | ### home dir setup & additional exports |
8a92a2c0 | 139 | # XXX: traditionally, zshenv should just contain exports, and not touch the |
7c1bd0ff | 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. | |
8a92a2c0 | 149 | |
150 | ## xdg local dir | |
151 | # NOTE: need this for tmp, so confirm it exists. | |
1a578fd0 | 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 | |
8a92a2c0 | 155 | |
156 | ## tmp | |
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 | |
7c1bd0ff | 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. | |
8a92a2c0 | 167 | if [[ ! -v _sev_tmp ]] { |
96b7dae3 | 168 | _sev_tmp=~/.local/tmp |
8a92a2c0 | 169 | # create personal TMPDIR under system tmp |
1a578fd0 | 170 | # NOTE: under proot with uid remapping and shared /tmp, we can reuse old |
171 | # dir, without worrying about permission issues; intended for termux. | |
6bbeb528 | 172 | _t=${TMPDIR:-${TEMPDIR:-${TEMP:-${TMP:-${${TMPPREFIX%/zsh}:-/tmp}}}}}/.home-${_sev_proot_real_user:-$LOGNAME} |
173 | [[ -e $_t ]] || mkdir -m700 $_t 2>/dev/null | |
174 | if [[ ! -d $_t ]] { | |
8a92a2c0 | 175 | # fallback TMPDIR to bare local directory or existing softlink |
176 | [[ -o interactive ]] && | |
6bbeb528 | 177 | print -P "%F{orange}*** Can't create tmp dir $_t, using $_sev_tmp%f" |
8a92a2c0 | 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 ]] { | |
c9bdb479 | 181 | _sev_tmp=${$(mktemp 2>/dev/null):-/tmp} |
8a92a2c0 | 182 | [[ -o interactive ]] && |
183 | print -P "%F{red}!!! Can't create tmp dir, using $_sev_tmp%f" | |
184 | } | |
96b7dae3 | 185 | } elif [[ -e $_sev_tmp && ! -h $_sev_tmp ]] { |
186 | # non-softlink node is on our local dir | |
8a92a2c0 | 187 | [[ -o interactive ]] && |
6bbeb528 | 188 | print -P "%F{orange}*** $_sev_tmp exists, can't link to tmp dir $_t, ignoring it%f" |
189 | _sev_tmp=$_t | |
8a92a2c0 | 190 | } else { |
6bbeb528 | 191 | if [[ ! -v $_sev_tmp_keep_link && -h $_sev_tmp && $_sev_tmp:P != $_t:P ]] { |
1a578fd0 | 192 | [[ -o interactive ]] && |
8a92a2c0 | 193 | print -P "%F{orange}*** $_sev_tmp links to ${_sev_tmp:P} and not ${t:P}, unlinking it%f" |
96b7dae3 | 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? | |
8a92a2c0 | 198 | unlink $_sev_tmp 2>/dev/null |
199 | } | |
1a578fd0 | 200 | if [[ ! -e $_sev_tmp ]] { |
201 | # link local dir to tmp dir | |
6bbeb528 | 202 | ln -s $_t $_sev_tmp 2>/dev/null |
1a578fd0 | 203 | } |
8a92a2c0 | 204 | } |
96b7dae3 | 205 | # ensure dir is clean |
206 | _sev_zcleanup tmp | |
207 | # finally create our subdir for this session | |
6bbeb528 | 208 | _t=$_sev_tmp/.session.$$ |
176b807b | 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) { | |
96b7dae3 | 212 | [[ -o interactive ]] && |
6bbeb528 | 213 | print -P "%F{red}!!! Can't create session tmp subdir $_t, using $_sev_tmp%f" |
214 | _t=$_sev_tmp | |
176b807b | 215 | } |
6bbeb528 | 216 | export _sev_tmp TMPDIR=$_t TEMPDIR=$_t TEMP=$_t TMP=$_t TMPPREFIX=$_t/zsh |
217 | unset _t | |
b31a3fb1 | 218 | } |
219 | ||
8a92a2c0 | 220 | ## xdg |
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 | |
227 | ||
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. | |
ef32cb1c | 232 | [[ -f $XDG_CONFIG_HOME/user-dirs.dirs ]] && |
8a92a2c0 | 233 | emulate sh -c "source $XDG_CONFIG_HOME/user-dirs.dirs" |
234 | ||
235 | typeset -UT XDG_DATA_DIRS xdg_data_dirs | |
12843a0c | 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)) | |
8a92a2c0 | 242 | export XDG_DATA_DIRS |
243 | ||
244 | typeset -UT XDG_CONFIG_DIRS xdg_config_dirs | |
12843a0c | 245 | [[ -v XDG_CONFIG_HOME ]] && export XDG_CONFIG_HOME |
246 | [[ -e ${XDG_CONFIG_HOME:-~/.config} ]] || | |
247 | mkdir -m760 ${XDG_CONFIG_HOME:-~/.config} | |
8a92a2c0 | 248 | # I am of the belief .local should follow FHS /usr/local... |
12843a0c | 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)) | |
8a92a2c0 | 253 | export XDG_CONFIG_DIRS |
254 | ||
12843a0c | 255 | [[ -v XDG_STATE_HOME ]] && export XDG_STATE_HOME |
256 | [[ -e ${XDG_STATE_HOME:-~/.local/state} ]] || | |
257 | mkdir -m760 ${XDG_STATE_HOME:-~/.local/state} | |
8a92a2c0 | 258 | |
259 | if [[ -v XDG_CACHE_HOME ]] { | |
260 | export XDG_CACHE_HOME | |
261 | } else { | |
12843a0c | 262 | export XDG_CACHE_HOME=$_sev_tmp/.xdg.cache |
8a92a2c0 | 263 | } |
12843a0c | 264 | [[ -e ${XDG_CACHE_HOME:-~/.cache} ]] || |
265 | mkdir -m700 ${XDG_CACHE_HOME:-~/.cache} | |
8a92a2c0 | 266 | |
12843a0c | 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 | |
8a92a2c0 | 270 | if [[ -v XDG_RUNTIME_DIR ]] { |
8a92a2c0 | 271 | export XDG_RUNTIME_DIR |
12843a0c | 272 | } else { |
8a92a2c0 | 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 | |
280 | } | |
8a92a2c0 | 281 | } |
12843a0c | 282 | [[ ! -e $XDG_RUNTIME_DIR ]] && |
283 | mkdir -m700 $XDG_RUNTIME_DIR 2>/dev/null | |
8a92a2c0 | 284 | |
285 | export _sev_setup_xdg= | |
286 | } | |
287 | ||
288 | ### app setup & exports | |
289 | # NOTE: we set these up here since some scripts might need them | |
290 | ## gpg home | |
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 | |
296 | } | |
297 | } | |
298 | ||
299 | ## perl local lib | |
ec09b7dc | 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) | |
305 | } else { | |
306 | # emulate local::lib if not installed | |
307 | path=($_p5/bin "${path[@]}") | |
308 | export \ | |
309 | PERL_MB_OPT="--install_base '$_p5'" \ | |
310 | PERL_MM_OPT=INSTALL_BASE=$_p5 \ | |
311 | PERL5LIB=$_p5/lib/perl5 \ | |
024170a4 | 312 | PERL_LOCAL_LIB_ROOT=$_p5${PERL_LOCAL_LIB_ROOT:+:$PERL_LOCAL_LIB_ROOT} |
ec09b7dc | 313 | } |
314 | unset _p5 | |
315 | } | |
ef32cb1c | 316 | ## go |
317 | if [[ -v commands[go] ]] { | |
318 | [[ ! -v GOPATH ]] && export GOPATH=${XDG_DATA_HOME:-~/.local/share}/go:~/go | |
319 | [[ ! -v GOBIN ]] && export GOBIN=~/.local/bin | |
320 | } | |
321 | ||
d3e867c0 | 322 | ### plugins |
323 | autoload -Uz load-plugins | |
324 | load-plugins zshenv | |
325 | ||
b31a3fb1 | 326 | ### load zshenv site-specific |
327 | autoload -Uz load-site-dotfile | |
328 | load-site-dotfile zshenv | |
329 | ||
8a92a2c0 | 330 | ### source .zprofile early for non-login shells that should be |
b31a3fb1 | 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 | |
caa1dd46 | 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. | |
b31a3fb1 | 336 | # |
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. | |
341 | # | |
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. | |
345 | ||
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= | |
350 | ||
351 | export _sev_first_display= | |
352 | [[ ! -o login ]] && source ${ZDOTDIR:-~}/.zprofile | |
353 | } elif [[ ${+TERMUX_VERSION} -eq 0 && ! -o login && $SHLVL -eq 1 ]] { | |
8a92a2c0 | 354 | # Termux first process isn't login shell, so source early |
b31a3fb1 | 355 | source ${ZDOTDIR:-~}/.zprofile |
356 | } |