summaryrefslogtreecommitdiffstats
path: root/modules/x11/xsession.nix
blob: 91b59490d4eb16adf1ba35baec04547fcb9ac42c (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# X sessions as systemd user services.
#
# When an X server of the user creates its socket /tmp/.X11-unix/XN, the
# user's xsession-orchestrator starts xsession@N.target, which wants every
# service named in hrm.xsession.services as NAME@N.service, with
# DISPLAY=:N.  When the socket goes away, the target is stopped.
{ config, lib, pkgs, ... }: let
  cfg = config.hrm.xsession;
in {
  options.hrm.xsession = {
    enable = lib.mkEnableOption "X sessions as systemd user services";
    services = lib.mkOption {
      default = [];
      type = lib.types.listOf lib.types.str;
      description = ''
        Names of the systemd user services NAME@ to run in each X session.
      '';
    };
  };
  config = lib.mkIf cfg.enable (lib.mkMerge [
    {
      systemd.user.targets."xsession@".wants =
        map (name: "${name}@%i.service") cfg.services;
      systemd.user.services = lib.genAttrs' cfg.services (name:
        lib.nameValuePair "${name}@" {
          bindsTo = [ "xsession@%i.target" ];
          requires = lib.optional (name != "xserver-wait-ready") "xserver-wait-ready@%i.service";
          after = lib.optional (name != "xserver-wait-ready") "xserver-wait-ready@%i.service";
          serviceConfig.SyslogIdentifier = "${name}@%i";
          environment.DISPLAY = ":%i";
        });
    }

    {
      systemd.user.paths.xsession-orchestrator = {
        wantedBy = [ "paths.target" ];
        pathConfig.PathChanged = "/tmp/.X11-unix";
      };
      systemd.user.services.xsession-orchestrator = {
        serviceConfig = {
          ExecStart = pkgs.writeDash "xsession-orchestrator" ''
            set -efu

            workdir=$(${pkgs.coreutils}/bin/mktemp --tmpdir -d xsession-orchestrator.XXXXXXXX)
            readonly workdir
            trap 'cd / && rm -R "$workdir"' EXIT
            cd "$workdir"

            ${pkgs.findutils}/bin/find /tmp/.X11-unix -type s -user "$USER" |
            ${pkgs.gnused}/bin/sed -nr 's:.*/X([0-9]+)$:\1:p' |
            ${pkgs.coreutils}/bin/sort -n > running_displays
            if test -s running_displays; then
              echo "running displays:$(printf ' :%s' $(cat running_displays))" >&2
            else
              echo 'no running displays' >&2
            fi

            ${pkgs.systemd}/bin/systemctl --user show 'xsession@[0-9]*.target' -p Id --state=active |
            ${pkgs.gnused}/bin/sed -nr 's:^Id=xsession@([0-9]+)\.target$:\1:p' |
            ${pkgs.coreutils}/bin/sort -n > running_sessions
            if test -s running_sessions; then
              echo "running sessions:$(printf ' xsession@%s.target' $(cat running_sessions))" >&2
            else
              echo 'no running sessions' >&2
            fi

            missing_sessions=$(${pkgs.coreutils}/bin/comm -23 running_displays running_sessions)
            obsolete_sessions=$(${pkgs.coreutils}/bin/comm -13 running_displays running_sessions)

            if test -n "$obsolete_sessions"; then
              obsolete_sessions=$(printf ' xsession@%s.target' $obsolete_sessions)
              echo "stopping obsolete sessions:$obsolete_sessions" >&2
              ${pkgs.systemd}/bin/systemctl --user stop $obsolete_sessions
            fi

            if test -n "$missing_sessions"; then
              missing_sessions=$(printf ' xsession@%s.target' $missing_sessions)
              echo "starting missing sessions:$missing_sessions" >&2
              ${pkgs.systemd}/bin/systemctl --user start $missing_sessions
            fi
          '';
          SyslogIdentifier = "xsession-orchestrator";
          Type = "oneshot";
        };
      };
    }

    # Wait for X to become ready.
    {
      hrm.xsession.services = [ "xserver-wait-ready" ];
      systemd.user.services."xserver-wait-ready@".serviceConfig = {
        ExecStart = pkgs.writeDash "xserver-wait-ready" ''
          until ${pkgs.xdpyinfo}/bin/xdpyinfo >/dev/null 2>&1; do
            ${pkgs.coreutils}/bin/sleep 0.1s
          done
        '';
        TimeoutStartSec = "3s";
        Type = "oneshot";
        RemainAfterExit = true;
      };
    }
  ]);
}