Thanks for your answer!
I am writing a setup script which installs and configures my system automatically.
As the creation of rclone configurations requires passwords, I wanted to boot up a Plasma Wayland session (for each user which needs to be created) and prompt them to create the necessary rclone configurations and sign out afterwards.
My current (non functioning) draft of the script looks like this: https://git.nuth.ch/manuth/PortValhalla/src/commit/c88be78a7bce6b5d4d3bb64456b58366d1a1305d/scripts/Common/Software/rclone/main.fish#L10-L60
Alright, so I found an absolute trainwreck of a solution. All the ways I have tested (
sudo,login,openvt,machinectl,systemd-run) don't work because important variables likeXDG_SESSION_IDare not created. So probably these methods don't open up a real session or something...?What I did is to automatically open up a session for the specified user - in this case using
getty. Using these code lines, I created asystemd-configuration and booted it up by runningsystemctl daemon-reload:{ printf %s\n \ "[Service]" \ "ExecStart=" \ "ExecStart=-/sbin/agetty -o '-p -f -- \\u' --noclear --autologin $TARGET_USER %I \$TERM" } | sudo tee "/etc/systemd/system/getty@tty2.service.d/autologin.conf" sudo systemctl daemon-reload sudo systemctl restart getty@tty2source: https://wiki.archlinux.org/title/Getty
After this, the user has a running session and proper environment variables set up. I use
pgrepto wait for the expected process to show up:sessionPid="" while [ -z "$sessionPid" ]; do sessionPid=$(sudo pgrep -t tty2 -u "$TARGET_USER") doneThe environment variables of the process with the PID
$sessionPidcan then be borrowed to runstartplasma-waylandproperly as the desired user:sudo -u "$TARGET_USER" \ env \ $(sudo -u "$TARGET_USER" fish -c "cat /proc/$sessionPid/environ | string split0)") \ /usr/lib/plasma-dbus-run-session-if-needed startplasma-waylandThe fact, that this command would always end up focussing on tty2 after exiting the plasma session, I kind of abused the
openvt-command to force the focus to return to the previous tty:openvt -sw -- \ sudo -u "$TARGET_USER" \ env \ $(sudo -u "$TARGET_USER" fish -c "cat /proc/$sessionPid/environ | string split0)") \ /usr/lib/plasma-dbus-run-session-if-needed startplasma-waylandI also did some quirks like using
openvtto find an unused tty. If you want to see the whole thing in action - script is here:https://git.nuth.ch/manuth/PortValhalla/src/commit/b6329693d6fd717c8ca6f7a278a864930b7f705d/scripts/Common/Software/rclone/main.fish#L10-L60