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