-
Notifications
You must be signed in to change notification settings - Fork 3
/
commons
430 lines (363 loc) · 15.4 KB
/
commons
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
#!/bin/env bash
# Shebang set to make shellcheck happy.
# Common shelll settings (used by at least my .bashrc and .zshrc)
# Modeline {
# vi: foldmarker={,} filetype=sh foldmethod=marker foldlevel=0 tabstop=8 shiftwidth=8
# }
# Env Bootstrap & XDG {
# Set XDG envvars to default values, so that scripts can refer to them (like in ~/.config/nvim/syntax/)
# Reference: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
# Don't use these direclty, but substitute for default value if empty:
# - ${XDG_CONFIG_HOME:-$HOME/.config}
# - ${XDG_DATA_HOME:-$HOME/.local/share}
# - ${XDG_STATE_HOME:-$HOME/.local/state}
# - ${XDG_CACHE_HOME:-$HOME/.cache}
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
export XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-$HOME/.local/run}" # Need to be set for yarn(1).
# Make programs respect XDG.
export INPUTRC=$XDG_CONFIG_HOME/readline/inputrc
export KDEHOME=$XDG_CONFIG_HOME/kde
export MPLAYER_HOME=$XDG_CONFIG_HOME/mplayer
export SCREENRC=$XDG_CONFIG_HOME/screen/screenrc
export CGDB_DIR=$XDG_CONFIG_HOME/cgdb
export GRADLE_USER_HOME=$XDG_DATA_HOME/gradle
export GRIPHOME=$XDG_CONFIG_HOME/grip
export GTK2_RC_FILES="$XDG_CONFIG_HOME"/gtk-2.0/gtkrc
export SQLITE_HISTORY=$XDG_DATA_HOME/sqlite_history
export XINITRC=$XDG_CONFIG_HOME/X11/xinitrc
export ACKRC=$XDG_CONFIG_HOME/ack/ackrc # Project local .ackrc is still possible.
export WGETRC=$XDG_CONFIG_HOME/wget/wgetrc
export GDBHISTFILE=$XDG_DATA_HOME/gdb/history
export SOLARGRAPH_CACHE=$XDG_CACHE_HOME/solargraph
export TRAVIS_CONFIG_PATH=$XDG_CONFIG_HOME/travis
export RIPGREP_CONFIG_PATH=$XDG_CONFIG_HOME/ripgrep/config
export FZF_MARKS_FILE=$XDG_CONFIG_HOME/fzf-marks/marks
export GNUPGHOME="$XDG_DATA_HOME"/gnupg
export DOCKER_CONFIG="$XDG_CONFIG_HOME"/docker
export ANDROID_HOME="$XDG_DATA_HOME"/android
export NODE_REPL_HISTORY="$XDG_DATA_HOME"/node_repl_history
# asdf; Not yet fully compliant: https://github.com/asdf-vm/asdf/issues/687
export ASDF_CONFIG_FILE=${XDG_CONFIG_HOME}/asdf/asdfrc
export ASDF_DATA_DIR=${XDG_DATA_HOME}/asdf
# Hack for avoiding having $HOME/.tools-version: https://github.com/asdf-vm/asdf/issues/1248#issuecomment-1155978678
export ASDF_DEFAULT_TOOL_VERSIONS_FILENAME=.local/share/asdf/tool-versions
# asdf plugins:
export ASDF_PYTHON_DEFAULT_PACKAGES_FILE=${XDG_CONFIG_HOME}/pip/asdf-default-python-packages.txt
export ASDF_NPM_DEFAULT_PACKAGES_FILE=${XDG_CONFIG_HOME}/npm/asdf-default-npm-packages.txt
export ASDF_GEM_DEFAULT_PACKAGES_FILE=${XDG_CONFIG_HOME}/gem/asdf-default-gems.txt
export ASDF_GOLANG_DEFAULT_PACKAGES_FILE=${XDG_CONFIG_HOME}/golang/asdf-default-golang-pkgs.txt
# Octave
export OCTAVE_SITE_INITFILE=$XDG_CONFIG_HOME/octave/octaverc
export OCTAVE_HISTFILE=$XDG_CACHE_HOME/octave-hsts
# XCompose
export XCOMPOSECACHE=$XDG_CACHE_HOME/X11/XCompose
export XCOMPOSEFILE=$XDG_CONFIG_HOME/X11/XCompose
# Gems
#export GEM_SPEC_CACHE=$XDG_CACHE_HOME/gem # TODO maybe also not needed, try disable to prevent ruby build issues.
# NOTE GEM_HOME cause issues (openssl lib can't be compiled) when using rbevn to install new ruby versions. https://github.com/asdf-vm/asdf-ruby/issues/206#issuecomment-860106503
# It might not be necessary to do this when using asdf, as gems are installed to asdf version dirs.
#export GEM_HOME=$XDG_DATA_HOME/gem
#export PATH="$GEM_HOME/bin:$PATH"
# Bundle
export BUNDLE_USER_CONFIG=$XDG_CONFIG_HOME/bundle/config
export BUNDLE_USER_CACHE=$XDG_CACHE_HOME/bundle
export BUNDLE_USER_PLUGIN=$XDG_DATA_HOME/bundle
# NPM
export NPM_CONFIG_USERCONFIG=$XDG_CONFIG_HOME/npm/npmrc
export PATH="$XDG_DATA_HOME/npm/bin:$PATH"
# Mailcap. Seems like can use envvar at least for NeoMutt. Ref: https://manpages.debian.org/testing/neomutt/neomutt.1.en.html
export MAILCAPS="$XDG_DATA_HOME:$MAILCAPS"
# AWS cli
#export AWS_SHARED_CREDENTIALS_FILE="$XDG_CONFIG_HOME"/aws/credentials
#export AWS_CONFIG_FILE="$XDG_CONFIG_HOME"/aws/config
# Load utility functions
source "${XDG_CONFIG_HOME:-$HOME/.config}/shell/functions"
# }
# Paths {
# Make the path available in subshells. Export is only needed once.
export PATH
# Include system binaries.
PATH="/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin:$PATH"
# Include binaries in home directory.
PATH="$HOME/bin:$PATH"
# E.g. poetry installs here.
test -d $HOME/.local/bin && PATH="$HOME/.local/bin:$PATH"
# Homebrew installation path prefix - do this once, instead of slowing down shell everywhere.
brew_bin=
if [ -e /opt/homebrew/bin/brew ]; then # Apple Silicon macs
brew_bin=/opt/homebrew/bin/brew
elif [ -e /usr/local/bin/brew ]; then # Intel Macs
brew_bin=/usr/local/bin/brew
elif [ -e /home/linuxbrew/.linuxbrew/bin/brew ]; then # Linux
brew_bin=/home/linuxbrew/.linuxbrew/bin/brew
fi
if [ -n "$brew_bin" ]; then
eval "$(${brew_bin} shellenv)"
fi
unset brew_bin
# }
# Environment {
# Use THE text editor
if program_is_in_path nvim; then
export EDITOR=nvim VISUAL=nvim CSHEDIT=nvim
elif program_is_in_path vim; then
export EDITOR=vim VISUAL=vim CSHEDIT=vim
fi
# and the only pager.
export PAGER=less
# Current DE in use.
if shell_is_macos; then
export DESKTYPE=macos
else
# LINUX-CONFIG
# FREEBSD-CONFIG
export DESKTYPE=dwm
fi
# Locale.
# Needs to include encoding as well for iTerm: https://gitlab.com/gnachman/iterm2/-/issues/10879#note_1433417922
export LANG=en_US.UTF-8
# Let others know what underlying terminal emulator is used. This is needed since $TERM does not always represent the real terminal e.g. in tmux when you want colors.
shell_is_linux && program_is_in_path urxvt && export TERMEMU=urxvt
# Brew bundler
if program_is_in_path brew; then
# Not using the Brewfile.lock.json feature.
export HOMEBREW_BUNDLE_NO_LOCK=1
fi
# A better compiler for C langs.
# Unfortunately still quite some problems e.g. with compiling native extension of $(gem install byebuy)
#if program_is_in_path clang; then
#export CC=clang
#export CXX=clang++
#fi
# Personal log folder used by some program configurations.
test -d ${XDG_STATE_HOME:-$HOME/.local/state}/tmux || mkdir -p ${XDG_STATE_HOME:-$HOME/.local/state}/tmux
#test -d ${XDG_STATE_HOME:-$HOME/.local/state}/irssi || mkdir -p ${XDG_STATE_HOME:-$HOME/.local/state}/irssi
# Enable sandboxing a.k.a. lazy loading of shell initialization for some programs.
#sourceifexists "$HOME/.local/repos/sandboxd/sandboxd"
# Needed for gnupg's gpg(1) to work, thus for git commit signing.
program_is_in_path gnupg && export GPG_TTY=$(tty)
# Nice to have when fetching scanned documents
export SCANNED=$HOME/media/images/scanned/
# Ditto screenshots
export SCREENSHOTS=$HOME/media/images/screenshots/
# Disable macos shell restore feature (/etc/bashrc_Apple_Terminal) that creates e.g. ~/.zsh_session
shell_is_macos && export SHELL_SESSIONS_DISABLE=1
# Speed up dfm by telling it where the source is
test -d "$HOME/src/github.com/erikw/dotfiles" && export DFM_REPO="$HOME/src/github.com/erikw/dotfiles"
# Source aliases (must be done after setting $DESKTYPE)
sourceifexists "${XDG_CONFIG_HOME:-$HOME/.config}/shell/aliases"
# }
# UI {
# Solarized ls colors.
dircolorsdb=$HOME/.local/repos/dircolors-solarized/dircolors.256dark
if ! [ -f "$dircolorsdb" ]; then
dircolorsdb=${XDG_CONFIG_HOME:-$HOME/.config}/shell/dircolors
fi
if program_is_in_path dircolors; then
eval "$(dircolors -b $dircolorsdb)"
elif shell_is_macos && program_is_in_path gdircolors; then
eval "$(gdircolors -b $dircolorsdb)"
fi
unset dircolorsdb
# Colorize ls(1) on BSD/Mac systems.
shell_is_bsd && export CLICOLOR=1
# Use colorful terminal.
case "$TERM" in
xterm*) export TERM=xterm-256color ;;
screen*) export TERM=screen-256color ;;
rxvt*) export TERM=rxvt-unicode-256color ;;
esac
# Colored man pages. Reference: https://wiki.archlinux.org/index.php/Man_Page#Colored_man_pages
# But only if we don't use batman (alias set in ~/.config/shell/aliases).
#if ! program_is_in_path batman; then
function man() {
env \
LESS_TERMCAP_mb=$(printf "\e[1;31m") \
LESS_TERMCAP_md=$(printf "\e[1;31m") \
LESS_TERMCAP_me=$(printf "\e[0m") \
LESS_TERMCAP_se=$(printf "\e[0m") \
LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
LESS_TERMCAP_ue=$(printf "\e[0m") \
LESS_TERMCAP_us=$(printf "\e[1;32m") \
man "$@"
}
#fi
# }
# Programs {
# Go {
#export GOPATH="$XDG_DATA_HOME/go"
export GOPATH="$HOME/src/go"
if [ -d "$GOPATH/bin" ]; then
PATH="$GOPATH/bin::$PATH"
fi
#if [ -d "$HOME/src/github.com/golang/go" ]; then
# export GOROOT="$HOME/src/golang/go"
# PATH="$GOROOT/bin:$GOROOT/pkg/tool/linux_amd64:$PATH"
#fi
# }
# Java {
#if [ -e /usr/libexec/java_home ] ; then
#export JAVA_HOME=$(/usr/libexec/java_home 2>/dev/null)
#test $? -eq 0 || unset JAVA_HOME
#fi
# Add ANSI color output to ant.
#if program_is_in_path ant; then
#export ANT_ARGS='-logger org.apache.tools.ant.listener.AnsiColorLogger'
#fi
# Set $JAVA_HOME from asdf. Reference: https://github.com/halcyon/asdf-java
sourceifexists "${XDG_DATA_HOME:-$HOME/.local/share}/asdf/plugins/java/set-java-home.$SHELL_NAME"
# }
# less {
# Syntax highlighting for less with src-highlight.
# Normal highlight.
#if type src-hilite-lesspipe.sh >/dev/null 2>&1; then
#export LESSOPEN="| src-hilite-lesspipe.sh %s"
#fi
if program_is_in_path source-highlight; then
export LESSOPEN="| $(which src-hilite-lesspipe.sh) %s"
# Solarized version, as the normal version does not work (hidden text) when solarized terminal background is used.
# https://github.com/jrunning/source-highlight-solarized
#export LESSOPEN="| source-highlight --failsafe -f esc-solarized --style-file=esc-solarized.style -i %s -o STDOUT"
fi
# --RAW-CONTROL-CHARS" - Display colors.
# --ignore-case" - Case insensitive search. However using capital letter(s) will enable case sensitive search.
# --status-column" - Status column with number of lines etc.
# --LINE-NUMBERS" - Show line numbers.
export LESS="--RAW-CONTROL-CHARS --ignore-case --status-column"
# }
# Ruby {
# ruby-build: recommended build env.
# Reference: https://github.com/rbenv/ruby-build/wiki#suggested-build-environment
#export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$(brew --prefix [email protected])"
if program_is_in_path ruby-build || program_is_in_path asdf ; then
# For Ruby versions 2.x-3.0
#export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$HOMEBREW_PREFIX/opt/[email protected]"
# For Ruby versions >=3.1
export RUBY_CONFIGURE_OPTS="--with-openssl-dir=$HOMEBREW_PREFIX/opt/openssl@3"
fi
# }
# Python {
# Python pip binary installation directory (pip3 install --user)
if shell_is_macos; then
PATH="$HOME/Library/Python/3.9/bin:$PATH"
fi
# }
# iTerm2 shell integration. Reference: https://www.iterm2.com/documentation-shell-integration.html
#sourceifexists $HOME/.iterm2_shell_integration.$SHELL_NAME
# Start SSH agent, unless this is an SSH session already
# Ref: https://stackoverflow.com/a/18915067
if [ -z "$SSH_CLIENT" ]; then
SSH_ENV="$HOME/.ssh/env"
function start_agent {
#echo -n "Initialising new SSH agent... "
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
#echo succeeded
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add;
}
# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
#ps ${SSH_AGENT_PID} doesn't work under cywgin
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi
fi
# Add tab completion to daemonize.
if program_is_in_path daemonize; then
$completion_func -cf daemonize
fi
# Tab complete for viw.
if program_is_in_path viw; then
$completion_func -cf viw
fi
# Android SDK
#if [ -d "$HOME/src/android-sdk-linux/" ]; then
#PATH="$HOME/src/android-sdk-linux/tools/:$HOME/src/android-sdk-linux/platform-tools/:$PATH"
#fi
# Vim XDG. Ref: https://wiki.archlinux.org/title/XDG_Base_Directory
export GVIMINIT='let $MYGVIMRC = !has("nvim") ? "$XDG_CONFIG_HOME/vim/gvimrc" : "$XDG_CONFIG_HOME/nvim/init.gvim" | so $MYGVIMRC'
export VIMINIT='let $MYVIMRC = !has("nvim") ? "$XDG_CONFIG_HOME/vim/vimrc" : "$XDG_CONFIG_HOME/nvim/init.lua" | so $MYVIMRC'
# Perl
# cpan(1): avoid interactive configuration on first launch.
export PERL_MM_USE_DEFAULT=1
# Custom locallib installation path for modules
# Ref: https://stackoverflow.com/a/41541273/265508
export PERL_BASE="$XDG_DATA_HOME/perl5"
export PERL_MM_OPT="INSTALL_BASE=$PERL_BASE"
export PERL_MB_OPT="--install_base $PERL_BASE"
export PERL5LIB="$PERL_BASE/lib/perl5"
export PATH="$PERL_BASE/bin:$PATH"
export MANPATH="$PERL_BASE/man:$MANPATH"
# Load custom keymap
# NOTE allow me or %wheeler to issue loadkeys with no password.
#if shell_is_linux && [ -f "${XDG_CONFIG_HOME:-$HOME/.config}/loadkeys/keymap" ]; then
#sudo loadkeys -q ${XDG_CONFIG_HOME:-$HOME/.config}/loadkeys/keymap &>/dev/null
#fi
# Must be at the end of shell init file. but here should do...
if program_is_in_path sdk; then
export SDKMAN_DIR="$HOME/.sdkman"
[[ -s "$SDKMAN_DIR/bin/sdkman-init.sh" ]] && source "$SDKMAN_DIR/bin/sdkman-init.sh"
fi
# Add MacTex binaries (macos) to PATH.
# Reference: http://rkrug.github.io/LMS-test/downloads/UpdatingForElCapitan.pdf
test -e /Library/TeX/texbin && PATH="/Library/TeX/texbin:$PATH"
# ghq
# https://github.com/motemen/ghq
# Set envvar to my github clones directory, use same base path as
# $(git config --global ghq.root)
export GITHUB=$HOME/src/github.com/erikw
# Travis cli client. https://github.com/travis-ci/travis.rb
sourceifexists $HOME/.travis/travis.sh
# broot - https://dystroy.org/broot/install-br/
sourceifexists "$HOME/.config/broot/launcher/bash/br"
# fzf https://github.com/junegunn/fzf#using-homebrew
if program_is_in_path fzf; then
sourceifexists ${XDG_CONFIG_HOME:-$HOME/.config}/fzf/fzf.${SHELL_NAME}
# Default cli options. See fzf(1)
export FZF_COMPLETION_OPTS='--multi'
# Find dot files as well. Reference: https://github.com/junegunn/fzf/issues/634
if program_is_in_path fd; then
#export FZF_DEFAULT_COMMAND="rg --hidden --files --glob '!{.git,node_modules}/'"
#export FZF_DEFAULT_COMMAND='fd --type file --hidden --follow --exclude node_modules'
export FZF_DEFAULT_COMMAND='fd --type file --hidden --follow'
else
export FZF_DEFAULT_COMMAND='find . -type d \( -path './.git' -o -path './node_modules' \) -prune -o -print'
fi
export FZF_CTRL_T_COMMAND="$FZF_DEFAULT_COMMAND"
# To exclude path:
#export FZF_DEFAULT_COMMAND="$FZF_DEFAULT_COMMAND --exclude 'vcr_cassettes/'"
export FZF_DEFAULT_COMMAND="$FZF_DEFAULT_COMMAND --exclude '.git/'"
fi
# Homebrew sqlite to shadow old version shipped with macOS.
if [ -d "$HOMEBREW_PREFIX/opt/sqlite/bin" ]; then
PATH="$HOMEBREW_PREFIX/opt/sqlite/bin:$PATH"
fi
# Homebrew curl
if [ -d "$HOMEBREW_PREFIX/opt/curl/bin" ]; then
PATH="$HOMEBREW_PREFIX/opt/curl/bin:$PATH"
fi
# asdf
if program_is_in_path asdf; then
if [ -n "$HOMEBREW_PREFIX" ]; then
source "$HOMEBREW_PREFIX/opt/asdf/libexec/asdf.sh"
# Hide asdf from brew, otherwise brew-doctor will complain on python-config files in PATH.
# Reference: https://github.com/pyenv/pyenv#homebrew-in-macos
# NOTE seems not needed anymore?
#shell_is_macos && alias brew="env PATH=${PATH//$ASDF_DATA_DIR\/shims:/} brew"
elif shell_is_linux; then
source /opt/asdf-vm/asdf.sh
fi
fi
# direnv: https://direnv.net/
if program_is_in_path direnv; then
eval "$(direnv hook $SHELL_NAME)"
fi
# }