joshmedeski / sesh

Smart session manager for the terminal
MIT License
697 stars 41 forks source link

Support custom environment variables in Session configurations #182

Closed DanielCardonaRojas closed 1 month ago

DanielCardonaRojas commented 2 months ago

What would you like sesh to do?

Hey, first of all, thank you for creating this tool!

I would like to suggest a feature enhancement that would allow users to export custom environment variables via the configuration file. The idea is to define environment variables within each session configuration, and when a session is started, these variables should be set in the environment for that session.

Proposed Change:

In addition to the current options available in the TOML configuration (like name, path, startup_command), I propose adding an option for env, where users can define one or more environment variables that will be set when the session is opened.

For example:

[[session]]
name = "Downloads 📥"
path = "~/Downloads"
startup_command = "ls"
env = { MY_CUSTOM_VAR = "downloads", ANOTHER_VAR = "example" }

When the session is started, the specified environment variables should be exported. This would allow external scripts or commands within the tmux session to pick up these environment variables and modify their behavior accordingly.

Benefits:

I believe this can be accomplished with:

tmux set-environment MY_CUSTOM_VAR "some_value"
joshmedeski commented 2 months ago

Interesting idea, would this work as-is?

startup_command = "MY_CUSTOM_VAR ='downloads' ANOTHER_VAR='example' ls"

If there are other tools like tmuxp that can do something similar I might be more interested in adding that integration rather than trying to build this kind of feature by hand.

DanielCardonaRojas commented 2 months ago

That doesn't seem to work.

My second attempt was to try the following:

startup_command = "tmux set-environment MY_CUSTOM_VAR 'some_value'"

The previous startup command works but only for subsequently created windows. The window that is initially created for the session does not receive the environment variable.

Investigating a bit more seems like this would allow the first window to receive the environment variable:

tmux new-session -d -s my_session "export MY_CUSTOM_VAR='some_value'; exec $SHELL"

I believe this touches territory of sesh connect.

So the final combination that yields the desired result would be:

tmux new-session -d -s my_session "export MY_CUSTOM_VAR='some_value'; exec $SHELL"
tmux set-environment -t  my_session MY_CUSTOM_VAR 'some_value'
joshmedeski commented 2 months ago

Could you use a tool like direnv?

DanielCardonaRojas commented 2 months ago

I guess that could work for a very project centric workflow, however I think there are still valid escenarios where you would want environment variables tied to sessions, not directories. An obvious benefit is less planning and configuration.

joshmedeski commented 2 months ago

Hmm, I guess I'd like a realistic example. I've never heard anyone mention this scenario, seems overcomplicated to me (without an example).

I'm not opposed to the idea, I just don't want to add complexity to the project if I can't justify it being useful to more than one person, hope you understand that.

DanielCardonaRojas commented 1 month ago

I understand, and perhaps we need more specific use cases to make a solid case for why this might be beneficial. Here are some that I personally would use, though I admit that for each specific case, there might be a workaround, and maybe it’s just convenience that I’m requesting here:

joshmedeski commented 1 month ago

Interesting, I think you can solve this with a startup script.

#!/bin/bash
tmux set-environment EDITOR "nano"
tmux set-environment DOCKER_HOST "localhost"

Then, you can call it as a custom configruation

[[session]]
name = "nano custom docker"
path = "~/projects/example"
startup_command = "nano-docker-localhost.sh"

Make sure the script is in your path, or define an absolute path, and make sure the file is exectuable.

I don't see why this wouldn't solve all your problems, and you can reuse the startup scripts. Hope this helps!

joshmedeski commented 1 month ago

The previous startup command works but only for subsequently created windows

I have a feeling you'll have to experiment with the script to get this to work on the first window, seems like you're trying to do something tmux isn't expecting (updating environmental variables on the fly).

DanielCardonaRojas commented 1 month ago

Yes, this almost does it, and is what I'm doing for now. As I mentioned in:

https://github.com/joshmedeski/sesh/issues/182#issuecomment-2378424467

This takes care of any new windows after the session is created but misses on the first window.

But I understand maybe its not worth the extra work. The following startup command does work:

startup_command = "export MY_CUSTOM_VAR='some_value'; tmux set-environment MY_CUSTOM_VAR 'some_value'; clear"

Feel free to close.

joshmedeski commented 1 month ago

I was thinking the same thing (exporting and setting).

Glad we could talk it out, thanks for your feedback.