Automatically activate user session with loginctl in Qubes debian-12-minimal template (#7689 workaround)
debian-12-minimal templates in Qubes don't recognize USB devices by default. They require the qubes-usb-proxy package. But a
longstanding issue ((#7689) prevents users from interacting with
connected USB devices. Qubes documentation for Debian miminal templates describes manually using loginctl to activate the
current user session to resolve this. But it doesn't describe a way to automatically activate the user session.
Until the team resolves #7689, I'm using this method to automatically activate the user session on debian-12-minimal templates. It relies on the rc.local script to execute commands on qube startup. The root user runs rc.local.
# Run this RPC locally. It exits when a usable session is available.
/etc/qubes-rpc/qubes.WaitForSession
# After qubes.WaitForSession exits, there will be more than one user session available. This returns a space-separated list.
USER_SESSIONS="$(loginctl show-user user --value --property=Sessions)"
# The first session in the list is the Qubes-associated session. Use shell parameter expansion to delete everything after it.
FIRST_USER_SESSION="${USER_SESSIONS%% *}"
loginctl activate "${FIRST_USER_SESSION}"The qubes.WaitForSession RPC will block until the Qubes-associated user session is available. If you need to execute anything
before this happens, put those commands above /etc/qubes-rpc/qubes.WaitForSession in rc.local. You can also break these lines
out into rc scripts in rc.local.d. Here's how I do that:
# /rw/config/rc.local.d/50-wait-for-session.rc
/etc/qubes-rpc/qubes.WaitForSession# /rw/config/rc.local.d/60-activate-user-session.rc
USER_SESSIONS="$(loginctl show-user user --value --property=Sessions)"
FIRST_USER_SESSION="${USER_SESSIONS%% *}"
loginctl activate "${FIRST_USER_SESSION}"As rc.local says, all rc scripts in /rw/config/rc.local.d run before rc.local itself. But rc scripts in /rw/config/rc.local.d numbered lower than 50 will run before waiting for the user sssion, and scripts numbered higher than 50 will run after the session is available.