diff --git a/.chezmoiscripts/run_onchange_after_setup-shared-symlinks.sh.tmpl b/.chezmoiscripts/run_onchange_after_setup-shared-symlinks.sh.tmpl index c599e05..33cc4cc 100644 --- a/.chezmoiscripts/run_onchange_after_setup-shared-symlinks.sh.tmpl +++ b/.chezmoiscripts/run_onchange_after_setup-shared-symlinks.sh.tmpl @@ -39,6 +39,36 @@ symlink_to_home() { done } +# ======================================== +# Function: Persist a ~/.config/ directory via /shared/.config +# ======================================== +# Symlinks ~/.config/ -> /shared/.config/ so its contents +# (credentials, caches, etc.) survive workspace rebuilds. A pre-existing real +# directory is migrated into the shared location once, then backed up. +symlink_shared_config() { + local name="$1" + local shared="/shared/.config/$name" + local target="$HOME/.config/$name" + + mkdir -p "$shared" + # Migrate a pre-existing real directory into the shared location once. Only + # swap it for the symlink if the copy fully succeeds — otherwise leave the + # real config in place and bail, so we never symlink to an incomplete copy + # (which would leave the real data only in the backup). + if [ -e "$target" ] && [ ! -L "$target" ]; then + if ! cp -a "$target/." "$shared/"; then + echo "⚠ Could not migrate ~/.config/$name into $shared; leaving real config in place" >&2 + return 0 + fi + local backup="${shared}.backup_$(date +%Y%m%d_%H%M%S)" + mv "$target" "$backup" + echo "✓ Migrated existing ~/.config/$name into $shared (backup: $backup)" + fi + mkdir -p "$HOME/.config" + ln -sfn "$shared" "$target" + echo "✓ ~/.config/$name -> $shared" +} + # ======================================== # CODER WORKSPACE SETUP # ======================================== @@ -85,5 +115,15 @@ if [ "$CODER" = "true" ]; then echo "✓ GitHub CLI symlinked to $GH_SHARED_CONFIG" fi + # Persist selected ~/.config/* directories to /shared/.config so their + # contents (credentials, caches, etc.) survive workspace rebuilds. + # Add directory names here to share more config folders. + SHARED_CONFIG_DIRS=( + gcloud + ) + for cfg in "${SHARED_CONFIG_DIRS[@]}"; do + symlink_shared_config "$cfg" + done + fi {{ end -}}