fork/home-manager
git clone https://git.julia.blue/fork/home-manager
8aac47a1
master
1{ config , lib , pkgs , ...} : 2 3let 4inherit ( lib ) 5all filterAttrs hasAttr isStorePath literalExpression optionalAttrs types ; 6inherit ( lib . options ) mkEnableOption mkOption ; 7inherit ( lib . modules ) mkIf mkMerge ; 8 9cfg = config . programs . waybar ; 10 11jsonFormat = pkgs . formats . json { }; 12 13mkMargin = name : 14mkOption { 15type = types . nullOr types . int ; 16default = null ; 17example = 10 ; 18description = "Margin value without unit." ; 19}; 20 21waybarBarConfig = with types ; 22submodule { 23freeformType = jsonFormat . type ; 24 25options = { 26layer = mkOption { 27type = nullOr ( enum [ "top" "bottom" ]); 28default = null ; 29description = '' 30Decide if the bar is displayed in front (`"top"`) 31of the windows or behind (`"bottom"`). 32'' ; 33example = "top" ; 34}; 35 36output = mkOption { 37type = nullOr ( either str ( listOf str )); 38default = null ; 39example = literalExpression '' 40[ "DP-1" "!DP-2" "!DP-3" ] 41'' ; 42description = '' 43Specifies on which screen this bar will be displayed. 44Exclamation mark(!) can be used to exclude specific output. 45'' ; 46}; 47 48position = mkOption { 49type = nullOr ( enum [ "top" "bottom" "left" "right" ]); 50default = null ; 51example = "right" ; 52description = "Bar position relative to the output." ; 53}; 54 55height = mkOption { 56type = nullOr ints . unsigned ; 57default = null ; 58example = 5 ; 59description = 60"Height to be used by the bar if possible. Leave blank for a dynamic value." ; 61}; 62 63width = mkOption { 64type = nullOr ints . unsigned ; 65default = null ; 66example = 5 ; 67description = 68"Width to be used by the bar if possible. Leave blank for a dynamic value." ; 69}; 70 71modules-left = mkOption { 72type = listOf str ; 73default = [ ]; 74description = "Modules that will be displayed on the left." ; 75example = literalExpression '' 76[ "sway/workspaces" "sway/mode" "wlr/taskbar" ] 77'' ; 78}; 79 80modules-center = mkOption { 81type = listOf str ; 82default = [ ]; 83description = "Modules that will be displayed in the center." ; 84example = literalExpression '' 85[ "sway/window" ] 86'' ; 87}; 88 89modules-right = mkOption { 90type = listOf str ; 91default = [ ]; 92description = "Modules that will be displayed on the right." ; 93example = literalExpression '' 94[ "mpd" "custom/mymodule#with-css-id" "temperature" ] 95'' ; 96}; 97 98modules = mkOption { 99type = jsonFormat . type ; 100visible = false ; 101default = null ; 102description = "Modules configuration." ; 103example = literalExpression '' 104{ 105"sway/window" = { 106max-length = 50; 107}; 108"clock" = { 109format-alt = "{:%a, %d. %b %H:%M}"; 110}; 111} 112'' ; 113}; 114 115margin = mkOption { 116type = nullOr str ; 117default = null ; 118description = "Margins value using the CSS format without units." ; 119example = "20 5" ; 120}; 121 122margin-left = mkMargin "left" ; 123margin-right = mkMargin "right" ; 124margin-bottom = mkMargin "bottom" ; 125margin-top = mkMargin "top" ; 126 127name = mkOption { 128type = nullOr str ; 129default = null ; 130description = 131"Optional name added as a CSS class, for styling multiple waybars." ; 132example = "waybar-1" ; 133}; 134 135gtk-layer-shell = mkOption { 136type = nullOr bool ; 137default = null ; 138example = false ; 139description = 140"Option to disable the use of gtk-layer-shell for popups." ; 141}; 142}; 143}; 144in { 145meta . maintainers = with lib . maintainers ; [ berbiche ]; 146 147options . programs . waybar = with lib . types ; { 148enable = mkEnableOption "Waybar" ; 149 150package = mkOption { 151type = package ; 152default = pkgs . waybar ; 153defaultText = literalExpression "pkgs.waybar" ; 154description = '' 155Waybar package to use. Set to `null` to use the default package. 156'' ; 157}; 158 159settings = mkOption { 160type = either ( listOf waybarBarConfig ) ( attrsOf waybarBarConfig ); 161default = [ ]; 162description = '' 163Configuration for Waybar, see <https://github.com/Alexays/Waybar/wiki/Configuration> 164for supported values. 165'' ; 166example = literalExpression '' 167{ 168mainBar = { 169layer = "top"; 170position = "top"; 171height = 30; 172output = [ 173"eDP-1" 174"HDMI-A-1" 175]; 176modules-left = [ "sway/workspaces" "sway/mode" "wlr/taskbar" ]; 177modules-center = [ "sway/window" "custom/hello-from-waybar" ]; 178modules-right = [ "mpd" "custom/mymodule#with-css-id" "temperature" ]; 179 180"sway/workspaces" = { 181disable-scroll = true; 182all-outputs = true; 183}; 184"custom/hello-from-waybar" = { 185format = "hello {}"; 186max-length = 40; 187interval = "once"; 188exec = pkgs.writeShellScript "hello-from-waybar" ''' 189echo "from within waybar" 190'''; 191}; 192}; 193} 194'' ; 195}; 196 197systemd . enable = mkEnableOption "Waybar systemd integration" ; 198 199systemd . target = mkOption { 200type = str ; 201default = "graphical-session.target" ; 202example = "sway-session.target" ; 203description = '' 204The systemd target that will automatically start the Waybar service. 205 206When setting this value to `"sway-session.target"`, 207make sure to also enable {option}`wayland.windowManager.sway.systemd.enable`, 208otherwise the service may never be started. 209'' ; 210}; 211 212style = mkOption { 213type = nullOr ( either path lines ); 214default = null ; 215description = '' 216CSS style of the bar. 217 218See <https://github.com/Alexays/Waybar/wiki/Configuration> 219for the documentation. 220 221If the value is set to a path literal, then the path will be used as the css file. 222'' ; 223example = '' 224* { 225border: none; 226border-radius: 0; 227font-family: Source Code Pro; 228} 229window#waybar { 230background: #16191C; 231color: #AAB2BF; 232} 233#workspaces button { 234padding: 0 5px; 235} 236'' ; 237}; 238}; 239 240config = let 241# Removes nulls because Waybar ignores them. 242# This is not recursive. 243removeTopLevelNulls = filterAttrs ( _ :v :v != null ); 244 245# Makes the actual valid configuration Waybar accepts 246# (strips our custom settings before converting to JSON) 247makeConfiguration = configuration : 248let 249# The "modules" option is not valid in the JSON 250# as its descendants have to live at the top-level 251settingsWithoutModules = removeAttrs configuration [ "modules" ]; 252settingsModules = 253optionalAttrs ( configuration . modules != null ) configuration . modules ; 254in removeTopLevelNulls ( settingsWithoutModules // settingsModules ); 255 256# Allow using attrs for settings instead of a list in order to more easily override 257settings = if builtins . isAttrs cfg . settings then 258lib . attrValues cfg . settings 259else 260cfg . settings ; 261 262# The clean list of configurations 263finalConfiguration = map makeConfiguration settings ; 264 265configSource = jsonFormat . generate "waybar-config.json" finalConfiguration ; 266 267in mkIf cfg . enable ( mkMerge [ 268{ 269assertions = [ 270( lib . hm . assertions . assertPlatform "programs.waybar" pkgs 271lib . platforms . linux ) 272({ 273assertion = 274if lib . versionAtLeast config . home . stateVersion "22.05" then 275all ( x :! hasAttr "modules" x || x . modules == null ) settings 276else 277true ; 278message = '' 279The `programs.waybar.settings.[].modules` option has been removed. 280It is now possible to declare modules in the configuration without nesting them under the `modules` option. 281'' ; 282}) 283]; 284 285home . packages = [ cfg . package ]; 286 287xdg . configFile . "waybar/config" = mkIf ( settings != [ ]) { 288source = configSource ; 289onChange = '' 290${ pkgs . procps } /bin/pkill -u $USER -USR2 waybar || true 291'' ; 292}; 293 294xdg . configFile . "waybar/style.css" = mkIf ( cfg . style != null ) { 295source = if builtins . isPath cfg . style || isStorePath cfg . style then 296cfg . style 297else 298pkgs . writeText "waybar/style.css" cfg . style ; 299onChange = '' 300${ pkgs . procps } /bin/pkill -u $USER -USR2 waybar || true 301'' ; 302}; 303} 304 305( mkIf cfg . systemd . enable { 306systemd . user . services . waybar = { 307Unit = { 308Description = 309"Highly customizable Wayland bar for Sway and Wlroots based compositors." ; 310Documentation = "https://github.com/Alexays/Waybar/wiki" ; 311PartOf = [ "graphical-session.target" ]; 312After = [ "graphical-session-pre.target" ]; 313}; 314 315Service = { 316ExecStart = " ${ cfg . package } /bin/waybar" ; 317ExecReload = " ${ pkgs . coreutils } /bin/kill -SIGUSR2 $MAINPID" ; 318Restart = "on-failure" ; 319KillMode = "mixed" ; 320}; 321 322Install = { WantedBy = [ cfg . systemd . target ]; }; 323}; 324}) 325]); 326}