summaryrefslogtreecommitdiffstats
path: root/nixos/tests/startx.nix
blob: 51c38d0ecdd014e9c961d8fb2a684764e389e8cf (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
{ pkgs, runTest }:

{

  declarative = runTest {
    name = "startx";
    meta.maintainers = with pkgs.lib.maintainers; [ rnhmjoj ];

    nodes.machine =
      { pkgs, ... }:
      {
        services.getty.autologinUser = "root";

        environment.systemPackages = with pkgs; [
          xdotool
          catclock
        ];

        programs.bash.promptInit = "PS1='# '";

        # startx+bspwm setup
        services.xserver = {
          enable = true;
          windowManager.bspwm = {
            enable = true;
            configFile = pkgs.writeShellScript "bspwrc" ''
              bspc config border_width 2
              bspc config window_gap 12
              bspc rule -a xclock state=floating sticky=on
            '';
            sxhkd.configFile = pkgs.writeText "sxhkdrc" ''
              # open a terminal
              super + Return
                urxvtc
              # quit bspwm
              super + alt + Escape
                bspc quit
            '';
          };
          displayManager.startx = {
            enable = true;
            generateScript = true;
            extraCommands = ''
              xrdb -load ~/.Xresources
              xsetroot -solid '#343d46'
              xsetroot -cursor_name trek
              xclock &
            '';
          };
        };

        # enable some user services
        security.polkit.enable = true;
        services.urxvtd.enable = true;
        programs.xss-lock.enable = true;
      };

    testScript = ''
      import textwrap

      sysu = "env XDG_RUNTIME_DIR=/run/user/0 systemctl --user";
      prompt = "# "

      with subtest("Wait for the autologin"):
          machine.wait_until_tty_matches("1", prompt)

      with subtest("Setup dotfiles"):
          machine.execute(textwrap.dedent("""
            cat <<EOF > ~/.Xresources
              urxvt*foreground: #9b9081
              urxvt*background: #181b20
              urxvt*scrollBar:  false
              urxvt*title:      myterm
              urxvt*geometry:   80x240+0+0
              xclock*geometry:  164x164+24+440
            EOF
          """))

      with subtest("Can start the X server"):
          machine.send_chars("startx\n")
          machine.wait_for_x()
          machine.wait_for_window("xclock")

      with subtest("Graphical services are running"):
          machine.succeed(f"{sysu} is-active graphical-session.target")
          machine.succeed(f"{sysu} is-active urxvtd")
          machine.succeed(f"{sysu} is-active xss-lock")

      with subtest("Can interact with the WM"):
          machine.wait_until_succeeds("pgrep sxhkd")
          machine.wait_until_succeeds("pgrep bspwm")
          # spawn some terminals
          machine.send_key("meta_l-ret", delay=0.5)
          machine.send_key("meta_l-ret", delay=0.5)
          machine.send_key("meta_l-ret", delay=0.5)
          # Note: this tests that resources have beeen loaded
          machine.wait_for_window("myterm")
          machine.screenshot("screenshot.png")

      with subtest("Can stop the X server"):
          # kill the WM
          machine.send_key("meta_l-alt-esc")
          machine.wait_until_tty_matches("1", prompt)

      with subtest("Graphical session has stopped"):
          machine.fail(f"{sysu} is-active graphical-session.target")
          machine.fail(f"{sysu} is-active urxvtd")
          machine.fail(f"{sysu} is-active xss-lock")
    '';
  };

  imperative = runTest {
    name = "startx-imperative";
    meta.maintainers = with pkgs.lib.maintainers; [ rnhmjoj ];

    nodes.machine =
      { pkgs, ... }:
      {
        services.getty.autologinUser = "root";
        programs.bash.promptInit = "PS1='# '";

        # startx+twm setup
        services.xserver = {
          enable = true;
          windowManager.twm.enable = true;
          displayManager.startx.enable = true;
          displayManager.startx.generateScript = false;
        };

        # enable some user services
        security.polkit.enable = true;
        services.urxvtd.enable = true;
        programs.xss-lock.enable = true;
      };

    testScript = ''
      import textwrap

      sysu = "env XDG_RUNTIME_DIR=/run/user/0 systemctl --user";
      prompt = "# "

      with subtest("Wait for the autologin"):
          machine.wait_until_tty_matches("1", prompt)

      with subtest("Setup dotfiles"):
          machine.execute(textwrap.dedent("""
            cat <<EOF > ~/.Xresources
            urxvt*foreground: #9b9081
            urxvt*background: #181b20
            urxvt*scrollBar:  false
            urxvt*title:      myterm
            urxvt*geometry:   20x20+40+40
            EOF
            cat <<EOF > ~/.twmrc
            "Return" = meta : all : f.exec "urxvtc"
            "Escape" = meta : all : f.quit
            EOF
            cat <<EOF > ~/.xinitrc
            xrdb -load ~/.Xresources
            xsetroot -solid '#343d46'
            xsetroot -cursor_name trek
            # start user services
            systemctl --user import-environment DISPLAY XDG_SESSION_ID
            systemctl --user start nixos-fake-graphical-session.target
            # run the window manager
            twm
            # stop services and all subprocesses
            systemctl --user stop nixos-fake-graphical-session.target
            EOF
          """))

      with subtest("Can start the X server"):
          machine.send_chars("startx\n")
          machine.wait_for_x()

      with subtest("Graphical services are running"):
          machine.succeed(f"{sysu} is-active graphical-session.target")
          machine.succeed(f"{sysu} is-active urxvtd")
          machine.succeed(f"{sysu} is-active xss-lock")

      with subtest("Can interact with the WM"):
          machine.wait_until_succeeds("pgrep twm")
          # spawn a terminal
          machine.send_key("alt-ret")
          machine.wait_for_window("myterm")
          machine.screenshot("screenshot.png")

      with subtest("Can stop the X server"):
          # kill the WM
          machine.send_key("alt-esc")
          machine.wait_until_tty_matches("1", prompt)

      with subtest("Graphical session has stopped"):
          machine.fail(f"{sysu} is-active graphical-session.target")
          machine.fail(f"{sysu} is-active urxvtd")
          machine.fail(f"{sysu} is-active xss-lock")
    '';
  };

}