vuls: init at 0.27.0 (#348530)
[NixPkgs.git] / nixos / tests / swayfx.nix
blobfe1caf8fffa8b5879f5ef56b4ae7cce66083db24
1 import ./make-test-python.nix (
2   { pkgs, lib, ... }:
3   {
4     name = "swayfx";
5     meta = {
6       maintainers = with lib.maintainers; [ ];
7     };
9     # testScriptWithTypes:49: error: Cannot call function of unknown type
10     #           (machine.succeed if succeed else machine.execute)(
11     #           ^
12     # Found 1 error in 1 file (checked 1 source file)
13     skipTypeCheck = true;
15     nodes.machine =
16       { config, ... }:
17       {
18         # Automatically login on tty1 as a normal user:
19         imports = [ ./common/user-account.nix ];
20         services.getty.autologinUser = "alice";
22         environment = {
23           # For glinfo and wayland-info:
24           systemPackages = with pkgs; [
25             mesa-demos
26             wayland-utils
27             alacritty
28           ];
29           # Use a fixed SWAYSOCK path (for swaymsg):
30           variables = {
31             "SWAYSOCK" = "/tmp/sway-ipc.sock";
32             # TODO: Investigate if we can get hardware acceleration to work (via
33             # virtio-gpu and Virgil). We currently have to use the Pixman software
34             # renderer since the GLES2 renderer doesn't work inside the VM (even
35             # with WLR_RENDERER_ALLOW_SOFTWARE):
36             # "WLR_RENDERER_ALLOW_SOFTWARE" = "1";
37             "WLR_RENDERER" = "pixman";
38           };
39           # For convenience:
40           shellAliases = {
41             test-x11 = "glinfo | tee /tmp/test-x11.out && touch /tmp/test-x11-exit-ok";
42             test-wayland = "wayland-info | tee /tmp/test-wayland.out && touch /tmp/test-wayland-exit-ok";
43           };
45           # To help with OCR:
46           etc."xdg/foot/foot.ini".text = lib.generators.toINI { } {
47             main = {
48               font = "inconsolata:size=14";
49             };
50             colors = rec {
51               foreground = "000000";
52               background = "ffffff";
53               regular2 = foreground;
54             };
55           };
57           etc."gpg-agent.conf".text = ''
58             pinentry-timeout 86400
59           '';
60         };
62         fonts.packages = [ pkgs.inconsolata ];
64         # Automatically configure and start Sway when logging in on tty1:
65         programs.bash.loginShellInit = ''
66           if [ "$(tty)" = "/dev/tty1" ]; then
67             set -e
69             mkdir -p ~/.config/sway
70             sed s/Mod4/Mod1/ /etc/sway/config > ~/.config/sway/config
72             sway --validate
73             sway && touch /tmp/sway-exit-ok
74           fi
75         '';
77         programs.sway = {
78           enable = true;
79           package = pkgs.swayfx.override { isNixOS = true; };
80         };
82         # To test pinentry via gpg-agent:
83         programs.gnupg.agent.enable = true;
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       };
89     testScript =
90       { nodes, ... }:
91       ''
92         import shlex
93         import json
95         q = shlex.quote
96         NODE_GROUPS = ["nodes", "floating_nodes"]
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                 )
109             # execute also returns a status code, but disregard.
110             if not succeed:
111                 _, ret = ret
113             if not succeed and not ret:
114                 return None
116             parsed = json.loads(ret)
117             return parsed
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)
127         def wait_for_window(pattern):
128             def func(last_chance):
129                 nodes = (node["name"] for node in walk(swaymsg(type="get_tree")))
131                 if last_chance:
132                     nodes = list(nodes)
133                     machine.log(f"Last call! Current list of windows: {nodes}")
135                 return any(pattern in name for name in nodes)
137             retry(func)
139         start_all()
140         machine.wait_for_unit("multi-user.target")
142         # To check the version:
143         print(machine.succeed("sway --version"))
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")
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")
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")
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")
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")
185         # Test swaynag:
186         def get_height():
187             return [node['rect']['height'] for node in walk(swaymsg(type="get_tree")) if node['focused']][0]
189         before = get_height()
190         machine.send_key("alt-shift-e")
191         retry(lambda _: get_height() < before)
192         machine.screenshot("sway_exit")
194         swaymsg("exec swaylock")
195         machine.wait_until_succeeds("pgrep -x 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 -x swaylock")
201         # Exit Sway and verify process exit status 0:
202         swaymsg("exit", succeed=False)
203         machine.wait_until_fails("pgrep -x sway")
204         machine.wait_for_file("/tmp/sway-exit-ok")
205       '';
206   }