joshmedeski / sesh

Smart session manager for the terminal
MIT License
571 stars 34 forks source link

Support custom environment variables in Session configurations #182

Open DanielCardonaRojas opened 4 days ago

DanielCardonaRojas commented 4 days 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 4 days 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 3 days 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 3 days ago

Could you use a tool like direnv?

DanielCardonaRojas commented 16 hours 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 hours 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.