1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2025-01-11 11:39:49 +01:00

waybar: allow using attrs for settings (#2547)

Co-authored-by: Bruno Inec <binec@scaleway.com>
This commit is contained in:
Bruno Inec 2021-12-19 06:21:15 +01:00 committed by GitHub
parent 8b44e81978
commit 3db6036775
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 105 additions and 43 deletions

View file

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

View file

@ -3,5 +3,6 @@
./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;
waybar-settings-with-attrs = ./settings-with-attrs.nix;
waybar-deprecated-modules-option = ./deprecated-modules-option.nix; waybar-deprecated-modules-option = ./deprecated-modules-option.nix;
} }

View file

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

View file

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

View file

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