From 2d3694aa7a42813e89c61de75e8c5eea74d6b13a Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 18 Oct 2024 17:00:04 -0500 Subject: [PATCH] SSH startup eval: ssh-agent add key to keychain, use .shell dict for commonrc template --- home/.chezmoi.toml.tmpl | 20 ++++++++++++++----- .../scripts/commonrc.sh.tmpl | 17 ++++++++++++---- home/dot_bashrc.tmpl | 2 +- home/dot_zshrc.tmpl | 2 +- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/home/.chezmoi.toml.tmpl b/home/.chezmoi.toml.tmpl index e09c115..4e6a78b 100644 --- a/home/.chezmoi.toml.tmpl +++ b/home/.chezmoi.toml.tmpl @@ -1,3 +1,5 @@ +# Detect chassis type (likely: laptop | desktop | container) +# https://www.freedesktop.org/software/systemd/man/latest/hostnamectl.html#chassis%20%5BTYPE%5D {{- $chassisType := "desktop" }} {{- if eq .chezmoi.os "darwin" }} {{- if contains "MacBook" (output "sysctl" "-n" "hw.model") }} @@ -11,6 +13,7 @@ {{- $chassisType = (output "powershell.exe" "-NoProfile" "-NonInteractive" "-Command" "if ((Get-CimInstance -Class Win32_Battery | Measure-Object).Count -gt 0) { Write-Output 'laptop' } else { Write-Output 'desktop' }") | trim }} {{- end }} +# Detect number of CPU cores and threads {{- $cpuCores := 1 }} {{- $cpuThreads := 1 }} {{- if eq .chezmoi.os "darwin" }} @@ -24,15 +27,22 @@ {{- $cpuThreads = (output "powershell.exe" "-NoProfile" "-NonInteractive" "-Command" "(Get-CimInstance -ClassName 'Win32_Processor').NumberOfLogicalProcessors") | trim | atoi }} {{- end -}} +# Detect if running in WSL +{{- $wsl := true -}} +{{- if eq .chezmoi.os "linux" -}} +{{- $wsl = (.chezmoi.kernel.osrelease | lower | contains "microsoft") -}} +{{- end -}} + [merge] command = "bash" args = [ - "-c", - "cp {{ "{{" }} .Target {{ "}}" }} {{ "{{" }} .Target {{ "}}" }}.base && code --new-window --wait --merge {{ "{{" }} .Destination {{ "}}" }} {{ "{{" }} .Target {{ "}}" }} {{ "{{" }} .Target {{ "}}" }}.base {{ "{{" }} .Source {{ "}}" }}", + "-c", + "cp {{ "{{" }} .Target {{ "}}" }} {{ "{{" }} .Target {{ "}}" }}.base && code --new-window --wait --merge {{ "{{" }} .Destination {{ "}}" }} {{ "{{" }} .Target {{ "}}" }} {{ "{{" }} .Target {{ "}}" }}.base {{ "{{" }} .Source {{ "}}" }}", ] [data] chassis = "{{ $chassisType }}" + wsl = {{ $wsl }} [data.cpu] cores = {{ $cpuCores }} @@ -40,8 +50,8 @@ args = [ encryption = "age" [age] - identity = "{{ .chezmoi.homeDir }}/key.txt" - recipient = "age1s3ctpj9lafl6qwyvd89sn448us7gdzd53d8yyhsc7zny78c0k4sqerrkze" + identity = "{{ .chezmoi.homeDir }}/key.txt" + recipient = "age1s3ctpj9lafl6qwyvd89sn448us7gdzd53d8yyhsc7zny78c0k4sqerrkze" [hooks.read-source-state.pre] - command = "{{ .chezmoi.sourceDir }}/.install-password-manager.sh" + command = "{{ .chezmoi.sourceDir }}/.install-password-manager.sh" diff --git a/home/.chezmoitemplates/scripts/commonrc.sh.tmpl b/home/.chezmoitemplates/scripts/commonrc.sh.tmpl index 55b1b1e..dc4b26c 100644 --- a/home/.chezmoitemplates/scripts/commonrc.sh.tmpl +++ b/home/.chezmoitemplates/scripts/commonrc.sh.tmpl @@ -12,9 +12,9 @@ export OPENAI_API_KEY="{{ (rbw "OpenAI - chatgpt-cli Key").data.password }}" # hishtory export HISHTORY_SERVER="https://hsh.{{ template "keys.tmpl" "privateDomain" }}" export PATH="$PATH:$HOME/.hishtory" -{{ if eq . "zsh" -}} +{{ if eq .shell "zsh" -}} source $HOME/.hishtory/config.zsh -{{ else if eq . "bash" -}} +{{ else if eq .shell "bash" -}} source $HOME/.hishtory/config.sh {{- else -}} {{ fail "Unexpected shell." }} @@ -29,7 +29,11 @@ export PATH="$HOME/.local/share/bob/nvim-bin:$PATH" # Bob, the Neovim package m . "$HOME/.cargo/env" # Rustup + Cargo + Cargo-installed tools eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" # Brew command -v rbenv &> /dev/null && eval "$(rbenv init -)" # rbenv for Ruby -command -v chatgpt &> /dev/null && . <(chatgpt --set-completions {{ . -}}) # chatgpt completions +command -v chatgpt &> /dev/null && . <(chatgpt --set-completions {{ .shell -}}) # chatgpt completions +{{ if eq .shell "bash" -}} +. "$HOME/.asdf/asdf.sh" +. "$HOME/.asdf/completions/asdf.bash" +{{- end }} # pyenv, python version manager export PYENV_ROOT="$HOME/.pyenv" @@ -88,4 +92,9 @@ fi # export EDITOR='micro' # else # export EDITOR='nvim' -# fi \ No newline at end of file +# fi + +# If WSL, add SSH key on startup (once per WSL start) +{{- if .data.wsl }} +eval `keychain --quiet --eval --agents ssh ~/.ssh/id_rsa` +{{ end }} \ No newline at end of file diff --git a/home/dot_bashrc.tmpl b/home/dot_bashrc.tmpl index 24168bb..5644eac 100644 --- a/home/dot_bashrc.tmpl +++ b/home/dot_bashrc.tmpl @@ -98,5 +98,5 @@ if ! shopt -oq posix; then fi fi -{{ template "scripts/commonrc.sh.tmpl" "bash" }} +{{ template "scripts/commonrc.sh.tmpl" dict "data" . "shell" "bash" }} {{ completion "bash" }} \ No newline at end of file diff --git a/home/dot_zshrc.tmpl b/home/dot_zshrc.tmpl index 7856e93..0d373dc 100644 --- a/home/dot_zshrc.tmpl +++ b/home/dot_zshrc.tmpl @@ -28,5 +28,5 @@ plugins=(git asdf yarn golang sudo zoxide bun node npm zsh-autosuggestions brew source $ZSH/oh-my-zsh.sh [[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh # configure with `p10k configure` -{{ template "scripts/commonrc.sh.tmpl" "zsh" }} +{{ template "scripts/commonrc.sh.tmpl" dict "data" . "shell" "zsh" }} {{ completion "zsh" }} \ No newline at end of file