Repository navigation
Expand file tree
/
Copy pathenv
More file actions
609 lines (518 loc) · 16 KB
/
Copy pathenv
File metadata and controls
609 lines (518 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#!/bin/sh
#
# ~/.env: environment variables
export BASH_ENV=${SSHRC:-$HOME}/.bash.env # non-interactive setup
export ENV=${SSHRC:-$HOME}/.shinit # equivalent to .bashrc for sh
# Sane defaults {{{1
#
: ${HOME:=$(cd; pwd)}
: ${LOGNAME:=$(id -un)}
: ${UNAME:=$(uname)}
: ${TMPDIR:=/tmp}
: ${TMP:=$TMPDIR}
export HOME LOGNAME UNAME TMPDIR TMP
# Helper functions {{{1
# NOTE: these are unset at the end of the file
#
# Basic nl(1) for pathuniq {{{2
#
command -v nl >/dev/null || nl() {
n=1
while read -r line; do
printf '\t%s\t%s\n' "$n" "$line"
n=$((n+1))
done
}
# pathadd {{{2
#
# Add <path> to the beginning of <var> (Default: PATH)
# Usage: pathadd <path> [<var>]
#
# Example: $ pathadd ~/share XDG_DATA_DIRS
#
pathadd() {
eval "${2:-PATH}='$1:$(eval echo "\$${2:-PATH}")'"
}
# pathuniq {{{2
#
# Remove duplicate entries from <var> (Default: $PATH)
# Usage: pathuniq [<var>]
#
# Example: $ pathuniq PATH
#
pathuniq() {
eval "${1:-PATH}=$(eval echo "\$${1:-PATH}" |
tr : "\n" |
nl |
sort -uk2,2 |
sort -n |
sed -e 's/^[[:blank:]]*[[:digit:]]*[[:blank:]]*//' \
-e '/^$/d' \
-e 's/^[[:blank:]]*//' |
paste -sd ":" -)"
}
# have {{{2
#
# @description Check PATH for a command
#
# @arg $1 str Command to search for
#
# @exitcode true if found
# @exitcode false if not found
#
have() { command -v "$1" >/dev/null; }
# pick {{{2
#
# @description Wrapper for my that uses defaults
# if my is not installed
#
# @arg $1 str Category to pass to my
# @arg $@ str Alternatives if my is not installed
#
# @stdout Result of my or first alternative found in PATH
pick() {
unset p
have my &&
p=$(my -v "$1" 2>/dev/null)
if [ -z "$p" ]; then
shift
for p in "$@"; do
if have "$p"; then
printf %s "$p"
return
fi
done
else
printf %s "$p"
fi
}
# XDG Directores {{{1
#
# Variable | Purpose | Equivalent | Default
# -------- | ------- | ---------- | -------
# XDG_CONFIG_HOME | config files | /etc | ~/.config
# XDG_CACHE_HOME | non-essential data | /var/cache | ~/.cache
# XDG_DATA_HOME | data files | /usr/share | ~/.local/share
# XDG_STATE_HOME | state data | /var/lib | ~/.local/state
# XDG_LOCAL_HOME | root for other directories | /var | ~/.local
# XDG_LIB_HOME | library files | /usr/lib | ~/.local/lib
# XDG_BIN_HOME | binaries | /usr/bin | ~/.local/bin
# XDG_GAMES_HOME | games | /usr/games | ~/games
# XDG_OPT_HOME | optional packages | /opt | ~/.local/opt
# XDG_SRC_HOME | source code | /usr/src | ~/src
# The ansible baseline playbooks will generate the proper settings
# based on their configuration, but we allow setting the variables
# here in case that file isn't found
#
xdg_env=$HOME/.env.xdg
if [ -r "$xdg_env" ]; then
. "$xdg_env"
# Because $xdg_env is also used by systemd, it cannot export anything
export $(<"$xdg_env" awk -F= '/^#/ { next } /^$/ { next } { print $1 }')
else
export XDG_CONFIG_HOME=${SSHRC:-$HOME}/.config
export XDG_DATA_HOME=${SSHRC:-$HOME}/.local/share
export XDG_CACHE_HOME=${SSHRC:-$HOME}/.cache
export XDG_STATE_HOME=${SSHRC:-$HOME}/.local/state
# These are not real XDG directories
export XDG_LOCAL_HOME=${SSHRC:-$HOME}/.local
export XDG_BIN_HOME=${SSHRC:-$HOME}/.local/bin
export XDG_LIB_HOME=${SSHRC:-$HOME}/.local/lib
export XDG_GAMES_HOME=$HOME/games
export XDG_OPT_HOME=${SSHRC:-$HOME}/.local/opt
export XDG_SRC_HOME=$HOME/src
# Make python use ~/lib instead of ~/.local/lib
[ -z "$SSHRC" ] && export PYTHONUSERBASE=$HOME
fi
unset xdg_env
# Root directory for sh libraries
#
export SH_LIBDIR=$XDG_LIB_HOME/sh
# Set variables for XDG user directories
#
[ -r "$XDG_CONFIG_HOME/user-dirs.dirs" ] && . "$XDG_CONFIG_HOME/user-dirs.dirs"
# Add flatpak to XDG_DATA_DIRS
#
export XDG_DATA_DIRS=${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
if have flatpak; then
for p in \
"$(unset G_MESSAGES_DEBUG; GIO_USE_VFS=local flatpak --installations)" \
"$XDG_DATA_HOME/flatpak" \
; do
pathadd "$p/exports/share" XDG_DATA_DIRS
done
fi
pathuniq XDG_DATA_DIRS
# Setup PATH {{{1
#
# Make sure standard directories are in PATH. Listed in order from least- to most-significant
# NOTE: /{s,}bin is here because some systems do not add it as it is just a symlink to
# /usr/{s,}bin. However, certain (Alpine) LXC containers require it
for d in \
/usr/local/games \
/bin \
/sbin \
/usr/bin \
/usr/sbin \
/usr/local/bin \
/usr/local/sbin \
; do
[ -d "$d" ] && pathadd "$d"
done
# Locale {{{1
#
: "${LANG:=en_US.UTF-8}"
export LANG
# Colors 'n' Stuff {{{1
#
have highlight && export HIGHLIGHT_STYLE='base16/google-dark'
have bat && export BAT_THEME='Solarized (light)'
have pygmentize && export PYGMENTIZE_STYLE='solarized-light'
unset HAS_COLOR
if ! command -v tput >/dev/null || # If tput doesn't exist we yolo it
[ -n "$DISPLAY" ] || [ "$(tput colors)" -ne 0 ]; then
export HAS_COLOR=1
fi
# X-specific {{{2
#
# Select Window Manager or Desktop Environment
#
# Supports:
# - dwm
# - Any WM/DE that is run as a single command with no arguments
#
export WINDOWMANAGER=cwm
# Select Xresources colorscheme
#
# Colorscheme definitions must be placed in
# $HOME/.Xresources.$COLORSCHEME
#
export COLORSCHEME=gnome-dark
# Give Vim access to the terminal colorscheme. See ~/.vimrc
if awk --version 2>/dev/null | grep -q GNU && [ -r "$HOME/.Xresources.$COLORSCHEME" ]; then
export VIM_TERMINAL_COLORS=$(awk '$2 ~ /COLOR/ {print gensub(/COLOR/, "", "1", $2), $3}' \
"$HOME/.Xresources.$COLORSCHEME" |
sort -n |
sed -e '1i[' -e '$a]' -e 's/^/"/' -e 's/$/"/' -e '$!s/$/,/' |
tr -d '\n')
fi
# Default terminal
#
export TERMINAL=$(pick terminal alacritty st gnome-terminal xterm rxvt urxvt)
# Alternatives {{{1
#
# EDITOR/VISUAL {{{2
#
export EDITOR=$(pick editor vim vi nano pico ed)
export VISUAL=$EDITOR
export FCEDIT=$EDITOR
# PAGER/MANPAGER {{{2
#
export PAGER=$(pick pager less most more)
export MANPAGER=$PAGER
if have ack; then
export ACK_PAGER=$PAGER
export ACK_PAGER_COLOR=$PAGER
fi
# Configure pager {{{3
case $PAGER in
vimpager)
export VIMPAGER_RC=${SSHRC:-$HOME}/.vimrc ;;
less)
# NOTE: $LESS_TERMCAP_* variables are set in ~/.shinit
# because *this* file may be sourced *before* TERM is defined
export LESSCHARSET='utf-8';
export LESS='-R --quit-if-one-screen'
# Colorize things if highlight, bat, or pygmentize is available
#
# The '||' here is intentional, see rhbz@1254837
if have highlight; then
export LESSOPEN="||highlight --force -O truecolor --style $HIGHLIGHT_STYLE %s"
elif have bat; then
export LESSOPEN='||bat %s --decorations never --paging never --color=always'
elif have pygmentize; then
export LESSOPEN="||pygmentize -O style=$PYGMENTIZE_STYLE -g %s"
elif [ -x "${lesspipe:=$(command -pv lesspipe.sh)}" ]; then
export LESSOPEN="||$lesspipe %s"
unset lesspipe
fi
;;
esac
# Pass $LESS to MANPAGER if it is less
[ "$MANPAGER" = 'less' ] && MANPAGER="less $LESS"
# Web browser and Search Engine {{{2
#
browsers='elinks links w3m lynx'
[ -n "$DISPLAY" ] &&
browsers="firefox google-chrome chromium netsurf $browsers"
# shellcheck disable=SC2086
export BROWSER=$(pick browser $browsers)
unset browsers
export WWW_SEARCH=www.duckduckgo.com # used in ~/.functions
# ls {{{2
#
# NOTE: Changing the value of LS outside of this file is not supported
export LS=$(pick ls ls eza exa)
case $LS in
eza | exa) export LS_OPTS="--group-directories-first --sort extension -F ${HAS_COLOR:+--color=auto}" ;;
ls) if command ls --version 2>/dev/null | grep -q GNU; then
export LS_OPTS="--group-directories-first -hXF ${HAS_COLOR:+--color=auto}"
else
export LS_OPTS='-XF'
fi ;;
esac
# dircolors {{{1
#
if [ -n "$HAS_COLOR" ] && have dircolors; then
# If colorfile remains blank, dircolors will use defaults
colorfile=
for colorfile in "${SSHRC:-$HOME}/.dir_colors.$TERM" \
"${SSHRC:-$HOME}/.dircolors.$TERM" \
"${SSHRC:-$HOME}/.dir_colors" \
"${SSHRC:-$HOME}/.dircolors" \
"/etc/DIR_COLORS.$TERM" \
/etc/DIR_COLORS \
'' ; do
[ -r "$colorfile" ] && break
done
if [ -n "$colorfile" ]; then
eval "$(dircolors "$colorfile")"
else
eval "$(dircolors)"
fi
unset colorfile
fi
# fzf {{{1
if have fzf; then
# Options for *all* fzf processes
export FZF_DEFAULT_OPTS='
--layout=reverse
--height=50%
--info=inline'
# Options for fzf-tmux
# Open in a floating pane
export FZF_TMUX_OPTS="-p $FZF_DEFAULT_OPTS"
# fzf-tmux height (needed for C-t, C-r and M-c)
export FZF_TMUX_HEIGHT=
# Options for C-r history search
export FZF_CTRL_R_OPTS='--preview-window down:5 --preview "echo {2..}"'
# C-t file search command
export FZF_CTRL_T_COMMAND=
# Options for C-t file search
export FZF_CTRL_T_OPTS='--preview "
if [ -f {} ]; then bat -pp --color always {}
elif [ -d {} ]; then tree -C -L 1 --gitignore --noreport {}
fi"'
# M-c directory search command
export FZF_ALT_C_COMMAND=
# Options for M-c directory search
export FZF_ALT_C_OPTS='--preview "tree -C -L 1 --gitignore --noreport {}"'
have rg &&
export FZF_DEFAULT_COMMAND='rg --files'
fi
# Development {{{1
# Perl {{{2
#
# Setup Perl to use a local lib
perldoc -l local::lib >/dev/null 2>&1 && eval "$(perl -Mlocal::lib=$XDG_LIB_HOME/perl5)"
if have cpanm; then
export PERL_CPANM_HOME=$XDG_CACHE_HOME/cpanm
[ -d "$PERL_CPANM_HOME" ] || mkdir -p "$PERL_CPANM_HOME" >/dev/null
fi
# ARM cross-compiler {{{2
#
[ -d "$HOME/opt/arm-none-eabi/" ] && pathadd "$HOME/opt/arm-none-eabi/bin"
[ -d "$HOME/opt/aarch64-elf/bin/" ] && pathadd "$HOME/opt/aarch64-elf/bin"
# Rust {{{2
#
# Prefer personal rust over system rust
for d in '' "$HOME"; do
[ -d "$d/opt/rust" ] && export RUSTUP_HOME=$d/opt/rust
[ -d "$d/opt/rust/bin" ] && pathadd "$d/opt/rust/bin"
done
[ -d "$XDG_CACHE_HOME/cargo" ] && export CARGO_HOME=$XDG_CACHE_HOME/cargo
[ -d "$HOME/.cargo/bin" ] && pathadd "$HOME/.cargo/bin"
if have podman && have cross; then
export CROSS_CONTAINER_ENGINE=podman
fi
# Packer {{{2
#
export PACKER_CONFIG_DIR=$XDG_STATE_HOME
export PACKER_CONFIG=$XDG_CONFIG_HOME/packerconfig
export PACKER_CACHE_DIR=$XDG_CACHE_HOME/packer
# Go {{{2
#
if [ -d /opt/go ]; then
pathadd /opt/go/bin
export GOROOT=/opt/go
export GOPATH=$HOME # Go packages live in $GOPATH/src
export GOCACHE=$XDG_CACHE_HOME/go-build
export GOMODCACHE=$XDG_CACHE_HOME/go-build/mod
mkdir -p "$GOPATH"
mkdir -p "$GOCACHE"
mkdir -p "$GOMODCACHE"
fi
# gcloud {{{2
#
[ -d /opt/google-cloud-sdk/bin ] && pathadd /opt/google-cloud-sdk/bin
# .NET {{{2
#
if have dotnet; then
# Set location for AppHost lookup
[ -d /usr/lib64/dotnet ] && export DOTNET_ROOT=/usr/lib64/dotnet
# Add dotnet tools directory to PATH
[ -d "$HOME/.dotnet/tools" ] && pathadd "$HOME/.dotnet/tools"
# Extract self-contained executables under HOME
# to avoid multi-user issues from using the default '/var/tmp'.
export DOTNET_BUNDLE_EXTRACT_BASE_DIR="$XDG_CACHE_HOME/dotnet_bundle_extract"
fi
# Other {{{2
#
[ -r "unix://$XDG_RUNTIME_DIR/podman/podman.sock" ] &&
export DOCKER_HOST=unix://$XDG_RUNTIME_DIR/podman/podman.sock
# System specific {{{1
#
case $(uname -o 2>/dev/null || uname -v) in
*) ;;
esac
# Bash-specific {{{1
#
if have bash; then
# Filename completion ignores backups and vim swap files
#
export FIGNORE="~:.swp"
# History stuff
#
export HISTFILE=$XDG_STATE_HOME/misc/bash_history
export HISTSIZE=100 # Max history entries
export HISTFILESIZE=200 # Allow average of two lines per entry
export HISTCONTROL=erasedups # No duplicate history entries
export HISTIGNORE='&:[bf]g:exit' # History ignores these matches
mkdir -p "$(dirname "$HISTFILE")"
# Create nerd-font variables
[ -f "$XDG_DATA_HOME/nerd-font/scripts/i_all.sh" ] &&
. "$XDG_DATA_HOME/nerd-font/scripts/i_all.sh"
fi
# Miscellaneous {{{1
#
# locate(1) {{{2
if have locate; then
export LOCATEDB=$XDG_STATE_HOME/locate/locate.db
mkdir -p "$(dirname "$LOCATEDB")"
fi
# LESSHISTFILE {{{2
LESSHISTFILE=$XDG_STATE_HOME/misc/lesshst
mkdir -p "$(dirname "$LESSHISTFILE")"
# Personal wiki {{{2
#
if [ -d "$HOME/wiki" ]; then
export WIKI_DIR=$HOME/wiki
export WIKI_SITE=$HOME/wiki/site
fi
# DOTFILES {{{2
#
# This is used in vimrc and scripts/curlvim
#
# Get realpath to this file
if command -v readlink >/dev/null; then
export DOTFILES=$(dirname "$(readlink -f "$HOME/.env")")
# Fallback if readlink(1) is not available
elif [ -d "$HOME/src/dotfiles" ]; then
export DOTFILES=$HOME/src/dotfiles
# In the weird case the standard dotfiles path
# is not found, I can live without it
else
:
fi
# Always use my .inputrc {{{2
if [ -r "${SSHRC:-$HOME}/.inputrc" ]; then
export INPUTRC=${SSHRC:-$HOME}/.inputrc
fi
# Set news server {{{2
export NNTPSERVER=snews://news.eternal-september.org
# Set local mail directory {{{2
export MAIL=$HOME/.maildir/local
# Enable pass(1) extensions {{{2
have pass && export PASSWORD_STORE_ENABLE_EXTENSIONS=true
# pinentry stuff {{{2
[ -x "$HOME/bin/pinentry-ssh-askpass" ] &&
export SSH_ASKPASS=$HOME/bin/pinentry-ssh-askpass
[ -n "$DISPLAY" ] &&
export SUDO_ASKPASS=$SSH_ASKPASS
# *grep Colors {{{2
#
# sl matching line (non-matching wwith -v)
# cx context line (matching with -v, non-matching without -v)
# rv reverse sl and cx when using -v (boolean)
# mt set both ms and mc to this value
# ms matching text (without -v)
# mc matching text (in context line wwith -v)
# fn filename
# ln line number
# bn byte offset
# se separators
#
export GREP_COLORS='sl=:cx=:mt=1;31:fn=35:ln=32:bn=33:se=36'
# local-only SSH server {{{2
[ -r "$XDG_CONFIG_HOME/ssh/sshd_config" ] &&
export LOCAL_SSHD_CONFIG=$XDG_CONFIG_HOME/ssh/sshd_config
# Hostnames for bash-completion {{{2
[ -r "$HOME/.ssh/known_hosts" ] && export HOSTFILE=$HOME/.ssh/known_hosts
# rpg-cli {{{2
#
if have rpg-cli; then
# Use cd overloads. Temporarily turn them off by setting this to 0
export RPG_CLI_CD_OVERLOAD=1
fi
# todo.sh {{{2
#
if have todo.sh; then
# Use cd overloads. Temporarily turn them off by setting this to 0
export TODOSH_CD_OVERLOAD=1
# Default action for wrapper
export TODOSH_DEFAULT_ACTION='view project'
fi
# cdlog.sh {{{2
#
export cdlog_path=$SH_LIBDIR/cdlog.sh
export cdlog_autorecover=n # Do not restore cdlog session on login (default)
export cdlog_sess_dir=$XDG_CACHE_HOME/cdlog # Where to store cdlog sessions
# Move things into XDG directories {{{2
have elinks && export ELINKS_CONFDIR=$XDG_CONFIG_HOME/elinks
# Configure tldr-sh client {{{2
#
if have tldr; then
export TLDR_HEADER='underline bold magenta'
export TLDR_QUOTE='white'
export TLDR_DESCRIPTION='italic bold blue'
export TLDR_CODE='bold yellow'
export TLDR_PARAM='reset white'
export TLDR_EPARAM='white'
fi
# grc(1) {{{2
#
have grc && export GRC_OPTS='-es' # colorize stderr and stdout
# Finalize PATH {{{1
#
# Add personal directories. Listed in order from least- to most-significant
for d in \
games/bin \
sbin \
bin/.flatpak \
bin \
; do
[ -d "$HOME/$d" ] && pathadd "$HOME/$d"
done
pathuniq PATH
# Include private and local settings {{{1
#
[ -r "$XDG_CONFIG_HOME/environment" ] &&
. "$XDG_CONFIG_HOME/environment"
[ -r "$HOME/.env.local" ] && . "$HOME/.env.local"
# Cleanup {{{1
#
unset pathadd
unset pathuniq
unset have
unset pick
# vim: foldlevel=0