From 3a435342e2e5e2bff1d3331c2bd9e70f25693ea2 Mon Sep 17 00:00:00 2001 From: Andrew Marshall Date: Thu, 9 Nov 2023 13:19:45 -0500 Subject: [PATCH] sway: check config file validity Helps avoid successful build but Sway failing to start. To meaningfully test this, I had to actually use `pkgs.sway` in the test rather than the stub, but left all other tests using the stub and changed them to skipping the test. We need to pass `--unsupported-gpu` as Sway checks for `nvidia` in `/proc/modules`, and the Nix sandbox has `/proc/modules` available. --- .../services/window-managers/i3-sway/sway.nix | 24 +++++++++++++++---- .../services/window-managers/sway/default.nix | 1 + .../sway/sway-bar-focused-colors.nix | 1 + ...sway-bindkeys-to-code-and-extra-config.nix | 1 + .../sway/sway-check-config.nix | 12 ++++++++++ .../window-managers/sway/sway-default.nix | 1 + .../sway/sway-followmouse-legacy.nix | 1 + .../window-managers/sway/sway-followmouse.nix | 1 + .../window-managers/sway/sway-modules.nix | 1 + .../window-managers/sway/sway-no-xwayland.nix | 1 + .../window-managers/sway/sway-null-config.nix | 1 + .../sway/sway-null-package.nix | 1 + .../window-managers/sway/sway-post-2003.nix | 1 + .../sway/sway-systemd-autostart.nix | 1 + .../sway/sway-systemd-variables.nix | 1 + .../sway/sway-workspace-default.nix | 1 + .../sway/sway-workspace-output.nix | 1 + 17 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 tests/modules/services/window-managers/sway/sway-check-config.nix diff --git a/modules/services/window-managers/i3-sway/sway.nix b/modules/services/window-managers/i3-sway/sway.nix index ea63e78c..bc20e9d9 100644 --- a/modules/services/window-managers/i3-sway/sway.nix +++ b/modules/services/window-managers/i3-sway/sway.nix @@ -258,12 +258,21 @@ let variables = concatStringsSep " " cfg.systemd.variables; extraCommands = concatStringsSep " && " cfg.systemd.extraCommands; + swayPackage = if cfg.package == null then pkgs.sway else cfg.package; systemdActivation = '' exec "${pkgs.dbus}/bin/dbus-update-activation-environment --systemd ${variables}; ${extraCommands}"''; configFile = pkgs.writeTextFile { name = "sway.conf"; - text = (concatStringsSep "\n" + + # Sway always does some init, see https://github.com/swaywm/sway/issues/4691 + checkPhase = lib.optionalString cfg.checkConfig '' + export DBUS_SESSION_BUS_ADDRESS=/dev/null + export XDG_RUNTIME_DIR=$(mktemp -d) + ${pkgs.xvfb-run}/bin/xvfb-run ${swayPackage}/bin/sway --config "$target" --validate --unsupported-gpu + ''; + + text = concatStringsSep "\n" ((optional (cfg.extraConfigEarly != "") cfg.extraConfigEarly) ++ (if cfg.config != null then with cfg.config; @@ -319,7 +328,7 @@ let else [ ]) ++ (optional cfg.systemd.enable systemdActivation) ++ (optional (!cfg.xwayland) "xwayland disable") - ++ [ cfg.extraConfig ])); + ++ [ cfg.extraConfig ]); }; defaultSwayPackage = pkgs.sway.override { @@ -469,6 +478,13 @@ in { description = "Sway configuration options."; }; + checkConfig = mkOption { + type = types.bool; + default = true; + description = + "If enabled (the default), validates the generated config file."; + }; + extraConfig = mkOption { type = types.lines; default = ""; @@ -506,9 +522,7 @@ in { home.packages = optional (cfg.package != null) cfg.package ++ optional cfg.xwayland pkgs.xwayland; - xdg.configFile."sway/config" = let - swayPackage = if cfg.package == null then pkgs.sway else cfg.package; - in { + xdg.configFile."sway/config" = { source = configFile; onChange = '' swaySocket="''${XDG_RUNTIME_DIR:-/run/user/$UID}/sway-ipc.$UID.$(${pkgs.procps}/bin/pgrep --uid $UID -x sway || true).sock" diff --git a/tests/modules/services/window-managers/sway/default.nix b/tests/modules/services/window-managers/sway/default.nix index e3ce4a9c..7627aab0 100644 --- a/tests/modules/services/window-managers/sway/default.nix +++ b/tests/modules/services/window-managers/sway/default.nix @@ -5,6 +5,7 @@ sway-default = ./sway-default.nix; sway-followmouse = ./sway-followmouse.nix; sway-followmouse-legacy = ./sway-followmouse-legacy.nix; + sway-check-config = ./sway-check-config.nix; sway-modules = ./sway-modules.nix; sway-no-xwayland = ./sway-no-xwayland.nix; sway-null-config = ./sway-null-config.nix; diff --git a/tests/modules/services/window-managers/sway/sway-bar-focused-colors.nix b/tests/modules/services/window-managers/sway/sway-bar-focused-colors.nix index 399790fb..9488d00f 100644 --- a/tests/modules/services/window-managers/sway/sway-bar-focused-colors.nix +++ b/tests/modules/services/window-managers/sway/sway-bar-focused-colors.nix @@ -6,6 +6,7 @@ wayland.windowManager.sway = { enable = true; package = config.lib.test.mkStubPackage { outPath = "@sway@"; }; + checkConfig = false; # overriding findutils causes issues config.menu = "${pkgs.dmenu}/bin/dmenu_run"; diff --git a/tests/modules/services/window-managers/sway/sway-bindkeys-to-code-and-extra-config.nix b/tests/modules/services/window-managers/sway/sway-bindkeys-to-code-and-extra-config.nix index 0817837a..cf316988 100644 --- a/tests/modules/services/window-managers/sway/sway-bindkeys-to-code-and-extra-config.nix +++ b/tests/modules/services/window-managers/sway/sway-bindkeys-to-code-and-extra-config.nix @@ -6,6 +6,7 @@ wayland.windowManager.sway = { enable = true; package = config.lib.test.mkStubPackage { outPath = "@sway@"; }; + checkConfig = false; # overriding findutils causes issues config.menu = "${pkgs.dmenu}/bin/dmenu_run"; config.bindkeysToCode = true; diff --git a/tests/modules/services/window-managers/sway/sway-check-config.nix b/tests/modules/services/window-managers/sway/sway-check-config.nix new file mode 100644 index 00000000..e566e4fd --- /dev/null +++ b/tests/modules/services/window-managers/sway/sway-check-config.nix @@ -0,0 +1,12 @@ +{ config, lib, ... }: + +lib.mkIf config.test.enableBig { + wayland.windowManager.sway = { + enable = true; + checkConfig = true; + }; + + nmt.script = '' + assertFileExists home-files/.config/sway/config + ''; +} diff --git a/tests/modules/services/window-managers/sway/sway-default.nix b/tests/modules/services/window-managers/sway/sway-default.nix index 59527247..c62effbe 100644 --- a/tests/modules/services/window-managers/sway/sway-default.nix +++ b/tests/modules/services/window-managers/sway/sway-default.nix @@ -6,6 +6,7 @@ wayland.windowManager.sway = { enable = true; package = config.lib.test.mkStubPackage { outPath = "@sway@"; }; + checkConfig = false; # overriding findutils causes issues config.menu = "${pkgs.dmenu}/bin/dmenu_run"; }; diff --git a/tests/modules/services/window-managers/sway/sway-followmouse-legacy.nix b/tests/modules/services/window-managers/sway/sway-followmouse-legacy.nix index 03f64fc2..3bce4850 100644 --- a/tests/modules/services/window-managers/sway/sway-followmouse-legacy.nix +++ b/tests/modules/services/window-managers/sway/sway-followmouse-legacy.nix @@ -6,6 +6,7 @@ wayland.windowManager.sway = { enable = true; package = config.lib.test.mkStubPackage { outPath = "@sway@"; }; + checkConfig = false; config = { focus.followMouse = false; diff --git a/tests/modules/services/window-managers/sway/sway-followmouse.nix b/tests/modules/services/window-managers/sway/sway-followmouse.nix index b8a2bc16..de2e91b3 100644 --- a/tests/modules/services/window-managers/sway/sway-followmouse.nix +++ b/tests/modules/services/window-managers/sway/sway-followmouse.nix @@ -6,6 +6,7 @@ wayland.windowManager.sway = { enable = true; package = config.lib.test.mkStubPackage { outPath = "@sway@"; }; + checkConfig = false; config = { focus.followMouse = "always"; diff --git a/tests/modules/services/window-managers/sway/sway-modules.nix b/tests/modules/services/window-managers/sway/sway-modules.nix index b8aa58c5..287dec17 100644 --- a/tests/modules/services/window-managers/sway/sway-modules.nix +++ b/tests/modules/services/window-managers/sway/sway-modules.nix @@ -6,6 +6,7 @@ wayland.windowManager.sway = { enable = true; package = config.lib.test.mkStubPackage { outPath = "@sway@"; }; + checkConfig = false; # overriding findutils causes issues config = { menu = "${pkgs.dmenu}/bin/dmenu_run"; diff --git a/tests/modules/services/window-managers/sway/sway-no-xwayland.nix b/tests/modules/services/window-managers/sway/sway-no-xwayland.nix index b4091267..5eded3b8 100644 --- a/tests/modules/services/window-managers/sway/sway-no-xwayland.nix +++ b/tests/modules/services/window-managers/sway/sway-no-xwayland.nix @@ -6,6 +6,7 @@ wayland.windowManager.sway = { enable = true; package = config.lib.test.mkStubPackage { outPath = "@sway@"; }; + checkConfig = false; config = null; systemd.enable = false; xwayland = false; diff --git a/tests/modules/services/window-managers/sway/sway-null-config.nix b/tests/modules/services/window-managers/sway/sway-null-config.nix index a859e04e..46e4df23 100644 --- a/tests/modules/services/window-managers/sway/sway-null-config.nix +++ b/tests/modules/services/window-managers/sway/sway-null-config.nix @@ -6,6 +6,7 @@ wayland.windowManager.sway = { enable = true; package = config.lib.test.mkStubPackage { outPath = "@sway@"; }; + checkConfig = false; config = null; systemd.enable = false; }; diff --git a/tests/modules/services/window-managers/sway/sway-null-package.nix b/tests/modules/services/window-managers/sway/sway-null-package.nix index a449b42c..042830d1 100644 --- a/tests/modules/services/window-managers/sway/sway-null-package.nix +++ b/tests/modules/services/window-managers/sway/sway-null-package.nix @@ -9,6 +9,7 @@ wayland.windowManager.sway = { enable = true; package = null; + checkConfig = false; config.menu = "${pkgs.dmenu}/bin/dmenu_run"; }; diff --git a/tests/modules/services/window-managers/sway/sway-post-2003.nix b/tests/modules/services/window-managers/sway/sway-post-2003.nix index 13aac533..3617a3b1 100644 --- a/tests/modules/services/window-managers/sway/sway-post-2003.nix +++ b/tests/modules/services/window-managers/sway/sway-post-2003.nix @@ -8,6 +8,7 @@ wayland.windowManager.sway = { enable = true; package = config.lib.test.mkStubPackage { outPath = "@sway@"; }; + checkConfig = false; # overriding findutils causes issues config.menu = "${pkgs.dmenu}/bin/dmenu_run"; }; diff --git a/tests/modules/services/window-managers/sway/sway-systemd-autostart.nix b/tests/modules/services/window-managers/sway/sway-systemd-autostart.nix index 5a1d90e2..5c32c19c 100644 --- a/tests/modules/services/window-managers/sway/sway-systemd-autostart.nix +++ b/tests/modules/services/window-managers/sway/sway-systemd-autostart.nix @@ -6,6 +6,7 @@ wayland.windowManager.sway = { enable = true; package = config.lib.test.mkStubPackage { outPath = "@sway@"; }; + checkConfig = false; # overriding findutils causes issues config.menu = "${pkgs.dmenu}/bin/dmenu_run"; diff --git a/tests/modules/services/window-managers/sway/sway-systemd-variables.nix b/tests/modules/services/window-managers/sway/sway-systemd-variables.nix index aae02fef..f0b42ecc 100644 --- a/tests/modules/services/window-managers/sway/sway-systemd-variables.nix +++ b/tests/modules/services/window-managers/sway/sway-systemd-variables.nix @@ -6,6 +6,7 @@ wayland.windowManager.sway = { enable = true; package = config.lib.test.mkStubPackage { outPath = "@sway@"; }; + checkConfig = false; # overriding findutils causes issues config.menu = "${pkgs.dmenu}/bin/dmenu_run"; diff --git a/tests/modules/services/window-managers/sway/sway-workspace-default.nix b/tests/modules/services/window-managers/sway/sway-workspace-default.nix index 6af944eb..e5637fd9 100644 --- a/tests/modules/services/window-managers/sway/sway-workspace-default.nix +++ b/tests/modules/services/window-managers/sway/sway-workspace-default.nix @@ -6,6 +6,7 @@ wayland.windowManager.sway = { enable = true; package = config.lib.test.mkStubPackage { outPath = "@sway@"; }; + checkConfig = false; # overriding findutils causes issues config.menu = "${pkgs.dmenu}/bin/dmenu_run"; config.defaultWorkspace = "workspace number 9"; diff --git a/tests/modules/services/window-managers/sway/sway-workspace-output.nix b/tests/modules/services/window-managers/sway/sway-workspace-output.nix index 47fb7527..dcbff91e 100644 --- a/tests/modules/services/window-managers/sway/sway-workspace-output.nix +++ b/tests/modules/services/window-managers/sway/sway-workspace-output.nix @@ -15,6 +15,7 @@ in { wayland.windowManager.sway = { enable = true; package = config.lib.test.mkStubPackage { outPath = "@sway@"; }; + checkConfig = false; # overriding findutils causes issues config.menu = "${pkgs.dmenu}/bin/dmenu_run";