From 9baaf7852261199c550e8946a4b17385ea109ab5 Mon Sep 17 00:00:00 2001 From: bcherb2 Date: Wed, 18 Nov 2026 01:29:55 -0500 Subject: [PATCH] feat: Claude Desktop personal/work profile launchers, claude:// dispatcher and handlers (Linux only) --- .chezmoiignore | 14 ++++++ ...hange_after_50-claude-url-handlers.sh.tmpl | 32 +++++++++++++ dot_local/bin/executable_claude-personal | 17 +++++++ dot_local/bin/executable_claude-url-dispatch | 45 +++++++++++++++++++ dot_local/bin/executable_claude-work | 17 +++++++ .../applications/claude-personal.desktop.tmpl | 10 +++++ .../claude-url-dispatch.desktop.tmpl | 6 +++ .../applications/claude-work.desktop.tmpl | 10 +++++ ...reate_claude-code-url-handler.desktop.tmpl | 7 +++ .../claude-shims/personal/executable_xdg-open | 7 +++ .../claude-shims/work/executable_xdg-open | 7 +++ 11 files changed, 172 insertions(+) create mode 100644 .chezmoiscripts/run_onchange_after_50-claude-url-handlers.sh.tmpl create mode 100755 dot_local/bin/executable_claude-personal create mode 100755 dot_local/bin/executable_claude-url-dispatch create mode 100755 dot_local/bin/executable_claude-work create mode 100644 dot_local/share/applications/claude-personal.desktop.tmpl create mode 100644 dot_local/share/applications/claude-url-dispatch.desktop.tmpl create mode 100644 dot_local/share/applications/claude-work.desktop.tmpl create mode 100644 dot_local/share/applications/create_claude-code-url-handler.desktop.tmpl create mode 100755 dot_local/share/claude-shims/personal/executable_xdg-open create mode 100755 dot_local/share/claude-shims/work/executable_xdg-open diff --git a/.chezmoiignore b/.chezmoiignore index 9eeb367..97a6951 100644 --- a/.chezmoiignore +++ b/.chezmoiignore @@ -30,6 +30,20 @@ README.md # box once and then leaves herdr's rewrites alone.) .config/btop/btop.conf +# Claude Desktop profile launchers (personal/work), their xdg-open shims, the +# claude:// dispatcher and their .desktop entries. Claude Desktop on Linux is +# the community package at /usr/bin/claude-desktop; macOS has the real app and +# no .desktop files, so none of this applies there. +{{ if ne .chezmoi.os "linux" }} +.local/bin/claude-personal +.local/bin/claude-work +.local/bin/claude-url-dispatch +.local/share/claude-shims +.local/share/claude-shims/** +.local/share/applications +.local/share/applications/** +{{ end }} + # Belt and braces on the tier boundary. The public repo does not manage any of # these and must never start: a stray `chezmoi add ~/.config/gh` would # otherwise sweep up an OAuth token, and the same for the bws token and the diff --git a/.chezmoiscripts/run_onchange_after_50-claude-url-handlers.sh.tmpl b/.chezmoiscripts/run_onchange_after_50-claude-url-handlers.sh.tmpl new file mode 100644 index 0000000..9d78b56 --- /dev/null +++ b/.chezmoiscripts/run_onchange_after_50-claude-url-handlers.sh.tmpl @@ -0,0 +1,32 @@ +{{- if eq .chezmoi.os "linux" -}} +#!/bin/sh +# Point the claude:// and claude-cli:// URL schemes at our handlers. +# +# ~/.config/mimeapps.list is rewritten by every desktop and by xdg-mime itself, +# so it is not a file this repo can own. Setting the two defaults through +# xdg-mime touches only those two lines. +# +# claude:// -> claude-url-dispatch, which routes OAuth callbacks and +# deep links to the Claude Desktop profile that asked for +# them. The packaged com.anthropic.Claude.desktop starts an +# unflagged instance that cannot keep the sign-in. +# claude-cli:// -> Claude Code's own handler. +# +# Linux only (the template renders empty elsewhere, and chezmoi skips an empty +# script). Skipped with a note when xdg-mime is absent, e.g. a headless box. +# It re-runs when this file changes, or when a handler's .desktop does: +# dispatch {{ include "dot_local/share/applications/claude-url-dispatch.desktop.tmpl" | sha256sum }} +# cli {{ include "dot_local/share/applications/create_claude-code-url-handler.desktop.tmpl" | sha256sum }} +set -u + +if ! command -v xdg-mime >/dev/null 2>&1; then + echo "claude URL handlers: xdg-mime not found, skipped" >&2 + exit 0 +fi + +rc=0 +xdg-mime default claude-url-dispatch.desktop x-scheme-handler/claude || rc=1 +xdg-mime default claude-code-url-handler.desktop x-scheme-handler/claude-cli || rc=1 +[ "$rc" -eq 0 ] || echo "claude URL handlers: xdg-mime failed; set them by hand with xdg-mime default" >&2 +exit 0 +{{ end -}} diff --git a/dot_local/bin/executable_claude-personal b/dot_local/bin/executable_claude-personal new file mode 100755 index 0000000..57cb2e3 --- /dev/null +++ b/dot_local/bin/executable_claude-personal @@ -0,0 +1,17 @@ +#!/bin/zsh +# Claude Desktop — isolated "personal" profile. +# --user-data-dir : separate Electron profile; also separates the +# single-instance lockfile, which is what allows a +# second concurrent instance to run at all. +# --password-store : app only auto-detects on KDE; XFCE would fall back +# to basic_text, where safeStorage is unavailable and +# OAuth tokens do not survive a restart. +# --class : distinct WM_CLASS so xfwm/taskbar treats the two +# windows as separate apps. +export CLAUDE_CONFIG_DIR="${CLAUDE_CONFIG_DIR:-$HOME/.claude}" +export PATH="$HOME/.local/share/claude-shims/personal:$PATH" +exec /usr/bin/claude-desktop \ + --user-data-dir="$HOME/.local/share/claude-personal" \ + --password-store=gnome-libsecret \ + --class=ClaudePersonal \ + "$@" diff --git a/dot_local/bin/executable_claude-url-dispatch b/dot_local/bin/executable_claude-url-dispatch new file mode 100755 index 0000000..6bce1c7 --- /dev/null +++ b/dot_local/bin/executable_claude-url-dispatch @@ -0,0 +1,45 @@ +#!/bin/zsh +# Routes claude:// URLs (OAuth callbacks, deep links) to the correct profile. +# +# The packaged entry runs plain `claude-desktop %U` with no flags, which +# starts an unflagged default-profile instance that has no keyring backend +# and cannot save the sign-in. This picks the right profile instead. +# +# Ordering: +# 1. A fresh hint written by that profile's xdg-open shim when the app +# opened the browser -- this is the authoritative signal, and it is +# consumed on use so it can never misroute a later link. +# 2. Whichever profile is the only one running. +# 3. The most recently started instance, else personal. +# +# Handing the URL to the profile wrapper is safe: Electron's second-instance +# handler forwards it into the running instance with that --user-data-dir. +url="$1" +hint="$HOME/.config/claude-profile-target" +HINT_MAX_AGE=600 # seconds; ignore an abandoned sign-in attempt +running() { pgrep -f -- "--user-data-dir=$HOME/.local/share/claude-$1" >/dev/null 2>&1 } + +target="" +if [[ -r "$hint" ]]; then + age=$(( $(date +%s) - $(stat -c %Y "$hint") )) + if (( age <= HINT_MAX_AGE )); then + candidate="$(<"$hint")" + [[ "$candidate" == (personal|work) ]] && target="$candidate" + fi + rm -f "$hint" +fi + +if [[ -z "$target" ]]; then + if running personal && ! running work; then + target=personal + elif running work && ! running personal; then + target=work + else + target=$(for p in personal work; do + pid=$(pgrep -f -- "--user-data-dir=$HOME/.local/share/claude-$p" | head -1) + [[ -n "$pid" ]] && echo "$(ps -o lstart= -p $pid | xargs -I{} date -d {} +%s) $p" + done | sort -rn | head -1 | cut -d' ' -f2) + target="${target:-personal}" + fi +fi +exec "$HOME/.local/bin/claude-$target" "$url" diff --git a/dot_local/bin/executable_claude-work b/dot_local/bin/executable_claude-work new file mode 100755 index 0000000..f189987 --- /dev/null +++ b/dot_local/bin/executable_claude-work @@ -0,0 +1,17 @@ +#!/bin/zsh +# Claude Desktop — isolated "work" profile. +# --user-data-dir : separate Electron profile; also separates the +# single-instance lockfile, which is what allows a +# second concurrent instance to run at all. +# --password-store : app only auto-detects on KDE; XFCE would fall back +# to basic_text, where safeStorage is unavailable and +# OAuth tokens do not survive a restart. +# --class : distinct WM_CLASS so xfwm/taskbar treats the two +# windows as separate apps. +export CLAUDE_CONFIG_DIR="${CLAUDE_CONFIG_DIR:-$HOME/.claude-work}" +export PATH="$HOME/.local/share/claude-shims/work:$PATH" +exec /usr/bin/claude-desktop \ + --user-data-dir="$HOME/.local/share/claude-work" \ + --password-store=gnome-libsecret \ + --class=ClaudeWork \ + "$@" diff --git a/dot_local/share/applications/claude-personal.desktop.tmpl b/dot_local/share/applications/claude-personal.desktop.tmpl new file mode 100644 index 0000000..0db6c02 --- /dev/null +++ b/dot_local/share/applications/claude-personal.desktop.tmpl @@ -0,0 +1,10 @@ +[Desktop Entry] +Name=Claude (Personal) +Comment=Claude Desktop — personal account +GenericName=AI Assistant +Exec={{ .chezmoi.homeDir }}/.local/bin/claude-personal %U +Icon=claude-desktop +Type=Application +StartupNotify=true +StartupWMClass=ClaudePersonal +Categories=Utility;Development; diff --git a/dot_local/share/applications/claude-url-dispatch.desktop.tmpl b/dot_local/share/applications/claude-url-dispatch.desktop.tmpl new file mode 100644 index 0000000..9d6b699 --- /dev/null +++ b/dot_local/share/applications/claude-url-dispatch.desktop.tmpl @@ -0,0 +1,6 @@ +[Desktop Entry] +Name=Claude (profile-aware link handler) +Exec={{ .chezmoi.homeDir }}/.local/bin/claude-url-dispatch %u +Type=Application +NoDisplay=true +MimeType=x-scheme-handler/claude; diff --git a/dot_local/share/applications/claude-work.desktop.tmpl b/dot_local/share/applications/claude-work.desktop.tmpl new file mode 100644 index 0000000..cdb674a --- /dev/null +++ b/dot_local/share/applications/claude-work.desktop.tmpl @@ -0,0 +1,10 @@ +[Desktop Entry] +Name=Claude (Work) +Comment=Claude Desktop — work account +GenericName=AI Assistant +Exec={{ .chezmoi.homeDir }}/.local/bin/claude-work %U +Icon=claude-desktop +Type=Application +StartupNotify=true +StartupWMClass=ClaudeWork +Categories=Utility;Development; diff --git a/dot_local/share/applications/create_claude-code-url-handler.desktop.tmpl b/dot_local/share/applications/create_claude-code-url-handler.desktop.tmpl new file mode 100644 index 0000000..5590826 --- /dev/null +++ b/dot_local/share/applications/create_claude-code-url-handler.desktop.tmpl @@ -0,0 +1,7 @@ +[Desktop Entry] +Name=Claude Code URL Handler +Comment=Handle claude-cli:// deep links for Claude Code +Exec="{{ .chezmoi.homeDir }}/.local/bin/claude" --handle-uri %u +Type=Application +NoDisplay=true +MimeType=x-scheme-handler/claude-cli; diff --git a/dot_local/share/claude-shims/personal/executable_xdg-open b/dot_local/share/claude-shims/personal/executable_xdg-open new file mode 100755 index 0000000..38b88de --- /dev/null +++ b/dot_local/share/claude-shims/personal/executable_xdg-open @@ -0,0 +1,7 @@ +#!/bin/sh +# PATH shim, injected only into the "personal" Claude instance. +# Electron shells out to xdg-open (via PATH) to open the browser for +# sign-in. Recording the profile here is what lets the claude:// callback +# find its way back to the instance that actually started the login. +printf '%s' "personal" > "$HOME/.config/claude-profile-target" 2>/dev/null +exec /usr/bin/xdg-open "$@" diff --git a/dot_local/share/claude-shims/work/executable_xdg-open b/dot_local/share/claude-shims/work/executable_xdg-open new file mode 100755 index 0000000..85751eb --- /dev/null +++ b/dot_local/share/claude-shims/work/executable_xdg-open @@ -0,0 +1,7 @@ +#!/bin/sh +# PATH shim, injected only into the "work" Claude instance. +# Electron shells out to xdg-open (via PATH) to open the browser for +# sign-in. Recording the profile here is what lets the claude:// callback +# find its way back to the instance that actually started the login. +printf '%s' "work" > "$HOME/.config/claude-profile-target" 2>/dev/null +exec /usr/bin/xdg-open "$@"