diff --git a/modules/programs/waybar.nix b/modules/programs/waybar.nix index 7234278f..5472a0ef 100644 --- a/modules/programs/waybar.nix +++ b/modules/programs/waybar.nix @@ -2,8 +2,8 @@ let inherit (lib) - any attrByPath attrNames concatMap concatMapStringsSep elem filter - filterAttrs flip foldl' hasPrefix mergeAttrs optionalAttrs removePrefix + any attrByPath attrNames concatMap concatMapStringsSep elem elemAt filter + filterAttrs flip foldl' hasPrefix head length mergeAttrs optionalAttrs stringLength subtractLists types unique; inherit (lib.options) literalExample mkEnableOption mkOption; inherit (lib.modules) mkIf mkMerge; @@ -40,13 +40,19 @@ let "bluetooth" ]; - # Allow specifying a CSS class after the default module name + # Allow specifying a CSS id after the default module name isValidDefaultModuleName = x: - any (name: hasPrefix name x && hasPrefix "#" (removePrefix name x)) + any (name: + let + res = builtins.split name x; + # if exact match of default module name + in if res == [ "" [ ] ] || res == [ "" [ ] "" ] then + true + else + head res == "" && length res >= 3 && hasPrefix "#" (elemAt res 2)) defaultModuleNames; - isValidCustomModuleName = x: - hasPrefix "custom/" x && stringLength x > 7 || isValidDefaultModuleName x; + isValidCustomModuleName = x: hasPrefix "custom/" x && stringLength x > 7; margins = let mkMargin = name: { @@ -321,12 +327,14 @@ in { # Modules declared in `modules` but not referenced in `modules-{left,center,right}` unreferencedModules = subtractLists allModules declaredModules; # Modules listed in modules-{left,center,right} that are not default modules - nonDefaultModules = subtractLists defaultModuleNames allModules; + nonDefaultModules = + filter (x: !isValidDefaultModuleName x) allModules; # Modules referenced in `modules-{left,center,right}` but not declared in `modules` undefinedModules = subtractLists declaredModules nonDefaultModules; # Check for invalid module names - invalidModuleNames = - filter (m: !isValidCustomModuleName m) declaredModules; + invalidModuleNames = filter + (m: !isValidCustomModuleName m && !isValidDefaultModuleName m) + declaredModules; in { # The Waybar bar configuration (since config.settings is a list) inherit settings; diff --git a/tests/modules/programs/waybar/broken-settings.nix b/tests/modules/programs/waybar/broken-settings.nix deleted file mode 100644 index d0e13150..00000000 --- a/tests/modules/programs/waybar/broken-settings.nix +++ /dev/null @@ -1,84 +0,0 @@ -{ config, lib, pkgs, ... }: - -with lib; - -let - package = pkgs.writeScriptBin "dummy-waybar" "" // { outPath = "@waybar@"; }; - expected = pkgs.writeText "expected-json" '' - [ - { - "height": 26, - "layer": "top", - "modules-center": [ - "sway/window" - ], - "modules-left": [ - "sway/workspaces", - "sway/mode" - ], - "modules-right": [ - "idle_inhibitor", - "pulseaudio", - "network", - "cpu", - "memory", - "backlight", - "tray", - "clock" - ], - "output": [ - "DP-1", - "eDP-1", - "HEADLESS-1" - ], - "position": "top", - "sway/workspaces": { - "all-outputs": true - } - } - ] - ''; -in { - config = { - programs.waybar = { - inherit package; - enable = true; - systemd.enable = true; - settings = [{ - layer = "top"; - position = "top"; - height = 26; - output = [ "DP-1" "eDP-1" "HEADLESS-1" ]; - modules-left = [ "sway/workspaces" "sway/mode" ]; - modules-center = [ "sway/window" ]; - modules-right = [ - "idle_inhibitor" - "pulseaudio" - "network" - "cpu" - "memory" - "backlight" - "tray" - "clock" - ]; - - modules = { "sway/workspaces".all-outputs = true; }; - }]; - }; - - # Remove when https://github.com/nix-community/home-manager/issues/1686 is - # fixed. - test.asserts.warnings.enable = false; - - nmt.description = '' - Test for the broken configuration - https://github.com/nix-community/home-manager/pull/1329#issuecomment-653253069 - ''; - nmt.script = '' - assertPathNotExists home-files/.config/waybar/style.css - assertFileContent \ - home-files/.config/waybar/config \ - ${expected} - ''; - }; -} diff --git a/tests/modules/programs/waybar/default.nix b/tests/modules/programs/waybar/default.nix index cac5de66..9ffebe24 100644 --- a/tests/modules/programs/waybar/default.nix +++ b/tests/modules/programs/waybar/default.nix @@ -3,6 +3,5 @@ ./systemd-with-graphical-session-target.nix; waybar-styling = ./styling.nix; waybar-settings-complex = ./settings-complex.nix; - # Broken configuration from https://github.com/nix-community/home-manager/pull/1329#issuecomment-653253069 - waybar-broken-settings = ./broken-settings.nix; + waybar-warnings = ./warnings-tests.nix; } diff --git a/tests/modules/programs/waybar/settings-complex.nix b/tests/modules/programs/waybar/settings-complex.nix index 16ceccb6..1756ae12 100644 --- a/tests/modules/programs/waybar/settings-complex.nix +++ b/tests/modules/programs/waybar/settings-complex.nix @@ -50,10 +50,6 @@ in { }]; }; - # Remove when https://github.com/nix-community/home-manager/issues/1686 is - # fixed. - test.asserts.warnings.enable = false; - nmt.script = '' assertPathNotExists home-files/.config/waybar/style.css assertFileContent \ diff --git a/tests/modules/programs/waybar/warnings-tests.nix b/tests/modules/programs/waybar/warnings-tests.nix new file mode 100644 index 00000000..f7912dba --- /dev/null +++ b/tests/modules/programs/waybar/warnings-tests.nix @@ -0,0 +1,64 @@ +{ config, pkgs, lib, ... }: + +with lib; + +let + package = pkgs.writeScriptBin "dummy-waybar" "" // { outPath = "@waybar@"; }; +in { + config = { + programs.waybar = { + inherit package; + enable = true; + settings = [{ + modules-left = [ "custom/my-module" ]; + modules-center = + [ "this_module_is_not_a_valid_default_module_nor_custom_module" ]; + modules-right = [ + "battery#bat1" # CSS identifier is allowed + "custom/this_custom_module_doesn't_have_a_definition_in_modules" + ]; + + modules = { + "custom/this_module_is_not_referenced" = { }; + "battery#bat1" = { }; + "custom/my-module" = { }; + }; + }]; + }; + + test.asserts.warnings.expected = [ + "The module 'this_module_is_not_a_valid_default_module_nor_custom_module' defined in 'programs.waybar.settings.[].modules-center' is neither a default module or a custom module declared in 'programs.waybar.settings.[].modules'" + + "The module 'custom/this_custom_module_doesn't_have_a_definition_in_modules' defined in 'programs.waybar.settings.[].modules-right' is neither a default module or a custom module declared in 'programs.waybar.settings.[].modules'" + + "The module 'custom/this_module_is_not_referenced' defined in 'programs.waybar.settings.[].modules' is not referenced in either `modules-left`, `modules-center` or `modules-right` of Waybar's options" + ]; + + nmt.script = '' + assertPathNotExists home-files/.config/waybar/style.css + assertFileContent \ + home-files/.config/waybar/config \ + ${ + pkgs.writeText "expected-json" '' + [ + { + "battery#bat1": {}, + "custom/my-module": {}, + "custom/this_module_is_not_referenced": {}, + "modules-center": [ + "this_module_is_not_a_valid_default_module_nor_custom_module" + ], + "modules-left": [ + "custom/my-module" + ], + "modules-right": [ + "battery#bat1", + "custom/this_custom_module_doesn't_have_a_definition_in_modules" + ] + } + ] + '' + } + ''; + }; +}