waybar: fix css identifier check (#1687)

Fixes #1686
This commit is contained in:
Nicolas Berbiche 2020-12-30 18:17:39 -05:00 committed by GitHub
parent 9d53afb709
commit f4b5ae026c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 82 additions and 99 deletions

View file

@ -2,8 +2,8 @@
let let
inherit (lib) inherit (lib)
any attrByPath attrNames concatMap concatMapStringsSep elem filter any attrByPath attrNames concatMap concatMapStringsSep elem elemAt filter
filterAttrs flip foldl' hasPrefix mergeAttrs optionalAttrs removePrefix filterAttrs flip foldl' hasPrefix head length mergeAttrs optionalAttrs
stringLength subtractLists types unique; stringLength subtractLists types unique;
inherit (lib.options) literalExample mkEnableOption mkOption; inherit (lib.options) literalExample mkEnableOption mkOption;
inherit (lib.modules) mkIf mkMerge; inherit (lib.modules) mkIf mkMerge;
@ -40,13 +40,19 @@ let
"bluetooth" "bluetooth"
]; ];
# Allow specifying a CSS class after the default module name # Allow specifying a CSS id after the default module name
isValidDefaultModuleName = x: 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; defaultModuleNames;
isValidCustomModuleName = x: isValidCustomModuleName = x: hasPrefix "custom/" x && stringLength x > 7;
hasPrefix "custom/" x && stringLength x > 7 || isValidDefaultModuleName x;
margins = let margins = let
mkMargin = name: { mkMargin = name: {
@ -321,12 +327,14 @@ in {
# Modules declared in `modules` but not referenced in `modules-{left,center,right}` # Modules declared in `modules` but not referenced in `modules-{left,center,right}`
unreferencedModules = subtractLists allModules declaredModules; unreferencedModules = subtractLists allModules declaredModules;
# Modules listed in modules-{left,center,right} that are not default modules # 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` # Modules referenced in `modules-{left,center,right}` but not declared in `modules`
undefinedModules = subtractLists declaredModules nonDefaultModules; undefinedModules = subtractLists declaredModules nonDefaultModules;
# Check for invalid module names # Check for invalid module names
invalidModuleNames = invalidModuleNames = filter
filter (m: !isValidCustomModuleName m) declaredModules; (m: !isValidCustomModuleName m && !isValidDefaultModuleName m)
declaredModules;
in { in {
# The Waybar bar configuration (since config.settings is a list) # The Waybar bar configuration (since config.settings is a list)
inherit settings; inherit settings;

View file

@ -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}
'';
};
}

View file

@ -3,6 +3,5 @@
./systemd-with-graphical-session-target.nix; ./systemd-with-graphical-session-target.nix;
waybar-styling = ./styling.nix; waybar-styling = ./styling.nix;
waybar-settings-complex = ./settings-complex.nix; waybar-settings-complex = ./settings-complex.nix;
# Broken configuration from https://github.com/nix-community/home-manager/pull/1329#issuecomment-653253069 waybar-warnings = ./warnings-tests.nix;
waybar-broken-settings = ./broken-settings.nix;
} }

View file

@ -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 = '' nmt.script = ''
assertPathNotExists home-files/.config/waybar/style.css assertPathNotExists home-files/.config/waybar/style.css
assertFileContent \ assertFileContent \

View file

@ -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"
]
}
]
''
}
'';
};
}