How can I reliably detect a Wayland or X11 session in an autostart script?

You could use what I use in the freerdp template [root tip] [Utility Script] FreeRDP script template

The script checks the current session and choose the correct binary

basically

if [[ ${XDG_SESSION_TYPE} = "wayland" ]]; then

else

fi

If you are not logged in then the session does not exist and the type is irrelevant.

The pacman-mirrors application (python) also checks the environment when it is run.
pacman_mirrors/functions/cliFn.py · master · Applications / pacman-mirrors · GitLab

        # wayland - sway - console - gtk
        if (
            os.environ.get("XDG_SESSION_TYPE") == "wayland"
            or os.environ.get("XDG_SESSION_TYPE") == "tty"
            or not os.environ.get("DISPLAY")
            or not gtk_available
        ):

            self.no_display = True
1 Like