New waypipe entering method

This commit is contained in:
Thomas Avé 2026-07-14 14:11:38 +02:00
parent d8b7f25c54
commit 353fbd99ec
Signed by: thomasave
SSH Key Fingerprint: SHA256:bvIbWy6TO9+PdMTPzWy6dqkRlVQ3eSky+vQcc9aRIiE
2 changed files with 47 additions and 20 deletions

View File

@ -111,11 +111,11 @@
] ]
}, },
"locked": { "locked": {
"lastModified": 1783134515, "lastModified": 1783963347,
"narHash": "sha256-qMoZazubXlXUD9k/syJ/aiWC4X4g73mwVmZ7Z4+rdpM=", "narHash": "sha256-r376E2XpakiXwModDHIxlvB6qLq4iFVEq730vxOO4JY=",
"owner": "nix-community", "owner": "nix-community",
"repo": "home-manager", "repo": "home-manager",
"rev": "b885baad531fa3d3beae2ba9a0712d22974d8016", "rev": "a45a7c451455a51ae740ec3bce4024b312809c29",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -163,11 +163,11 @@
}, },
"nixpkgs_2": { "nixpkgs_2": {
"locked": { "locked": {
"lastModified": 1782959384, "lastModified": 1783776592,
"narHash": "sha256-xnJJk+ct+D2+wdRxj1wk36w5zV9RVESwRqcklPdt3fM=", "narHash": "sha256-UgCQzxeWI75XM8G+hPrPh+MKzEPjG3SpAj7dtqSbksA=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "65179426c83bb3f6bc14898b42ea1c6f01d374b0", "rev": "e7a3ca8092b61ff85b6a45bf863ea2b2d6a661b3",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@ -1,20 +1,47 @@
if [ "$SSH_SESSION_ID" != "" ]; then if [ "$PREVIOUS_SESSION_ID" != "" ]; then
mkdir -p /tmp/ssh_sessions # Spawned as a new window/terminal for an existing remote session (by the
echo $$ > /tmp/ssh_sessions/"$SSH_SESSION_ID" # niri/hyprland keybind). Inherit that session's working directory: find a
fi # live shell whose SSH_SESSION_ID (carried in its environment) matches and
# cd to its cwd. Then drop the hint so child shells don't keep jumping back.
if [ "$REMOTE_PATH" != "" ]; then _best_pid=0
if [ -d "$REMOTE_PATH" ]; then _prev_cwd=""
cd "$REMOTE_PATH" for _environ in /proc/[0-9]*/environ; do
fi _pid=${_environ%/environ}
elif [ "$PREVIOUS_SESSION_ID" != "" ]; then _pid=${_pid#/proc/}
cd "$(readlink -e /proc/$(cat /tmp/ssh_sessions/$PREVIOUS_SESSION_ID)/cwd)" # cat (not a < redirect) so unreadable/ptrace-protected environ files
# fail quietly into the pipe instead of raising a shell error.
if cat "$_environ" 2>/dev/null | tr '\0' '\n' | grep -Fxq "SSH_SESSION_ID=$PREVIOUS_SESSION_ID"; then
[ "$(cat /proc/$_pid/comm 2>/dev/null)" = zsh ] || continue
_c=$(readlink -e /proc/$_pid/cwd 2>/dev/null) || continue
if [ -n "$_c" ] && [ "$_pid" -gt "$_best_pid" ]; then
_best_pid=$_pid
_prev_cwd=$_c
fi
fi
done
[ -n "$_prev_cwd" ] && cd "$_prev_cwd"
unset _best_pid _prev_cwd _environ _pid _c PREVIOUS_SESSION_ID
fi fi
function run_waypipe() { function run_waypipe() {
SSH_SESSION_ID=$RANDOM if [ -z "$1" ]; then
REMOTE_PATH=$(echo $(pwd) | sed "s|/home/[a-z]*|\$HOME|g") echo "usage: run_waypipe <host>" >&2
SSH_SESSION_ID=$SSH_SESSION_ID waypipe -n ssh -t $1 env REMOTE_PATH=\"$REMOTE_PATH\" SSH_SESSION_ID=$SSH_SESSION_ID "zsh --login" return 1
fi
local SSH_SESSION_ID=$RANDOM REMOTE_PATH
# Map the current path relative to $HOME so it resolves to the remote user's
# home even when the username differs (the remote shell expands $HOME).
if [[ "$PWD" == "$HOME" || "$PWD" == "$HOME"/* ]]; then
REMOTE_PATH="\$HOME${PWD#$HOME}"
else
REMOTE_PATH="$PWD"
fi
# cd into the mirrored path before the interactive shell starts, so the cwd
# is never carried in an inherited env var (which would leak to every child
# shell and override the cwd of new zellij panes). SSH_SESSION_ID stays in
# the environment on purpose: it is the token the WM keybind above uses to
# locate this session's shells later.
waypipe -n ssh -t "$1" "cd \"$REMOTE_PATH\" 2>/dev/null; exec env SSH_SESSION_ID=$SSH_SESSION_ID zsh --login"
} }
alias s='run_waypipe' alias s='run_waypipe'