waybar: allow using attrs for settings (#2547)

Co-authored-by: Bruno Inec <binec@scaleway.com>
main
Bruno Inec 2 years ago committed by GitHub
parent 8b44e81978
commit 3db6036775
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      modules/programs/waybar.nix
  2. 1
      tests/modules/programs/waybar/default.nix
  3. 16
      tests/modules/programs/waybar/settings-complex-expected.json
  4. 79
      tests/modules/programs/waybar/settings-complex.nix
  5. 29
      tests/modules/programs/waybar/settings-with-attrs.nix

@ -156,7 +156,7 @@ in {
};
settings = mkOption {
type = listOf waybarBarConfig;
type = either (listOf waybarBarConfig) (attrsOf waybarBarConfig);
default = [ ];
description = ''
Configuration for Waybar, see <link
@ -164,8 +164,8 @@ in {
for supported values.
'';
example = literalExpression ''
[
{
{
mainBar = {
layer = "top";
position = "top";
height = 30;
@ -190,8 +190,8 @@ in {
''';
};
};
}
]
};
}
'';
};
@ -242,8 +242,15 @@ in {
settingsModules =
optionalAttrs (configuration.modules != { }) configuration.modules;
in removeNulls (settingsWithoutModules // settingsModules);
# Allow using attrs for settings instead of a list in order to more easily override
settings = if builtins.isAttrs cfg.settings then
lib.attrValues cfg.settings
else
cfg.settings;
# The clean list of configurations
finalConfiguration = map makeConfiguration cfg.settings;
finalConfiguration = map makeConfiguration settings;
configSource = jsonFormat.generate "waybar-config.json" finalConfiguration;
@ -255,7 +262,7 @@ in {
({
assertion =
if lib.versionAtLeast config.home.stateVersion "22.05" then
all (x: !hasAttr "modules" x) cfg.settings
all (x: !hasAttr "modules" x) settings
else
true;
message = ''
@ -267,7 +274,7 @@ in {
home.packages = [ cfg.package ];
xdg.configFile."waybar/config" = mkIf (cfg.settings != [ ]) {
xdg.configFile."waybar/config" = mkIf (settings != [ ]) {
source = configSource;
onChange = ''
${pkgs.procps}/bin/pkill -u $USER -USR2 waybar || true

@ -3,5 +3,6 @@
./systemd-with-graphical-session-target.nix;
waybar-styling = ./styling.nix;
waybar-settings-complex = ./settings-complex.nix;
waybar-settings-with-attrs = ./settings-with-attrs.nix;
waybar-deprecated-modules-option = ./deprecated-modules-option.nix;
}

@ -43,5 +43,21 @@
"all-outputs": true,
"disable-scroll": true
}
},
{
"modules-center": [
"clock"
],
"modules-left": [
"sway/mode"
],
"modules-right": [],
"output": [
"!DP-1"
],
"position": "bottom",
"sway/mode": {
"tooltip": true
}
}
]

@ -9,43 +9,52 @@ with lib;
programs.waybar = {
package = config.lib.test.mkStubPackage { outPath = "@waybar@"; };
enable = true;
settings = [{
layer = "top";
position = "top";
height = 30;
output = [ "DP-1" ];
modules-left = [ "sway/workspaces" "sway/mode" "custom/my-module" ];
modules-center = [ "sway/window" ];
modules-right = [
"idle_inhibitor"
"pulseaudio"
"network"
"cpu"
"memory"
"backlight"
"tray"
"battery#bat1"
"battery#bat2"
"clock"
];
settings = [
{
layer = "top";
position = "top";
height = 30;
output = [ "DP-1" ];
modules-left = [ "sway/workspaces" "sway/mode" "custom/my-module" ];
modules-center = [ "sway/window" ];
modules-right = [
"idle_inhibitor"
"pulseaudio"
"network"
"cpu"
"memory"
"backlight"
"tray"
"battery#bat1"
"battery#bat2"
"clock"
];
modules = {
"sway/workspaces" = {
disable-scroll = true;
all-outputs = true;
modules = {
"sway/workspaces" = {
disable-scroll = true;
all-outputs = true;
};
"sway/mode" = { tooltip = false; };
"sway/window" = { max-length = 120; };
"idle_inhibitor" = { format = "{icon}"; };
"custom/my-module" = {
format = "hello from {}";
exec = let
dummyScript =
config.lib.test.mkStubPackage { outPath = "@dummy@"; };
in "${dummyScript}/bin/dummy";
};
};
"sway/mode" = { tooltip = false; };
"sway/window" = { max-length = 120; };
"idle_inhibitor" = { format = "{icon}"; };
"custom/my-module" = {
format = "hello from {}";
exec = let
dummyScript =
config.lib.test.mkStubPackage { outPath = "@dummy@"; };
in "${dummyScript}/bin/dummy";
};
};
}];
}
{
position = "bottom";
output = [ "!DP-1" ];
modules-left = [ "sway/mode" ];
modules-center = [ "clock" ];
modules = { "sway/mode" = { tooltip = true; }; };
}
];
};
nmt.script = ''

@ -0,0 +1,29 @@
{ config, lib, pkgs, ... }:
with lib;
{
config = {
home.stateVersion = "21.11";
programs.waybar = {
package = config.lib.test.mkStubPackage { outPath = "@waybar@"; };
enable = true;
settings = let
settingsComplex = (import ./settings-complex.nix {
inherit config lib pkgs;
}).config.programs.waybar.settings;
in {
mainBar = builtins.head settingsComplex;
secondaryBar = builtins.elemAt settingsComplex 1;
};
};
nmt.script = ''
assertPathNotExists home-files/.config/waybar/style.css
assertFileContent \
home-files/.config/waybar/config \
${./settings-complex-expected.json}
'';
};
}
Loading…
Cancel
Save