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