You are not logged in.

#1 2019-06-24 11:11:54

CarbonChauvinist
Member
Registered: 2012-06-16
Posts: 413
Website

[SOLVED] Tmux Autostart - Systemd User Session - Environment Variables

I'm auto-starting tmux using a systemd user service as described in the wiki.

$ cat .config/systemd/user/tmux@.service 
[Unit]
Description=Start tmux in detached session                                                                                               
                                                                                                                                         
[Service]                                                                                                                                
Type=forking                                                                                                                             
ExecStart=/usr/bin/tmux new-session -s %u -d                                                                                             
ExecStop=/usr/bin/tmux kill-session -t %u                                                                                                
                                                                                                                                         
[Install]                                                                                                                                
WantedBy=default.target

That coupled with this in my bashrc, also taken from the wiki, have been working relatively well for me and I have tmux available at system start on any terminal I happen to open.

# TMUX
if which tmux >/dev/null 2>&1; then
   #if not inside a tmux session, and if no session is started, start a new session
   test -z "$TMUX" && (tmux attach || tmux new-session -s bash)
fi

I have run into some minor issues though where environment variables that are set up in my standard user session are not being set up in my tmux instance. A good example of this is XDG_SESSION_TYPE, in my regular session it's set as "wayland", however within tmux it's not set.

Here's a github visual diff of the variables that are not set in tmux but have been set in my user session (also pasted below). I'd like some insight as to which ones I need to be specifically exporting into my tmux session. I'd suppose that XDG_SESSION_TYPE is definitely one as it's already caused me issues, but what about the other XDG_SESSION variables? I also suppose things like the plasma variables etc. are not necessarily needed specifically within tmux?

$ diff nontmux_env.txt tmux_env.txt
2,4d1
< SESSION_MANAGER=local/5510:@/tmp/.ICE-unix/1138,unix/5510:/tmp/.ICE-unix/1138
< KDED_STARTED_BY_KDEINIT=1
< COLORTERM=truecolor
6c3
< XDG_SESSION_PATH=/org/freedesktop/DisplayManager/Session1
---
> TMUX=/tmp//tmux-1000/default,997,0
9,10c6
< DESKTOP_SESSION=/usr/share/wayland-sessions/plasmawayland
< XCURSOR_SIZE=0
---
> yank=~/.tmux/yank.sh
12d7
< XDG_SEAT=seat0
14d8
< XDG_SESSION_DESKTOP=KDE
16d9
< XDG_SESSION_TYPE=wayland
19,23c12,13
< XDG_CURRENT_DESKTOP=KDE
< WAYLAND_DISPLAY=wayland-0
< XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
< KDE_SESSION_UID=1000
< XDG_SESSION_CLASS=user
---
> INVOCATION_ID=39c618b8d47640c685e2cacd763d3030
> MANAGERPID=984
25c15
< TERM=xterm-256color
---
> TERM=screen
26a17
> TMUX_PANE=%0
28,30d18
< PLASMA_USE_QT_SCALING=1
< KDE_SESSION_VERSION=5
< PAM_KWALLET5_LOGIN=/run/user/1000/kwallet5.socket
32,37c20
< QT_WAYLAND_FORCE_DPI=96
< DISPLAY=:1
< SHLVL=3
< XDG_VTNR=2
< XDG_SESSION_ID=2
< GS_LIB=/home/ghost/.fonts
---
> SHLVL=1
41,44c24
< QT_AUTO_SCREEN_SCALE_FACTOR=0
< XCURSOR_THEME=capitaine-cursors
< XDG_DATA_DIRS=/usr/share:/usr/share:/usr/local/share
< KDE_FULL_SESSION=true
---
> JOURNAL_STREAM=9:28695
46,47c26
< ALACRITTY_LOG=/tmp/Alacritty-1412.log
< DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-EASoAvM72u,guid=731b2db5f03fbe28f0a770375d0bee6d
---
> DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
48a28
> POWERLINE_COMMAND=powerline

And just for sake of completeness, here's my tmux.conf:

$ sed -E '/^\s*(#|$)/d' .tmux.conf 
unbind C-b
set -g prefix C-a
bind C-a send-prefix
source "/usr/lib/python3.7/site-packages/powerline/bindings/tmux/powerline.conf"
setw -g mode-keys vi
set -g history-limit 100000
set -g set-titles on
set -g set-titles-string "#T"
setw -g mouse on
yank="~/.tmux/yank.sh"
bind -T copy-mode-vi MouseDragEnd1Pane \
    send-keys -X copy-pipe "$yank"
bind -T copy-mode-vi MouseDown1Pane select-pane \;\
    send-keys -X clear-selection

Last edited by CarbonChauvinist (2020-05-02 16:42:19)


"the wind-blown way, wanna win? don't play"

Offline

#2 2019-06-24 12:09:07

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: [SOLVED] Tmux Autostart - Systemd User Session - Environment Variables

Where are these environment variables set in your "standard user session".

Not being set in other environments is expected.  Environment variables are so named because they are bound to the current environment (and generally inherited by child processes) but they are not global.

That second code block is odd - it is indeed in the wiki, but it's pointless.  First, it's your shellrc, so you don't really need to test whether tmux is installed; you put this in your shellrc because you have tmux installed (ironically, it doesn't test first whether "which" is installed before it uses "which" to test for tmux).  But more importantly that conditional and '||' command can all be a single tmux command:

tmux new-session -A -s some_name

Last edited by Trilby (2019-06-24 12:16:13)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2019-06-24 18:01:56

snakeroot
Member
Registered: 2012-10-06
Posts: 164

Re: [SOLVED] Tmux Autostart - Systemd User Session - Environment Variables

It seems you can either set environment variables applicable to a service individually in the service file ("Environment=NAME=VALUE" in the [Service] section), recycle an /etc/environment or similar "NAME=VALUE" file ("EnvironmentFile=/path/to/file" in the [Service] section) or set them globally for all systemd services.

https://coreos.com/os/docs/latest/using … units.html

Offline

#4 2019-06-25 00:33:07

CarbonChauvinist
Member
Registered: 2012-06-16
Posts: 413
Website

Re: [SOLVED] Tmux Autostart - Systemd User Session - Environment Variables

@Trilby I log into a wayland plasma session using sddm, so they're being set by either/both of those TMK. I do have just a few custom ones that I set up in my .bashrc.

I guess my question was more of are there particular variables that are expected to be set? Then I can make sure that I'm not inadvertently them leaving out by starting tmux this way.

Thanks for pointing out the butchered bash -- I'll fix that now. TBH, copy pasted from the wiki and should have looked closer at what it actually was doing.

--Edit: Actually, with further testing, without the line

test -z "$TMUX" && (tmux attach || tmux new-session -A -s bash)

in my .bashrc, the behavior is not what I want. When trying to just use

tmux new-session -A -s ghost

in my .bashrc, whenever I open a new terminal it nests the tmux sessions and I get this message

sessions should be nested with care, unset $TMUX to force.

IOW it does not attach to the already existing session that was started by my user systemd service and instead ?starts a new session within the already existing session?.

With the original conditional line included I get the desired behavior, namely any and every terminal I open connects automatically to the same tmux session that was automatically started by my systemd user service.

@snakeroot, thanks for this, very helpful.

Last edited by CarbonChauvinist (2019-06-25 11:46:03)


"the wind-blown way, wanna win? don't play"

Offline

#5 2019-06-25 15:28:06

IrvineHimself
Member
From: Scotland
Registered: 2016-08-21
Posts: 275

Re: [SOLVED] Tmux Autostart - Systemd User Session - Environment Variables

Recently moving back to Arch and into Wayland, I had to struggle a bit with this as well. Previously, several months ago, under xorg-server, it wasn't that difficult, (he say's somewhat tongue in cheek and hoping forum members have a short memory.) Reading the wiki:

...  The way systemd handles user sessions is pretty much in flux. ...

After an afternoon and evening of experimentation with the solution outlined above, I found the best solution was to add the following to my .bash_profile immediately after the section that launches  my DE. (in my case, a minimal gnome shell)

# Import environment into systemd/user. (In wayland, setting the correct environmental variables for
# systemd/user is not quite as transparent as it was under the xorg-server.)
# see https://wiki.archlinux.org/index.php/Systemd/User#Environment_variables
systemctl --user import-environment

Hope this helps
Irvine

Edited for preciseness

Last edited by IrvineHimself (2019-06-25 15:33:35)


Et voilà, elle arrive. La pièce, le sous, peut-être qu'il arrive avec vous!

Offline

#6 2019-06-26 13:34:29

CarbonChauvinist
Member
Registered: 2012-06-16
Posts: 413
Website

Re: [SOLVED] Tmux Autostart - Systemd User Session - Environment Variables

Thanks very much @IrvineHimself I don't know how I missed the Enironment Variables section in the wiki that you linked. Will go back through now and will update this thread when I've decided which route I want to go.


"the wind-blown way, wanna win? don't play"

Offline

#7 2020-05-02 16:40:56

CarbonChauvinist
Member
Registered: 2012-06-16
Posts: 413
Website

Re: [SOLVED] Tmux Autostart - Systemd User Session - Environment Variables

Finally got time to update this thread, thanks again to all who helped me better understand what was going on - hopefully this might help others struggling with the same concepts?

I followed the outline in this detailed blog write-up - Integrating systemctl --user and your Graphical Session

Basically, we're going to create a placeholder --user target that will be started only after we've imported the DE's environment into the --user session. Once this placeholder target is started we know we can then start any --user service and it will have the needed environment.

$ cat ~/.config/systemd/user/user-graphical-login-target
[Unit]
Description=User Graphical Login
Requires=default.target
After=default.target

Use your DE's startup script option to import the DE's environment into the systemd user session using `systemctl --user import-environment`. (For plasma I added a script to the autostart list under the startup/shutdown options in settings.)

The script also starts the --user target. But the order is important, the systemd --user target is only started AFTER we've imported the DE's environment into the systemd --user session.

$ cat ~/.local/bin/scripts/import_env.sh
#!/usr/bin/env bash
systemctl --user import-environment
systemctl --user start user-graphical-login.target

One can now define any new systemd user service and link it to the `user-graphical-login.target` to ensure that the --user session inherits the DE's environment. (This assumes you're only running one DE. You'll have to adjust if you regularly switch etc.)

$ cat ~/.config/systemd/user/tmux@.service
[Unit]
Description=Start tmux in detached session
Requires=user-graphical-login.target
After=user-graphical-login.target

[Service]
Type=forking
ExecStart=/usr/bin/tmux new-session -s '%u-init' -d;
ExecStop=/usr/bin/tmux kill-session -t '%u-init'

[Install]
WantedBy=user-graphical-login.target

Last edited by CarbonChauvinist (2020-05-03 15:51:08)


"the wind-blown way, wanna win? don't play"

Offline

#8 2024-02-22 08:33:12

miles969
Member
Registered: 2013-02-03
Posts: 23

Re: [SOLVED] Tmux Autostart - Systemd User Session - Environment Variables

sorry for the necrobump, just wanted to share a slightly more lightweight solution to passing graphical envvars to a (tmux) user unit:

I added an ExecStartPre= statement to explicitly pass envvars to the user service (importing the entire block is deprecated and leads to a warning, seems to still work, however).


# ~.config/systemd/user/tmux@.service
[Unit]
Description=tmux session for user %u
After=graphical-session.target
[Service]
Type=forking
# WARNING: use %I instead of %u here; %u=root in system services.
ExecStartPre=systemctl --user import-environment DISPLAY WAYLAND_DISPLAY XDG_SESSION_TYPE XDG_SESSION_DESKTOP GPG_TTY XDG_CURRENT_DESKTOP
ExecStart=/usr/bin/tmux new-session -s %u -d
ExecStop=/usr/bin/tmux kill-session -t %u

[Install]
WantedBy=graphical-session.target

Offline

Board footer

Powered by FluxBB