瀏覽代碼

refactor(zsh): use `$DOTFILES_DIR` in place of `$HOME` and set with `launchctl`

Joe 9 月之前
父節點
當前提交
675694bad1

+ 3 - 3
.config/alacritty/alacritty.toml

@@ -10,16 +10,16 @@
 # https://github.com/alacritty/alacritty/blob/master/extra/man/alacritty.5.scd
 general.import = [
     # Keybinding fixes for MacOS
-    "~/.config/alacritty/alacritty-fixed-mac-keyboard-bindings.toml",
+    "alacritty-fixed-mac-keyboard-bindings.toml",
     # Color scheme: expected to be automatically generated
-    "~/.config/alacritty/.theme.toml",
+    ".theme.toml",
 ]
 
 [env]
 TERM = "alacritty"
 [terminal.shell]
 program = "/bin/zsh"
-args = ["--login", "-c", "~/.scripts/terminal_launch.zsh"]
+args = ["--login", "-c", "terminal_launch.zsh"]
 
 [window]
 decorations = "none"

+ 5 - 5
.config/lazygit/config.yml

@@ -27,9 +27,9 @@ git:
     autoWrapCommitMessage: true
     autoWrapWidth: 72
 os:
-  edit: "$HOME/.scripts/lazygit_nvim_editor.zsh {{filename}}"
-  editAtLine: "$HOME/.scripts/lazygit_nvim_editor.zsh {{filename}} {{line}}"
-  editAtLineAndWait: "$HOME/.scripts/lazygit_nvim_editor.zsh {{filename}} {{line}}"
+  edit: "$DOTFILES_DIR/.scripts/lazygit_nvim_editor.zsh {{filename}}"
+  editAtLine: "$DOTFILES_DIR/.scripts/lazygit_nvim_editor.zsh {{filename}} {{line}}"
+  editAtLineAndWait: "$DOTFILES_DIR/.scripts/lazygit_nvim_editor.zsh {{filename}} {{line}}"
   editInTerminal: false
 refresher:
   refreshInterval: 2
@@ -38,7 +38,7 @@ customCommands:
   - key: "<c-c>"
     context: "files"
     description: "Create a conventional commit"
-    command: /usr/bin/env python3 $HOME/.scripts/lazygit_add_conventional_commit.py "{{.Form.Type}}" "{{.Form.Scope}}" "{{.Form.Breaking}}" "{{.Form.Subject}}" "{{.Form.Breaking}}"
+    command: /usr/bin/env python3 $DOTFILES_DIR/.scripts/lazygit_add_conventional_commit.py "{{.Form.Type}}" "{{.Form.Scope}}" "{{.Form.Breaking}}" "{{.Form.Subject}}" "{{.Form.Breaking}}"
     prompts:
       - type: "menu"
         key: "Type"
@@ -125,7 +125,7 @@ customCommands:
   - key: "<c-a>"
     context: "commits"
     description: "Modify author date and commit date"
-    command: /usr/bin/env python3 $HOME/.scripts/lazygit_modify_commit_date.py commit "{{.SelectedLocalCommit.Sha}}" "{{.Form.Commit}}" "{{.Form.Timestamp}}"
+    command: /usr/bin/env python3 $DOTFILES_DIR/.scripts/lazygit_modify_commit_date.py commit "{{.SelectedLocalCommit.Sha}}" "{{.Form.Commit}}" "{{.Form.Timestamp}}"
     prompts:
       - type: "menu"
         title: "Commit"

+ 1 - 1
.config/nvim/lua/plugins/floatingterminal.lua

@@ -83,7 +83,7 @@ return {
             MakeTerm("str", "Gamma", "zsh")
             MakeTerm("sta", "Delta", "zsh")
             MakeTerm("stn", "Epsilon", "zsh")
-            MakeTerm("ste", "Zeta", "/bin/zsh ~/.scripts/omniscratch.zsh")
+            MakeTerm("ste", "Zeta", "/bin/zsh $DOTFILES_DIR/.scripts/omniscratch.zsh")
             MakeTerm("sti", "Eta", "lazydocker", true)
             MakeTerm("sto", "Theta", "lazygit", true)
 

+ 11 - 11
.config/skhd/skhdrc

@@ -7,15 +7,15 @@ cmd - n : \
 
 cmd + shift - n : /bin/zsh $XDG_CONFIG_HOME/yabai/yabairc
 cmd + shift - m : /usr/bin/env python3 $XDG_CONFIG_HOME/yabai/yabai.py invert
-cmd + shift - d: /bin/zsh $HOME/.scripts/lightmode.zsh toggle
+cmd + shift - d: /bin/zsh $DOTFILES_DIR/.scripts/lightmode.zsh toggle
 
-cmd - 1 : /bin/zsh $HOME/.scripts/open_application.zsh terminal
-cmd - 2 : /bin/zsh $HOME/.scripts/open_application.zsh browser
-cmd - 3 : /bin/zsh $HOME/.scripts/open_application.zsh finder
-cmd - 4 : /bin/zsh $HOME/.scripts/open_application.zsh communications
-cmd - 5 : /bin/zsh $HOME/.scripts/open_application.zsh music
-cmd - 6 : /bin/zsh $HOME/.scripts/open_application.zsh notetaking
-cmd - 7 : /bin/zsh $HOME/.scripts/open_application.zsh videocall
-cmd - 8 : /bin/zsh $HOME/.scripts/open_application.zsh projecttracking
-cmd - 9 : /bin/zsh $HOME/.scripts/open_application.zsh timetracking
-cmd - 0 : /bin/zsh $HOME/.scripts/open_application.zsh
+cmd - 1 : /bin/zsh $DOTFILES_DIR/.scripts/open_application.zsh terminal
+cmd - 2 : /bin/zsh $DOTFILES_DIR/.scripts/open_application.zsh browser
+cmd - 3 : /bin/zsh $DOTFILES_DIR/.scripts/open_application.zsh finder
+cmd - 4 : /bin/zsh $DOTFILES_DIR/.scripts/open_application.zsh communications
+cmd - 5 : /bin/zsh $DOTFILES_DIR/.scripts/open_application.zsh music
+cmd - 6 : /bin/zsh $DOTFILES_DIR/.scripts/open_application.zsh notetaking
+cmd - 7 : /bin/zsh $DOTFILES_DIR/.scripts/open_application.zsh videocall
+cmd - 8 : /bin/zsh $DOTFILES_DIR/.scripts/open_application.zsh projecttracking
+cmd - 9 : /bin/zsh $DOTFILES_DIR/.scripts/open_application.zsh timetracking
+cmd - 0 : /bin/zsh $DOTFILES_DIR/.scripts/open_application.zsh

+ 1 - 1
.config/tmux/tmux.conf

@@ -8,7 +8,7 @@
 # Disable the leader key
 unbind C-b
 # Instead, use the omnibar script for controlling tmux
-bind -n C-S-Space run-shell -b "/bin/zsh ~/.scripts/omnibar.zsh"
+bind -n C-S-Space run-shell -b "/bin/zsh $DOTFILES_DIR/.scripts/omnibar.zsh"
 
 # Default commands
 set-option -g default-shell /bin/zsh

+ 44 - 18
.config/zsh/.zshenv

@@ -1,24 +1,17 @@
 ################################################################################
 # ZSH Configuration
-#
-# Note: in order for files in `~/.config` to be picked up, be sure to set the
-# global `XDG_*` and `ZDOTDIR` paths. On macOS, these are set at `/etc/zshenv`
-# or `~/.zshenv`), and look something like the following block:
-#
-# ```zsh
-# export XDG_CONFIG_HOME=$HOME/.config
-# export XDG_CACHE_HOME=$HOME/.cache
-# export XDG_DATA_HOME=$HOME/.local/share
-# export XDG_STATE_HOME=$HOME/.local/state
-# export XDG_RUNTIME_DIR=$HOME/.run
-# export XDG_DATA_DIRS=/usr/local/share:/usr/share
-# export XDG_CONFIG_DIRS=/etc/xdg
-# export ZDOTDIR=$XDG_CONFIG_HOME/zsh
-# if [ -f $ZDOTDIR/.zshenv ]; then source $ZDOTDIR/.zshenv; fi
-# ```
-#
 ################################################################################
 
+export XDG_CACHE_HOME=$HOME/.cache
+export XDG_DATA_HOME=$HOME/.local/share
+export XDG_STATE_HOME=$HOME/.local/state
+export XDG_RUNTIME_DIR=$HOME/.run
+export XDG_DATA_DIRS=/usr/local/share:/usr/share
+export XDG_CONFIG_DIRS=/etc/xdg
+export DOTFILES_DIR=$HOME/
+export XDG_CONFIG_HOME=$DOTFILES_DIR/.config
+export ZDOTDIR=$XDG_CONFIG_HOME/zsh
+
 ################################################################################
 # Disable /etc/zprofile, /etc/zshrc, /etc/zlogin and /etc/zlogout
 ################################################################################
@@ -40,7 +33,7 @@ PATH="${PATH}:/usr/sbin"
 PATH="${PATH}:/opt/homebrew/opt/ruby/bin"
 PATH="${PATH}:/opt/homebrew/opt/libpq/bin"
 
-PATH="${PATH}:${HOME}/.scripts"
+PATH="${PATH}:${DOTFILES_DIR}/.scripts"
 
 export GOPATH="${HOME}/.go"
 
@@ -133,6 +126,39 @@ npm() { unset -f npm; load-nvm; npm "$@"; }
 pnpm() { unset -f pnpm; load-nvm; pnpm "$@"; }
 yarn() { unset -f yarn; load-nvm; yarn "$@"; }
 
+################################################################################
+# On macOS, set relevant environment variables to newly-launched programs
+################################################################################
+
+lctl_env_label="LAUNCHCTL_CHECK_INSTANTIATED_$HOST_$USER"
+if [[ ! "$(launchctl getenv $lctl_env_label)" == "TRUE" ]]; then
+    launchctl setenv XDG_CACHE_HOME $XDG_CACHE_HOME
+    launchctl setenv XDG_DATA_HOME $XDG_DATA_HOME
+    launchctl setenv XDG_STATE_HOME $XDG_STATE_HOME
+    launchctl setenv XDG_RUNTIME_DIR $XDG_RUNTIME_DIR
+    launchctl setenv XDG_DATA_DIRS $XDG_DATA_DIRS
+    launchctl setenv XDG_CONFIG_DIRS $XDG_CONFIG_DIRS
+    launchctl setenv DOTFILES_DIR $DOTFILES_DIR
+    launchctl setenv XDG_CONFIG_HOME $XDG_CONFIG_HOME
+    launchctl setenv ZDOTDIR $ZDOTDIR
+    if [[ -o login ]]; then
+        lctl_service_label="local.$HOST.$USER.launchctlenvsetter"
+        launchd_file="/etc/launchd.conf"
+        if [[ ! $(
+            cat $launchd_file | grep "$lctl_service_label" > /dev/null 2>&1
+            ) ]]; then
+            >&2 echo "No \`launchctl\` service \`$lctl_service_label\` found;"
+            >&2 echo "- Environment variables may not be set correctly"
+            >&2 echo "- Run \`add_launchctl_item\` to create the service."
+            function add_launchctl_item {
+                printf "submit -l $lctl_service_label -- zsh"| sudo tee -a $file
+                printf "-c \"source $ZDOTDIR/.zshenv\" \n"| sudo tee -a $file
+            }
+        fi;
+    fi
+    launchctl setenv $lctl_env_label TRUE
+fi
+
 ################################################################################
 # Private or system-specific environment variables
 ################################################################################

+ 1 - 1
.config/zsh/.zshrc

@@ -78,7 +78,7 @@ trap 'set-short-prompt; return 130' INT
 
 # Load SSH key into keychain
 if [[ $OSTYPE == darwin* ]] then
-  ssh-add --apple-use-keychain ~/.ssh/id_rsa >/dev/null 2>&1
+  ssh-add --apple-use-keychain $HOME/.ssh/id_rsa >/dev/null 2>&1
 else
   eval `keychain --eval --quiet id_rsa`
 fi

+ 5 - 5
.scripts/omnibar.zsh

@@ -5,10 +5,10 @@ tmux display-menu -t . \
     "Command List" "r" "list-commands" \
     "Select Pane" "s" "choose-tree" \
     ""  \
-    "Open Project" "p" "new-window -c ~ -n 'Project Launcher' timeout 60s /bin/zsh ~/.scripts/project_launch.zsh" \
+    "Open Project" "p" "new-window -c ~ -n 'Project Launcher' timeout 60s /bin/zsh $DOTFILES_DIR/.scripts/project_launch.zsh" \
     "Open Shell" "o" "new-window /bin/zsh" \
-    "Open Omniscratch" "u" "new-window /bin/zsh ~/.scripts/omniscratch.zsh" \
-    "Launch SSH" "v" "new-window /bin/zsh ~/.scripts/ssh_launch.zsh" \
+    "Open Omniscratch" "u" "new-window /bin/zsh $DOTFILES_DIR/.scripts/omniscratch.zsh" \
+    "Launch SSH" "v" "new-window /bin/zsh $DOTFILES_DIR/.scripts/ssh_launch.zsh" \
     ""  \
     "Next Pane" "Right" "select-pane -t :.+" \
     "Previous Pane" "Left" "select-pane -t :.-" \
@@ -22,9 +22,9 @@ tmux display-menu -t . \
     "Rotate Window" "y" "rotate-window -D" \
     "Zoom Pane" "z" "resize-pane -Z" \
     ""  \
-    "Screenshot" "+" "run-shell \"/bin/zsh -l ~/.scripts/tmux_screencap.zsh\"" \
+    "Screenshot" "+" "run-shell \"/bin/zsh -l $DOTFILES_DIR/.scripts/tmux_screencap.zsh\"" \
     ""  \
-    "New Window" "c" "new-window /bin/zsh ~/.scripts/nvim_launch.zsh" \
+    "New Window" "c" "new-window /bin/zsh $DOTFILES_DIR/.scripts/nvim_launch.zsh" \
     "Close Pane" "x" "kill-pane" \
     "Close Window" "q" "split-window -v -c \"#{pane_current_path}\"" \
     "Detach Session" "d" "detach"

+ 1 - 1
.scripts/open_application.zsh

@@ -32,7 +32,7 @@ case $1 in
     projecttracking)
         open $PROJECTTRACKING_PATH ;;
     timetracking)
-        /bin/zsh $HOME/.scripts/traggo.zsh ;;
+        /bin/zsh $DOTFILES_DIR/.scripts/traggo.zsh ;;
     projectdocs)
         open $PROJECTDOCS_PATH ;;
     *)

+ 2 - 5
.scripts/project_launch.zsh

@@ -2,7 +2,7 @@
 
 (sleep 3; exit 1) &
 
-PROJECT=$(timeout 3 fd \
+PROJECT=$(timeout 2 fd \
     --base-directory ~ \
     -t d \
     -d 5 \
@@ -13,11 +13,8 @@ PROJECT=$(timeout 3 fd \
 if [ -n $PROJECT ] && [ ! "$PROJECT" = '' ]; then 
     PROJECT=$(realpath $PROJECT)
     NAME=$(basename $PROJECT)
-    if [ "$PROJECT" = "$HOME" ]; then
-        NAME="dotfiles"
-    fi
     tmux select-window -t $NAME || \
-        tmux new-window -c "$PROJECT" -n "$NAME" /bin/zsh ~/.scripts/nvim_launch.zsh $PROJECT
+        tmux new-window -c "$PROJECT" -n "$NAME" /bin/zsh $DOTFILES_DIR/.scripts/nvim_launch.zsh $PROJECT
 else
     exit 1
 fi;

+ 5 - 5
.scripts/terminal_launch.zsh

@@ -3,7 +3,7 @@ while true; do
     tput civis;
     items=("zsh" "alpha")
     items+=("${(@f)$(tmux list-sessions -F '#S' 2> /dev/null | grep -v '^alpha$' | cat - <(echo 'beta\ngamma') | uniq | head -n 2)}")
-    items+=("${(z)$(~/.scripts/ssh_launch.zsh 2)}")
+    items+=("${(z)$($DOTFILES_DIR/.scripts/ssh_launch.zsh 2)}")
     printf "\r\n"
     printf "  Welcome to \e[31m${TERM}\e[0m, \e[33m${USER}\e[0m@\e[33m${HOST}\e[0m.\n"
     printf "\n"
@@ -38,7 +38,7 @@ while true; do
             /bin/zsh
             ;;
         r|"")
-            tmux new-session -A -s ${items[2]} "timeout 60s /bin/zsh ~/.scripts/project_launch.zsh"
+            tmux new-session -A -s ${items[2]} "timeout 60s /bin/zsh $DOTFILES_DIR/.scripts/project_launch.zsh"
             ;;
         s)
             tmux new-session -A -s ${items[3]}
@@ -47,10 +47,10 @@ while true; do
             tmux new-session -A -s ${items[4]}
             ;;
         n)
-            /bin/zsh -c "~/.scripts/ssh_launch.zsh ${items[5]}"
+            /bin/zsh -c "$DOTFILES_DIR/.scripts/ssh_launch.zsh ${items[5]}"
             ;;
         e)
-            /bin/zsh -c "~/.scripts/ssh_launch.zsh ${items[6]}"
+            /bin/zsh -c "$DOTFILES_DIR/.scripts/ssh_launch.zsh ${items[6]}"
             ;;
         i)
             # It's hard to know if we're is already in a tmux pane; but, if the
@@ -62,7 +62,7 @@ while true; do
             fi
             ;;
         o)
-            /bin/zsh -c ~/.scripts/ssh_launch.zsh
+            /bin/zsh -c $DOTFILES_DIR/.scripts/ssh_launch.zsh
             ;;
     esac;
     selection=""