Skip to content

Conversation

@bcotton
Copy link
Contributor

@bcotton bcotton commented Feb 12, 2025

When creating new tmux sessions, sesh now automatically sets the SESH_SESSION_NAME environment variable to the name of the session. This enables scripts and programs to know the current session name.

  • Add -e SESH_SESSION_NAME to tmux new-session command

  • Document the new feature in README.md under Environment Variables section

When creating new tmux sessions, sesh now automatically sets the SESH_SESSION_NAME environment variable to the name of the session. This enables scripts and programs to know the current session name.

- Add -e SESH_SESSION_NAME to tmux new-session command

- Document the new feature in README.md under Environment Variables section
@joshmedeski
Copy link
Owner

Do you have an example of how you'd use it?

You can run the following command to get the name of the current session:

tmux display-message -p '#S'

@bcotton
Copy link
Contributor Author

bcotton commented Feb 12, 2025

My goal is make copies of ~/.kube/config so I can have a per-session kubeconfig. As setting "current" context and namespace changes the config file. having the session name will allow for this config.

Fetching the session name from tmux is ok, but would bind any scripting to HAVE to be running under tmux, which is most cases would be true, but less flexible.

@joshmedeski
Copy link
Owner

joshmedeski commented Feb 12, 2025

I understand and have some feedback:

  1. The -e was introduced in tmux 3.2, so I'll have to think about if I want to support backwards compatibility (which I haven't done yet in this project)

  2. The environment variable set is available only within the tmux session, I'm assuming that is alright for your needs

  3. There are chances of command injection vulnerabilities when introducing something like this, although this use-case feels innocuous, it's just a minor concern is worth noting

  4. As for this specific situation, I'm not sure this kind of thing is supported in .kube/config files:

current-context: $SESH_SESSION_NAME
  1. So can you be a little more specific in how you would use the env variable? I'm not yet convinced this is the best thing to introduce to sesh, I think you could achieve a similar result with a built-in tmux feature:
set-hook -g client-attached 'run-shell "set_current_session_name.sh"'
#!/bin/bash
export CURRENT_SESSION=$(tmux display-message -p '#S')

Thanks for suggesting this feature, let me know your thoughts.

@bcotton
Copy link
Contributor Author

bcotton commented Feb 13, 2025

I understand and have some feedback:

  1. The -e was introduced in tmux 3.2, so I'll have to think about if I want to support backwards compatibility (which I haven't done yet in this project)

That's your call :-)

  1. The environment variable set is available only within the tmux session, I'm assuming that is alright for your needs

That's exactly the point. One of the benefits I see of having multiple sessions, as opposed to tmux windows, is you can isolate environments and env vars in each session.

I could see situations where multiple env vars would want to be set for any given session, not just the session name.

  1. There are chances of command injection vulnerabilities when introducing something like this, although this use-case feels innocuous, it's just a minor concern is worth noting

I would say no more risk than the startup_command :-)

  1. As for this specific situation, I'm not sure this kind of thing is supported in .kube/config files:
current-context: $SESH_SESSION_NAME
  1. So can you be a little more specific in how you would use the env variable? I'm not yet convinced this is the best thing to introduce to sesh, I think you could achieve a similar result with a built-in tmux feature:
set-hook -g client-attached 'run-shell "set_current_session_name.sh"'
#!/bin/bash
export CURRENT_SESSION=$(tmux display-message -p '#S')

Working on getting this to work right now. This is hooked with tmux set-hook -g session-created



if [ $# -ne 1 ]; then
    echo "Usage: $0 [start|stop]"
    exit 1
fi

SESSION_NAME=$(tmux display-message -p '#S')

case "$1" in
    start)
        cp ~/.kube/config ~/.kube/config-${SESSION_NAME}
        tmux setenv -t ${SESSION_NAME} KUBECONFIG ~/.kube/config-${SESSION_NAME}
        ;;
    stop)
        echo "Unsetting $SESSION_NAME" > ~/.kube/stopping
        if [ -f ~/.kube/config-${SESSION_NAME} ]; then
            rm ~/.kube/config-${SESSION_NAME}
        else
            echo "No session-specific kubeconfig found"
        fi
        ;;
    *)
        echo "Invalid argument. Usage: $0 [start|stop]"
        exit 1
        ;;
esac

The order of when hooks get run and when the shell is started is not clear. tmux setenv is happening, but KUBECONFIG is not in the shell's env. It does show up in tmux show-environment -t <session> Then there's this: https://babushk.in/posts/renew-environment-tmux.html

also, it seems on the session-closed hook, the value of tmux display-message -p '#S' is the session that we are returning to, not the session that was closed 🤷

Still working on it, lol

@joshmedeski joshmedeski self-requested a review February 13, 2025 16:25
Copy link
Owner

@joshmedeski joshmedeski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for discussing this with me. I'm alright with shipping it!

@joshmedeski joshmedeski merged commit 5a223f1 into joshmedeski:main Feb 13, 2025
4 checks passed
@bcotton
Copy link
Contributor Author

bcotton commented Feb 13, 2025

TIL tmux hooks! Here is per-session KUBECONFIG. Didn't even need the PR.

#!/usr/bin/env bash

# tmux set-hook -g session-created 'run-shell "<this script> start #{hook_session_name}"'
# tmux set-hook -g session-closed 'run-shell "<this script> stop #{hook_session_name}"'


if [ $# -ne 2 ]; then
    echo "Usage: $0 [start|stop] <session_name>"
    exit 1
fi
SESSION_NAME=$2
CLEANED_SESSION_NAME=$(echo $SESSION_NAME | tr -cd '[:alnum:]')
KUBECONFIG_PATH=~/.kube/config-${CLEANED_SESSION_NAME}

case "$1" in
    start)
        cp ~/.kube/config $KUBECONFIG_PATH
        tmux setenv -t ${SESSION_NAME} KUBECONFIG $KUBECONFIG_PATH
        ;;
    stop)
        if [ -f $KUBECONFIG_PATH ]; then
            rm $KUBECONFIG_PATH
        fi
        ;;
    *)
        echo "Invalid argument. Usage: $0 [start|stop]"
        exit 1
        ;;
esac

Then in zsh you need:

      if [ -n "$TMUX" ]; then                                                                               
        function refresh {
          export $(tmux show-environment | grep "^KUBECONFIG") > /dev/null
        }
      else
        function refresh { }
      fi

      function preexec {                                                                                    
         refresh                                                                                           
      }

Then you can kubectx and kubens without effecting other sessions.

@joshmedeski
Copy link
Owner

Hmmm, I thought this kind of feature didn't need to be tied to sesh. I might decide to revert it

@bcotton
Copy link
Contributor Author

bcotton commented Feb 14, 2025

Makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants