1{ pkgs, runTest }:
2
3{
4
5 declarative = runTest {
6 name = "startx";
7 meta.maintainers = with pkgs.lib.maintainers; [ rnhmjoj ];
8
9 nodes.machine =
10 { pkgs, ... }:
11 {
12 services.getty.autologinUser = "root";
13
14 environment.systemPackages = with pkgs; [
15 xdotool
16 catclock
17 ];
18
19 programs.bash.promptInit = "PS1='# '";
20
21 # startx+bspwm setup
22 services.xserver = {
23 enable = true;
24 windowManager.bspwm = {
25 enable = true;
26 configFile = pkgs.writeShellScript "bspwrc" ''
27 bspc config border_width 2
28 bspc config window_gap 12
29 bspc rule -a xclock state=floating sticky=on
30 '';
31 sxhkd.configFile = pkgs.writeText "sxhkdrc" ''
32 # open a terminal
33 super + Return
34 urxvtc
35 # quit bspwm
36 super + alt + Escape
37 bspc quit
38 '';
39 };
40 displayManager.startx = {
41 enable = true;
42 generateScript = true;
43 extraCommands = ''
44 xrdb -load ~/.Xresources
45 xsetroot -solid '#343d46'
46 xsetroot -cursor_name trek
47 xclock &
48 '';
49 };
50 };
51
52 # enable some user services
53 security.polkit.enable = true;
54 services.urxvtd.enable = true;
55 programs.xss-lock.enable = true;
56 };
57
58 testScript = ''
59 import textwrap
60
61 sysu = "env XDG_RUNTIME_DIR=/run/user/0 systemctl --user";
62 prompt = "# "
63
64 with subtest("Wait for the autologin"):
65 machine.wait_until_tty_matches("1", prompt)
66
67 with subtest("Setup dotfiles"):
68 machine.execute(textwrap.dedent("""
69 cat <<EOF > ~/.Xresources
70 urxvt*foreground: #9b9081
71 urxvt*background: #181b20
72 urxvt*scrollBar: false
73 urxvt*title: myterm
74 urxvt*geometry: 80x240+0+0
75 xclock*geometry: 164x164+24+440
76 EOF
77 """))
78
79 with subtest("Can start the X server"):
80 machine.send_chars("startx\n")
81 machine.wait_for_x()
82 machine.wait_for_window("xclock")
83
84 with subtest("Graphical services are running"):
85 machine.succeed(f"{sysu} is-active graphical-session.target")
86 machine.succeed(f"{sysu} is-active urxvtd")
87 machine.succeed(f"{sysu} is-active xss-lock")
88
89 with subtest("Can interact with the WM"):
90 machine.wait_until_succeeds("pgrep sxhkd")
91 machine.wait_until_succeeds("pgrep bspwm")
92 # spawn some terminals
93 machine.send_key("meta_l-ret", delay=0.5)
94 machine.send_key("meta_l-ret", delay=0.5)
95 machine.send_key("meta_l-ret", delay=0.5)
96 # Note: this tests that resources have beeen loaded
97 machine.wait_for_window("myterm")
98 machine.screenshot("screenshot.png")
99
100 with subtest("Can stop the X server"):
101 # kill the WM
102 machine.send_key("meta_l-alt-esc")
103 machine.wait_until_tty_matches("1", prompt)
104
105 with subtest("Graphical session has stopped"):
106 machine.fail(f"{sysu} is-active graphical-session.target")
107 machine.fail(f"{sysu} is-active urxvtd")
108 machine.fail(f"{sysu} is-active xss-lock")
109 '';
110 };
111
112 imperative = runTest {
113 name = "startx-imperative";
114 meta.maintainers = with pkgs.lib.maintainers; [ rnhmjoj ];
115
116 nodes.machine =
117 { pkgs, ... }:
118 {
119 services.getty.autologinUser = "root";
120 programs.bash.promptInit = "PS1='# '";
121
122 # startx+twm setup
123 services.xserver = {
124 enable = true;
125 windowManager.twm.enable = true;
126 displayManager.startx.enable = true;
127 displayManager.startx.generateScript = false;
128 };
129
130 # enable some user services
131 security.polkit.enable = true;
132 services.urxvtd.enable = true;
133 programs.xss-lock.enable = true;
134 };
135
136 testScript = ''
137 import textwrap
138
139 sysu = "env XDG_RUNTIME_DIR=/run/user/0 systemctl --user";
140 prompt = "# "
141
142 with subtest("Wait for the autologin"):
143 machine.wait_until_tty_matches("1", prompt)
144
145 with subtest("Setup dotfiles"):
146 machine.execute(textwrap.dedent("""
147 cat <<EOF > ~/.Xresources
148 urxvt*foreground: #9b9081
149 urxvt*background: #181b20
150 urxvt*scrollBar: false
151 urxvt*title: myterm
152 urxvt*geometry: 20x20+40+40
153 EOF
154 cat <<EOF > ~/.twmrc
155 "Return" = meta : all : f.exec "urxvtc"
156 "Escape" = meta : all : f.quit
157 EOF
158 cat <<EOF > ~/.xinitrc
159 xrdb -load ~/.Xresources
160 xsetroot -solid '#343d46'
161 xsetroot -cursor_name trek
162 # start user services
163 systemctl --user import-environment DISPLAY XDG_SESSION_ID
164 systemctl --user start nixos-fake-graphical-session.target
165 # run the window manager
166 twm
167 # stop services and all subprocesses
168 systemctl --user stop nixos-fake-graphical-session.target
169 EOF
170 """))
171
172 with subtest("Can start the X server"):
173 machine.send_chars("startx\n")
174 machine.wait_for_x()
175
176 with subtest("Graphical services are running"):
177 machine.succeed(f"{sysu} is-active graphical-session.target")
178 machine.succeed(f"{sysu} is-active urxvtd")
179 machine.succeed(f"{sysu} is-active xss-lock")
180
181 with subtest("Can interact with the WM"):
182 machine.wait_until_succeeds("pgrep twm")
183 # spawn a terminal
184 machine.send_key("alt-ret")
185 machine.wait_for_window("myterm")
186 machine.screenshot("screenshot.png")
187
188 with subtest("Can stop the X server"):
189 # kill the WM
190 machine.send_key("alt-esc")
191 machine.wait_until_tty_matches("1", prompt)
192
193 with subtest("Graphical session has stopped"):
194 machine.fail(f"{sysu} is-active graphical-session.target")
195 machine.fail(f"{sysu} is-active urxvtd")
196 machine.fail(f"{sysu} is-active xss-lock")
197 '';
198 };
199
200}