zellij icon indicating copy to clipboard operation
zellij copied to clipboard

Preserve environment variable when attaching to existing session

Open blurgyy opened this issue 3 years ago • 15 comments
trafficstars

Currently the environment variables in shells opened in zellij are inherited from the session that starts the zellij server. It would be great to have the newly opened shells inherit the environment of the currently attached session. This is useful for, e.g., indicating current session is a SSH session via shell prompts.

blurgyy avatar Jun 20 '22 03:06 blurgyy

As far as I understand, you have a shell with, say, NEW_ENV="hello" set. You then attach to a pre-existing zellij session and expect zellij to apply that environment variable to every new pane you open from now on through this client?

That would probably be a bit confusing because you have to remember which panes have which environment as old panes already open still have the original server environment?

Can you elaborate on the use case?

raphCode avatar Jul 03 '22 22:07 raphCode

Hi @raphCode, thanks for the reply.

As far as I understand, you have a shell with, say, NEW_ENV="hello" set. You then attach to a pre-existing zellij session and expect zellij to apply that environment variable to every pane you open from now on through this client?

~~No, I'm aware of that expecting zellij to change an existing shell's environments is indeed confusing, and that's not what I would expect, either.~~

Edit(I misunderstood the above question): Yes, I am requesting zellij to apply/merge the environments of the client to newly created panes/tabs ever since I attach this client to an existing zellij session.

Can you elaborate on the use case?

The specific use case is: when I open a new pane/tab, I would like to be able to tell if I am in an SSH connection or not, by examining the shell environment in new pane/tab. This is the behaviour of TMUX and it would be great if zellij can make this configurable (I don't know a way to make this configurable in TMUX, afaik this is a forced behaviour in TMUX).

To explain this, here is a screenshot of how TMUX (with completely default configuration) handles this:

image

where the 0-th pane (left) is opened locally, and the 1-st pane (right) is openned from an SSH connection.

Note how the shell prompts in the two panes differ where the prompt in the 1-st pane (right) shows my username and my machine's hostname since it's opened from within an SSH connection so the shell inherits the SSH connection's environment variables.

I hope this makes it clearer.

blurgyy avatar Jul 04 '22 04:07 blurgyy

How is your shell configured in tmux @blurgyy?

a-kenji avatar Jul 04 '22 09:07 a-kenji

Hi @a-kenji,

How is your shell configured in tmux @blurgyy?

If you are asking about the shell prompt, it is managed by starship, which is configured to show my username if any of SSH_TTY, SSH_CONNECTION or SSH_CLIENT is set, and to show the machine's hostname if SSH_CONNECTION is set:

image

blurgyy avatar Jul 04 '22 10:07 blurgyy

Hi @blurgyy, I will rephrase my question: Did you configure your shell in tmux? (through default-shell)

a-kenji avatar Jul 04 '22 10:07 a-kenji

No, I did not configure my shell in tmux in the first screenshot.

The first screenshot was taken after I deleted my ~/.config/tmux/tmux.conf, and I just checked there is no other tmux configuration enabled (I don't have a ~/.tmux.conf).

blurgyy avatar Jul 04 '22 10:07 blurgyy

@blurgyy, Thank you! That means that your shell is running as a login shell, maybe that could explain the behaviour? I am not to sure if tmux is actually sharing all the environment variables with every window.

Do you see the same behaviour when setting the shell as an interactive shell in tmux?

a-kenji avatar Jul 04 '22 10:07 a-kenji

Hi @a-kenji, thank you for your reply!

Do you see the same behaviour when setting the shell as an interactive shell in tmux?

Yes, I think so:

image

I followed https://wiki.archlinux.org/title/Tmux#Start_a_non-login_shell to launch a non-login shell with tmux, does a "non-login shell" here mean an "interactive shell"?


Some extra information:

  • The tmux.conf in use:
set -g default-command "${SHELL}"
  • The fish script I used to launch and attach to an existing tmux session:
    set tsess "main"
    set default_wname "default"
    set wname (date '+%b-%d,%H-%M-%S')

    if not tmux has-session -t $tsess 2>/dev/null
      # If session '$tsess' does not exist, create first
      tmux start-server
      tmux select-window -t $tsess:0 \; \
        rename-window $default_wname
    end

    exec tmux new-session -t $tsess \; \
      new-window -n $wname \; \
      select-window -t $wname

blurgyy avatar Jul 04 '22 10:07 blurgyy

@blurgyy, Thank you for checking! In that case it seems that tmux is indeed doing something special with the environment variables. This might warrant an option to include in zellij.

a-kenji avatar Jul 04 '22 11:07 a-kenji

Cool, thanks for considering this!

blurgyy avatar Jul 04 '22 13:07 blurgyy

Thanks for the detail! If I understand correctly, this is your setup:

┌─────────────────────┐
│                     │       ┌───────────┐
│ ┌─────┐  ┌───────┐  │  ssh  │           │
│ │local├─►│ zellij│◄─┼───────┤ machine B │
│ │term │  └───────┘  │       │           │
│ └─────┘             │       └───────────┘
│      machine A      │
└─────────────────────┘

And you want zellij to pick up the environment from attached clients and apply it to newly opened panes.

I tested this, and indeed tmux does it, but only for some variables: https://github.com/tmux/tmux/blob/06869ff22fa9891c9633ce3e3efa77cac758b520/options-table.c#L746-L754

I still don't think this is a good idea because panes have different envs depending on who opened them and not based on who is using them. Also this leads to stale environments after the ssh connection closes and the clients go away.

But, if tmux does it, we might too...

raphCode avatar Jul 05 '22 16:07 raphCode

+1 - I just ran into this issue where I have Neovim's clipboard setup to use OSC52 via SSH but pbcopy/pbpaste otherwise.

If my zellij session was started in a local terminal, then I SSH into it, Neovim still think's I'm local.

dsully avatar Jan 21 '24 18:01 dsully