1import ./make-test-python.nix (
2 { pkgs, lib, ... }:
3 {
4 name = "sway";
5 meta = {
6 maintainers = with lib.maintainers; [
7 primeos
8 synthetica
9 ];
10 };
11
12 # testScriptWithTypes:49: error: Cannot call function of unknown type
13 # (machine.succeed if succeed else machine.execute)(
14 # ^
15 # Found 1 error in 1 file (checked 1 source file)
16 skipTypeCheck = true;
17
18 nodes.machine =
19 { config, ... }:
20 {
21 # Automatically login on tty1 as a normal user:
22 imports = [ ./common/user-account.nix ];
23 services.getty.autologinUser = "alice";
24
25 environment = {
26 # For glinfo and wayland-info:
27 systemPackages = with pkgs; [
28 mesa-demos
29 wayland-utils
30 alacritty
31 ];
32 # Use a fixed SWAYSOCK path (for swaymsg):
33 variables = {
34 "SWAYSOCK" = "/tmp/sway-ipc.sock";
35 # TODO: Investigate if we can get hardware acceleration to work (via
36 # virtio-gpu and Virgil). We currently have to use the Pixman software
37 # renderer since the GLES2 renderer doesn't work inside the VM (even
38 # with WLR_RENDERER_ALLOW_SOFTWARE):
39 # "WLR_RENDERER_ALLOW_SOFTWARE" = "1";
40 "WLR_RENDERER" = "pixman";
41 };
42 # For convenience:
43 shellAliases = {
44 test-x11 = "glinfo | tee /tmp/test-x11.out && touch /tmp/test-x11-exit-ok";
45 test-wayland = "wayland-info | tee /tmp/test-wayland.out && touch /tmp/test-wayland-exit-ok";
46 };
47
48 # To help with OCR:
49 etc."xdg/foot/foot.ini".text = lib.generators.toINI { } {
50 main = {
51 font = "inconsolata:size=14";
52 };
53 colors = rec {
54 foreground = "000000";
55 background = "ffffff";
56 regular2 = foreground;
57 };
58 };
59
60 etc."gpg-agent.conf".text = ''
61 pinentry-timeout 86400
62 '';
63 };
64
65 fonts.packages = [ pkgs.inconsolata ];
66
67 # Automatically configure and start Sway when logging in on tty1:
68 programs.bash.loginShellInit = ''
69 if [ "$(tty)" = "/dev/tty1" ]; then
70 set -e
71
72 mkdir -p ~/.config/sway
73 sed s/Mod4/Mod1/ /etc/sway/config > ~/.config/sway/config
74
75 sway --validate
76 sway && touch /tmp/sway-exit-ok
77 fi
78 '';
79
80 programs.sway.enable = true;
81
82 # To test pinentry via gpg-agent:
83 programs.gnupg.agent.enable = true;
84
85 # Need to switch to a different GPU driver than the default one (-vga std) so that Sway can launch:
86 virtualisation.qemu.options = [ "-vga none -device virtio-gpu-pci" ];
87 };
88
89 testScript =
90 { nodes, ... }:
91 ''
92 import shlex
93 import json
94
95 q = shlex.quote
96 NODE_GROUPS = ["nodes", "floating_nodes"]
97
98
99 def swaymsg(command: str = "", succeed=True, type="command"):
100 assert command != "" or type != "command", "Must specify command or type"
101 shell = q(f"swaymsg -t {q(type)} -- {q(command)}")
102 with machine.nested(
103 f"sending swaymsg {shell!r}" + " (allowed to fail)" * (not succeed)
104 ):
105 ret = (machine.succeed if succeed else machine.execute)(
106 f"su - alice -c {shell}"
107 )
108
109 # execute also returns a status code, but disregard.
110 if not succeed:
111 _, ret = ret
112
113 if not succeed and not ret:
114 return None
115
116 parsed = json.loads(ret)
117 return parsed
118
119
120 def walk(tree):
121 yield tree
122 for group in NODE_GROUPS:
123 for node in tree.get(group, []):
124 yield from walk(node)
125
126
127 def wait_for_window(pattern):
128 def func(last_chance):
129 nodes = (node["name"] for node in walk(swaymsg(type="get_tree")))
130
131 if last_chance:
132 nodes = list(nodes)
133 machine.log(f"Last call! Current list of windows: {nodes}")
134
135 return any(pattern in name for name in nodes)
136
137 retry(func)
138
139 start_all()
140 machine.wait_for_unit("multi-user.target")
141
142 # To check the version:
143 print(machine.succeed("sway --version"))
144
145 # Wait for Sway to complete startup:
146 machine.wait_for_file("/run/user/1000/wayland-1")
147 machine.wait_for_file("/tmp/sway-ipc.sock")
148
149 # Test XWayland (foot does not support X):
150 swaymsg("exec WINIT_UNIX_BACKEND=x11 WAYLAND_DISPLAY= alacritty")
151 wait_for_window("alice@machine")
152 machine.send_chars("test-x11\n")
153 machine.wait_for_file("/tmp/test-x11-exit-ok")
154 print(machine.succeed("cat /tmp/test-x11.out"))
155 machine.copy_from_vm("/tmp/test-x11.out")
156 machine.screenshot("alacritty_glinfo")
157 machine.succeed("pkill alacritty")
158
159 # Start a terminal (foot) on workspace 3:
160 machine.send_key("alt-3")
161 machine.sleep(3)
162 machine.send_key("alt-ret")
163 wait_for_window("alice@machine")
164 machine.send_chars("test-wayland\n")
165 machine.wait_for_file("/tmp/test-wayland-exit-ok")
166 print(machine.succeed("cat /tmp/test-wayland.out"))
167 machine.copy_from_vm("/tmp/test-wayland.out")
168 machine.screenshot("foot_wayland_info")
169 machine.send_key("alt-shift-q")
170 machine.wait_until_fails("pgrep foot")
171
172 # Test gpg-agent starting pinentry-gnome3 via D-Bus (tests if
173 # $WAYLAND_DISPLAY is correctly imported into the D-Bus user env):
174 swaymsg("exec mkdir -p ~/.gnupg")
175 swaymsg("exec cp /etc/gpg-agent.conf ~/.gnupg")
176
177 swaymsg("exec DISPLAY=INVALID gpg --no-tty --yes --quick-generate-key test", succeed=False)
178 machine.wait_until_succeeds("pgrep --exact gpg")
179 wait_for_window("gpg")
180 machine.succeed("pgrep --exact gpg")
181 machine.screenshot("gpg_pinentry")
182 machine.send_key("alt-shift-q")
183 machine.wait_until_fails("pgrep --exact gpg")
184
185 # Test swaynag:
186 def get_height():
187 return [node['rect']['height'] for node in walk(swaymsg(type="get_tree")) if node['focused']][0]
188
189 before = get_height()
190 machine.send_key("alt-shift-e")
191 retry(lambda _: get_height() < before)
192 machine.screenshot("sway_exit")
193
194 swaymsg("exec swaylock")
195 machine.wait_until_succeeds("pgrep -xf swaylock")
196 machine.sleep(3)
197 machine.send_chars("${nodes.machine.config.users.users.alice.password}")
198 machine.send_key("ret")
199 machine.wait_until_fails("pgrep -xf swaylock")
200
201 # Exit Sway and verify process exit status 0:
202 swaymsg("exit", succeed=False)
203 machine.wait_until_fails("pgrep -xf sway")
204 machine.wait_for_file("/tmp/sway-exit-ok")
205 '';
206 }
207)