1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-14 15:19:45 +01:00
home-manager/modules/programs/waybar.nix

327 lines
9.6 KiB
Nix
Raw Normal View History

2020-07-29 22:29:51 +02:00
{ config, lib, pkgs, ... }:
let
inherit (lib)
all filterAttrs hasAttr isStorePath literalExpression optionalAttrs types;
inherit (lib.options) mkEnableOption mkOption;
inherit (lib.modules) mkIf mkMerge;
2020-07-29 22:29:51 +02:00
cfg = config.programs.waybar;
jsonFormat = pkgs.formats.json { };
mkMargin = name:
mkOption {
type = types.nullOr types.int;
default = null;
example = 10;
description = "Margin value without unit.";
2020-07-29 22:29:51 +02:00
};
waybarBarConfig = with types;
2020-07-29 22:29:51 +02:00
submodule {
freeformType = jsonFormat.type;
2020-07-29 22:29:51 +02:00
options = {
layer = mkOption {
type = nullOr (enum [ "top" "bottom" ]);
default = null;
description = ''
Decide if the bar is displayed in front (`"top"`)
of the windows or behind (`"bottom"`).
2020-07-29 22:29:51 +02:00
'';
example = "top";
};
output = mkOption {
type = nullOr (either str (listOf str));
default = null;
example = literalExpression ''
2020-07-29 22:29:51 +02:00
[ "DP-1" "!DP-2" "!DP-3" ]
'';
description = ''
2020-07-29 22:29:51 +02:00
Specifies on which screen this bar will be displayed.
Exclamation mark(!) can be used to exclude specific output.
'';
};
position = mkOption {
type = nullOr (enum [ "top" "bottom" "left" "right" ]);
default = null;
example = "right";
description = "Bar position relative to the output.";
2020-07-29 22:29:51 +02:00
};
height = mkOption {
type = nullOr ints.unsigned;
default = null;
example = 5;
description =
2020-07-29 22:29:51 +02:00
"Height to be used by the bar if possible. Leave blank for a dynamic value.";
};
width = mkOption {
type = nullOr ints.unsigned;
default = null;
example = 5;
description =
2020-07-29 22:29:51 +02:00
"Width to be used by the bar if possible. Leave blank for a dynamic value.";
};
modules-left = mkOption {
type = nullOr (listOf str);
default = null;
description = "Modules that will be displayed on the left.";
example = literalExpression ''
2020-07-29 22:29:51 +02:00
[ "sway/workspaces" "sway/mode" "wlr/taskbar" ]
'';
};
modules-center = mkOption {
type = nullOr (listOf str);
default = null;
description = "Modules that will be displayed in the center.";
example = literalExpression ''
2020-07-29 22:29:51 +02:00
[ "sway/window" ]
'';
};
modules-right = mkOption {
type = nullOr (listOf str);
default = null;
description = "Modules that will be displayed on the right.";
example = literalExpression ''
2020-07-29 22:29:51 +02:00
[ "mpd" "custom/mymodule#with-css-id" "temperature" ]
'';
};
modules = mkOption {
type = jsonFormat.type;
visible = false;
default = null;
description = "Modules configuration.";
example = literalExpression ''
2020-07-29 22:29:51 +02:00
{
"sway/window" = {
max-length = 50;
};
"clock" = {
format-alt = "{:%a, %d. %b %H:%M}";
};
}
'';
};
margin = mkOption {
type = nullOr str;
default = null;
description = "Margins value using the CSS format without units.";
2020-07-29 22:29:51 +02:00
example = "20 5";
};
margin-left = mkMargin "left";
margin-right = mkMargin "right";
margin-bottom = mkMargin "bottom";
margin-top = mkMargin "top";
2020-07-29 22:29:51 +02:00
name = mkOption {
type = nullOr str;
default = null;
description =
2020-07-29 22:29:51 +02:00
"Optional name added as a CSS class, for styling multiple waybars.";
example = "waybar-1";
};
gtk-layer-shell = mkOption {
type = nullOr bool;
default = null;
example = false;
description =
2020-07-29 22:29:51 +02:00
"Option to disable the use of gtk-layer-shell for popups.";
};
};
};
in {
meta.maintainers = with lib.maintainers; [ berbiche ];
2020-07-29 22:29:51 +02:00
options.programs.waybar = with lib.types; {
enable = mkEnableOption "Waybar";
2020-07-29 22:29:51 +02:00
package = mkOption {
type = package;
default = pkgs.waybar;
defaultText = literalExpression "pkgs.waybar";
description = ''
Waybar package to use. Set to `null` to use the default package.
2020-07-29 22:29:51 +02:00
'';
};
settings = mkOption {
type = either (listOf waybarBarConfig) (attrsOf waybarBarConfig);
2020-07-29 22:29:51 +02:00
default = [ ];
description = ''
Configuration for Waybar, see <https://github.com/Alexays/Waybar/wiki/Configuration>
2020-07-29 22:29:51 +02:00
for supported values.
'';
example = literalExpression ''
{
mainBar = {
2020-07-29 22:29:51 +02:00
layer = "top";
position = "top";
height = 30;
output = [
"eDP-1"
"HDMI-A-1"
];
modules-left = [ "sway/workspaces" "sway/mode" "wlr/taskbar" ];
modules-center = [ "sway/window" "custom/hello-from-waybar" ];
modules-right = [ "mpd" "custom/mymodule#with-css-id" "temperature" ];
"sway/workspaces" = {
disable-scroll = true;
all-outputs = true;
};
"custom/hello-from-waybar" = {
format = "hello {}";
max-length = 40;
interval = "once";
exec = pkgs.writeShellScript "hello-from-waybar" '''
echo "from within waybar"
''';
2020-07-29 22:29:51 +02:00
};
};
}
2020-07-29 22:29:51 +02:00
'';
};
systemd.enable = mkEnableOption "Waybar systemd integration";
2020-07-29 22:29:51 +02:00
systemd.target = mkOption {
type = str;
default = "graphical-session.target";
example = "sway-session.target";
description = ''
The systemd target that will automatically start the Waybar service.
When setting this value to `"sway-session.target"`,
make sure to also enable {option}`wayland.windowManager.sway.systemd.enable`,
otherwise the service may never be started.
'';
};
2020-07-29 22:29:51 +02:00
style = mkOption {
type = nullOr (either path lines);
2020-07-29 22:29:51 +02:00
default = null;
description = ''
2020-07-29 22:29:51 +02:00
CSS style of the bar.
See <https://github.com/Alexays/Waybar/wiki/Configuration>
2020-07-29 22:29:51 +02:00
for the documentation.
If the value is set to a path literal, then the path will be used as the css file.
2020-07-29 22:29:51 +02:00
'';
example = ''
* {
border: none;
border-radius: 0;
font-family: Source Code Pro;
}
window#waybar {
background: #16191C;
color: #AAB2BF;
}
#workspaces button {
padding: 0 5px;
}
'';
};
};
config = let
# Removes nulls because Waybar ignores them.
# This is not recursive.
removeTopLevelNulls = filterAttrs (_: v: v != null);
# Makes the actual valid configuration Waybar accepts
# (strips our custom settings before converting to JSON)
makeConfiguration = configuration:
let
# The "modules" option is not valid in the JSON
# as its descendants have to live at the top-level
settingsWithoutModules = removeAttrs configuration [ "modules" ];
settingsModules =
optionalAttrs (configuration.modules != null) configuration.modules;
in removeTopLevelNulls (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 settings;
configSource = jsonFormat.generate "waybar-config.json" finalConfiguration;
2020-07-29 22:29:51 +02:00
in mkIf cfg.enable (mkMerge [
{
assertions = [
(lib.hm.assertions.assertPlatform "programs.waybar" pkgs
lib.platforms.linux)
({
assertion =
if lib.versionAtLeast config.home.stateVersion "22.05" then
all (x: !hasAttr "modules" x || x.modules == null) settings
else
true;
message = ''
The `programs.waybar.settings.[].modules` option has been removed.
It is now possible to declare modules in the configuration without nesting them under the `modules` option.
'';
})
];
home.packages = [ cfg.package ];
2020-07-29 22:29:51 +02:00
xdg.configFile."waybar/config" = mkIf (settings != [ ]) {
source = configSource;
onChange = ''
${pkgs.procps}/bin/pkill -u $USER -USR2 waybar || true
'';
};
xdg.configFile."waybar/style.css" = mkIf (cfg.style != null) {
source = if builtins.isPath cfg.style || isStorePath cfg.style then
cfg.style
else
pkgs.writeText "waybar/style.css" cfg.style;
onChange = ''
${pkgs.procps}/bin/pkill -u $USER -USR2 waybar || true
'';
};
}
2020-07-29 22:29:51 +02:00
(mkIf cfg.systemd.enable {
systemd.user.services.waybar = {
Unit = {
Description =
"Highly customizable Wayland bar for Sway and Wlroots based compositors.";
Documentation = "https://github.com/Alexays/Waybar/wiki";
PartOf = [ "graphical-session.target" ];
After = [ "graphical-session-pre.target" ];
2020-07-29 22:29:51 +02:00
};
Service = {
ExecStart = "${cfg.package}/bin/waybar";
ExecReload = "${pkgs.coreutils}/bin/kill -SIGUSR2 $MAINPID";
Restart = "on-failure";
KillMode = "mixed";
2020-07-29 22:29:51 +02:00
};
Install = { WantedBy = [ cfg.systemd.target ]; };
2020-07-29 22:29:51 +02:00
};
})
]);
}