]>
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 { | |
6d4ce4aa | 6 | local x p y |
7 | ||
8a92a2c0 | 8 | # gpg forwarding |
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 | | |
12 | while {read -r x} { | |
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. | |
18 | p=$(basename $x) | |
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 | |
23 | unlink $y | |
24 | } | |
25 | # don't force in case something important is still there | |
26 | rmdir $x | |
27 | } | |
28 | } | |
29 | # reset GNUPGHOME if we removed our own dir | |
6d4ce4aa | 30 | if [[ $GNUPGHOME =~ '/.ssh_forward/\d+/*$' && ! -d $GNUPGHOME ]] |
8a92a2c0 | 31 | GNUPGHOME=${GNUPGHOME%$MATCH} |
32 | } | |
b31a3fb1 | 33 | |
8a92a2c0 | 34 | # custom tmp |
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 | | |
42 | while {read -r x} { | |
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} { | |
47 | rm -rf $x | |
48 | } | |
49 | } | |
b31a3fb1 | 50 | } |
51 | } | |
52 | ||
8a92a2c0 | 53 | function _sev_setpath { |
b31a3fb1 | 54 | # add as many generic paths as possible to keep the order we want |
e7e78648 | 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 | |
b31a3fb1 | 69 | # XXX: XDG specifies ~/.local/bin as the only user-writable dir for |
70 | # executables, but we specify more; technically this is against spec | |
e7e78648 | 71 | path=({{${_sev_home:-~},~}{/.local,},{$PREFIX,}{,/usr{,/local,/pkg},/opt{,/*{/*,}}}}/{s,}bin(N) |
8a92a2c0 | 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) | |
6d4ce4aa | 75 | local -i i len=$#path |
b31a3fb1 | 76 | path=("$path[@]" "$syspath[@]") |
77 | # remove bad paths | |
78 | for (( i = 1; i <= $#path; i++ )) { | |
79 | if [[ ! -d $path[$i] ]] { | |
80 | path[$i]=() | |
81 | ((i <= len)) && ((len--)) | |
82 | ((i--)) | |
83 | continue | |
84 | } | |
85 | } | |
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]}") | |
b31a3fb1 | 88 | # include our zsh dir in fpath. unlike above, we always prefer our paths |
89 | fpath=({${ZDOTDIR:-~/.zsh},{${_sev_home:-~},~}/.zsh}/functions/**/*(/N) "$fpath[@]") | |
8a92a2c0 | 90 | # remove bad paths |
91 | for (( i = 1; i <= $#fpath; i++ )) { | |
92 | if [[ ! -d $fpath[$i] ]] { | |
93 | fpath[$i]=() | |
94 | ((i--)) | |
95 | continue | |
96 | } | |
97 | } | |
b31a3fb1 | 98 | # FPATH is not exported by default |
99 | export FPATH | |
e7e78648 | 100 | # un-unique system arrays for consistency |
b31a3fb1 | 101 | typeset +U path fpath |
8a92a2c0 | 102 | } |
103 | ||
104 | ### common exports | |
105 | export CHARSET=${CHARSET:-UTF-8} | |
106 | export LANG=${LANG:-en_US.UTF-8} | |
107 | ||
108 | ## alternative home for pulling in bin & config, used for zsu | |
109 | [[ -v _sev_home ]] || export _sev_home=$HOME | |
110 | ||
111 | ## fix broken term | |
294ed44e | 112 | # NOTE: we do this here instead of .zshrc since we might print stuff |
8a92a2c0 | 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 ]] { | |
116 | _oldterm=$TERM | |
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" | |
120 | unset _oldterm | |
121 | } | |
122 | } | |
123 | ||
124 | ## path | |
125 | if [[ ! -v _sev_setup_path || -o login ]] { | |
126 | _sev_setpath | |
127 | # NOTE: do not set _sev_setup_path, it is set in zprofile | |
128 | } | |
129 | ||
130 | ### home dir setup & exports | |
131 | # XXX: traditionally, zshenv should just contain exports, and not touch the | |
132 | # filesystem. however, some system profile scripts that are sourced in the | |
133 | # system zprofile may attempt to do things that rely on some of these | |
134 | # vars. for example, `flatpak-bindir.sh` in the Arch Linux flatpak package | |
135 | # references $XDG_DATA_HOME with no fallback. since we do special handling | |
136 | # for these vars before we export them, we're forced to do it all here | |
137 | # instead of zprofile. | |
138 | ||
139 | ## xdg local dir | |
140 | # NOTE: need this for tmp, so confirm it exists. | |
141 | # XXX: perms are not specified for XDG dirs except runtime, but I think 760 | |
142 | # makes the most sense. shouldn't break anything since no one else should | |
143 | # be poking around in our dir. | |
144 | [[ -e ~/.local ]] || mkdir -m760 ~/.local | |
145 | ||
146 | ## tmp | |
147 | # NOTE: specs say that POSIX tmp and XDG runtime directories should exist | |
148 | # until the last session is logged out (POSIX can exist for longer). | |
149 | # since we can't reliably keep track of sessions in a cross-platform | |
150 | # manner, the current implementation should use a separate directory per | |
151 | # toplevel session (i.e. SHLVL=1). this should placate most applications, | |
152 | # though it is not expressly spec compliant. this may also cause problems | |
153 | # with disowned applications that still try to use the directories after | |
154 | # the toplevel shell has already logged out and the dirs removed, but the | |
155 | # chances of that are slim. | |
156 | if [[ ! -v _sev_tmp ]] { | |
96b7dae3 | 157 | _sev_tmp=~/.local/tmp |
8a92a2c0 | 158 | # create personal TMPDIR under system tmp |
70c91a22 | 159 | # NOTE: under proot with uid remapping, we can reuse old tmp, without |
160 | # worrying about permission issues; intended for proot under termux. | |
161 | # XXX: _sev_proot_old_user only works if /tmp is shared! | |
162 | t=${TMPDIR:-${TEMPDIR:-${TEMP:-${TMP:-${${TMPPREFIX%/zsh}:-/tmp}}}}}/.home-${_sev_proot_old_username:-$LOGNAME} | |
8a92a2c0 | 163 | [[ -e $t ]] || mkdir -m700 $t 2>/dev/null |
8a92a2c0 | 164 | if [[ ! -d $t ]] { |
165 | # fallback TMPDIR to bare local directory or existing softlink | |
166 | [[ -o interactive ]] && | |
167 | print -P "%F{orange}*** Can't create tmp dir $t, using $_sev_tmp%f" | |
168 | [[ -h $_sev_tmp && ! -d _sev_tmp ]] && unlink $_sev_tmp 2>/dev/null | |
169 | [[ ! -e $_sev_tmp ]] && mkdir -m700 $_sev_tmp 2>/dev/null | |
170 | if [[ ! -d $_sev_tmp ]] { | |
c9bdb479 | 171 | _sev_tmp=${$(mktemp 2>/dev/null):-/tmp} |
8a92a2c0 | 172 | [[ -o interactive ]] && |
173 | print -P "%F{red}!!! Can't create tmp dir, using $_sev_tmp%f" | |
174 | } | |
96b7dae3 | 175 | } elif [[ -e $_sev_tmp && ! -h $_sev_tmp ]] { |
176 | # non-softlink node is on our local dir | |
8a92a2c0 | 177 | [[ -o interactive ]] && |
178 | print -P "%F{orange}*** $_sev_tmp exists, can't link to tmp dir $t, ignoring it%f" | |
179 | _sev_tmp=$t | |
180 | } else { | |
96b7dae3 | 181 | if [[ -h $_sev_tmp ]] { |
182 | [[ -o interactive && $_sev_tmp:P != $t:P ]] && | |
8a92a2c0 | 183 | print -P "%F{orange}*** $_sev_tmp links to ${_sev_tmp:P} and not ${t:P}, unlinking it%f" |
96b7dae3 | 184 | # NOTE: ln -f doesn't seem to work reliably with softlink |
185 | # directories, so explicitly remove the target if it exists | |
186 | # XXX: potential race condition | |
187 | # TODO: handle cleanup of old dir if it doesn't match? | |
8a92a2c0 | 188 | unlink $_sev_tmp 2>/dev/null |
189 | } | |
96b7dae3 | 190 | # link local dir to tmp dir |
8a92a2c0 | 191 | ln -s $t $_sev_tmp 2>/dev/null |
192 | } | |
96b7dae3 | 193 | # ensure dir is clean |
194 | _sev_zcleanup tmp | |
195 | # finally create our subdir for this session | |
196 | t=$_sev_tmp/.session.$$ | |
197 | if ! mkdir -m700 $t 2>/dev/null; then | |
198 | [[ -o interactive ]] && | |
199 | print -P "%F{red}!!! Can't create session tmp subdir $t, using $_sev_tmp%f" | |
200 | t=$_sev_tmp | |
201 | fi | |
202 | export _sev_tmp TMPDIR=$t TEMPDIR=$t TEMP=$t TMP=$t TMPPREFIX=$t/zsh | |
8a92a2c0 | 203 | unset t |
b31a3fb1 | 204 | } |
205 | ||
8a92a2c0 | 206 | ## xdg |
207 | if [[ ! -v _sev_setup_xdg ]] { | |
208 | ## merge with any existing dirs and remove duplicates using unique arrays | |
209 | # NOTE: we are accepting whatever value might be set for CONFIG and DATA; | |
210 | # if it wasn't set, we just use default and leave it unset | |
211 | # NOTE: include and then remove CONFIG_HOME and DATA_HOME to ensure they | |
212 | # are not present in the array if it was added before we got to it | |
213 | ||
214 | # source user dirs before other vars; technically it is against spec to | |
215 | # include any of the below dirs there, but you never know what crazy shit | |
216 | # people will do. I rather handle them sanely with our own code than let | |
217 | # them override after the fact. | |
ef32cb1c | 218 | [[ -f $XDG_CONFIG_HOME/user-dirs.dirs ]] && |
8a92a2c0 | 219 | emulate sh -c "source $XDG_CONFIG_HOME/user-dirs.dirs" |
220 | ||
221 | typeset -UT XDG_DATA_DIRS xdg_data_dirs | |
12843a0c | 222 | [[ -v XDG_DATA_HOME ]] && export XDG_DATA_HOME |
223 | [[ -e ${XDG_DATA_HOME:-~/.local/share} ]] || | |
224 | mkdir -m760 ${XDG_DATA_HOME:-~/.local/share} | |
225 | xdg_data_dirs=($XDG_DATA_HOME ${XDG_DATA_DIRS:+${xdg_data_dirs%%/}} | |
226 | /{usr{,/local,/pkg},opt{,/*{/*,}}}/share(N)) | |
227 | xdg_data_dirs=($xdg_data_dirs(/N)) | |
8a92a2c0 | 228 | export XDG_DATA_DIRS |
229 | ||
230 | typeset -UT XDG_CONFIG_DIRS xdg_config_dirs | |
12843a0c | 231 | [[ -v XDG_CONFIG_HOME ]] && export XDG_CONFIG_HOME |
232 | [[ -e ${XDG_CONFIG_HOME:-~/.config} ]] || | |
233 | mkdir -m760 ${XDG_CONFIG_HOME:-~/.config} | |
8a92a2c0 | 234 | # I am of the belief .local should follow FHS /usr/local... |
12843a0c | 235 | [[ -e ~/.local/etc ]] || ln -s ${XDG_CONFIG_HOME:-~/.config} ~/.local/etc |
236 | xdg_config_dirs=($XDG_CONFIG_HOME ${XDG_CONFIG_DIRS:+${xdg_config_dirs%%/}} | |
237 | {,/usr{,/local,/pkg},opt{,/*{/*,}}}/etc/xdg(N)) | |
238 | xdg_config_dirs=($xdg_config_dirs(/N)) | |
8a92a2c0 | 239 | export XDG_CONFIG_DIRS |
240 | ||
12843a0c | 241 | [[ -v XDG_STATE_HOME ]] && export XDG_STATE_HOME |
242 | [[ -e ${XDG_STATE_HOME:-~/.local/state} ]] || | |
243 | mkdir -m760 ${XDG_STATE_HOME:-~/.local/state} | |
8a92a2c0 | 244 | |
245 | if [[ -v XDG_CACHE_HOME ]] { | |
246 | export XDG_CACHE_HOME | |
247 | } else { | |
12843a0c | 248 | export XDG_CACHE_HOME=$_sev_tmp/.xdg.cache |
8a92a2c0 | 249 | } |
12843a0c | 250 | [[ -e ${XDG_CACHE_HOME:-~/.cache} ]] || |
251 | mkdir -m700 ${XDG_CACHE_HOME:-~/.cache} | |
8a92a2c0 | 252 | |
12843a0c | 253 | # NOTE: this can be set by systemd or other pre-shell supervisor, and if |
254 | # any services were started such as pipewire, we need to use the | |
255 | # existing runtime dir to preserve runtime state | |
8a92a2c0 | 256 | if [[ -v XDG_RUNTIME_DIR ]] { |
8a92a2c0 | 257 | export XDG_RUNTIME_DIR |
12843a0c | 258 | } else { |
8a92a2c0 | 259 | # make runtime dir in our session-specific tmpdir |
260 | export XDG_RUNTIME_DIR=$TMPDIR/.xdg.runtime | |
261 | # same as in tmpdir creation, ensure dir doesn't exist | |
262 | if [[ -h $XDG_RUNTIME_DIR ]] { | |
263 | unlink $XDG_RUNTIME_DIR 2>/dev/null | |
264 | } elif [[ -e $XDG_RUNTIME_DIR ]] { | |
265 | rm -rf $XDG_RUNTIME_DIR 2>/dev/null | |
266 | } | |
8a92a2c0 | 267 | } |
12843a0c | 268 | [[ ! -e $XDG_RUNTIME_DIR ]] && |
269 | mkdir -m700 $XDG_RUNTIME_DIR 2>/dev/null | |
8a92a2c0 | 270 | |
271 | export _sev_setup_xdg= | |
272 | } | |
273 | ||
274 | ### app setup & exports | |
275 | # NOTE: we set these up here since some scripts might need them | |
276 | ## gpg home | |
277 | if [[ ! -v GNUPGHOME ]] { | |
278 | export GNUPGHOME=${XDG_CONFIG_HOME:-~/.config}/gnupg | |
279 | # move existing gnupg dir to our new home | |
280 | if [[ -d ~/.gnupg && ! -d $GNUPGHOME ]] { | |
281 | mv ~/.gnupg $GNUPGHOME | |
282 | } | |
283 | } | |
284 | ||
285 | ## perl local lib | |
286 | [[ ! -v PERL_LOCAL_LIB_ROOT && -v commands[perl] && | |
287 | -d $XDG_DATA_HOME/perl5/lib/perl5 ]] && | |
288 | eval $(perl -I$XDG_DATA_HOME/perl5/lib/perl5 \ | |
289 | -Mlocal::lib=$XDG_DATA_HOME/perl5 2>/dev/null) | |
290 | ||
ef32cb1c | 291 | ## go |
292 | if [[ -v commands[go] ]] { | |
293 | [[ ! -v GOPATH ]] && export GOPATH=${XDG_DATA_HOME:-~/.local/share}/go:~/go | |
294 | [[ ! -v GOBIN ]] && export GOBIN=~/.local/bin | |
295 | } | |
296 | ||
b31a3fb1 | 297 | ### load zshenv site-specific |
298 | autoload -Uz load-site-dotfile | |
299 | load-site-dotfile zshenv | |
300 | ||
8a92a2c0 | 301 | ### source .zprofile early for non-login shells that should be |
b31a3fb1 | 302 | if [[ ! -v _sev_first_display && ( -v DISPLAY || -v WAYLAND_DISPLAY ) ]] { |
303 | # most graphical login/session managers will spawn the user's shell as a | |
304 | # parent of all child processes for that session. however, if the parent shell | |
305 | # isn't a login shell for some reason, our .zprofile won't be run, and the | |
306 | # environment won't be configured for child processes. | |
307 | # | |
308 | # XXX: .zprofile will be sourced by every new child shell if zsh is not | |
309 | # used to start the graphical session and the _sev_first_display var | |
310 | # isn't exported; for example, this previously happened when using | |
311 | # sway without a display manager in front of it to run a login shell. | |
312 | # | |
313 | # this issue is not mitigated by .zprofile only loading what has not | |
314 | # already been loaded if the env vars preventing the load are not set; | |
315 | # in that case, every shell will think it is a fresh login shell. | |
316 | ||
317 | # update gpgagent to use graphical pinentry | |
318 | # XXX: will steal display from any other logged in graphical sessions, but | |
319 | # I consider this to be an unlikely scenario | |
320 | _sev_refresh_gpgagent= | |
321 | ||
322 | export _sev_first_display= | |
323 | [[ ! -o login ]] && source ${ZDOTDIR:-~}/.zprofile | |
324 | } elif [[ ${+TERMUX_VERSION} -eq 0 && ! -o login && $SHLVL -eq 1 ]] { | |
8a92a2c0 | 325 | # Termux first process isn't login shell, so source early |
b31a3fb1 | 326 | source ${ZDOTDIR:-~}/.zprofile |
327 | } |