summary refs log tree commit diff
path: root/nixos/modules/services/x11/display-managers/set-session.py
blob: 75940efe32b42577150ea95c3ba2c3bc248098f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python

import gi, argparse, os, logging, sys

gi.require_version("AccountsService", "1.0")
from gi.repository import AccountsService, GLib
from ordered_set import OrderedSet


def get_session_file(session):
    system_data_dirs = GLib.get_system_data_dirs()

    session_dirs = OrderedSet(
        os.path.join(data_dir, session)
        for data_dir in system_data_dirs
        for session in {"wayland-sessions", "xsessions"}
    )

    session_files = OrderedSet(
        os.path.join(dir, session + ".desktop")
        for dir in session_dirs
        if os.path.exists(os.path.join(dir, session + ".desktop"))
    )

    # Deal with duplicate wayland-sessions and xsessions.
    # Needed for the situation in gnome-session, where there's
    # a xsession named the same as a wayland session.
    if any(map(is_session_wayland, session_files)):
        session_files = OrderedSet(
            session for session in session_files if is_session_wayland(session)
        )
    else:
        session_files = OrderedSet(
            session for session in session_files if is_session_xsession(session)
        )

    if len(session_files) == 0:
        logging.warning("No session files are found.")
        sys.exit(0)
    else:
        return session_files[0]


def is_session_xsession(session_file):
    return "/xsessions/" in session_file


def is_session_wayland(session_file):
    return "/wayland-sessions/" in session_file


def main():
    parser = argparse.ArgumentParser(
        description="Set session type for all normal users."
    )
    parser.add_argument("session", help="Name of session to set.")

    args = parser.parse_args()

    session = getattr(args, "session")
    session_file = get_session_file(session)

    user_manager = AccountsService.UserManager.get_default()
    users = user_manager.list_users()

    for user in users:
        if user.is_system_account():
            continue
        else:
            if is_session_wayland(session_file):
                logging.debug(
                    f"Setting session name: {session}, as we found the existing wayland-session: {session_file}"
                )
                user.set_session(session)
                user.set_session_type("wayland")
            elif is_session_xsession(session_file):
                logging.debug(
                    f"Setting session name: {session}, as we found the existing xsession: {session_file}"
                )
                user.set_x_session(session)
                user.set_session(session)
                user.set_session_type("x11")
            else:
                logging.error(f"Couldn't figure out session type for {session_file}")
                sys.exit(1)


if __name__ == "__main__":
    main()