fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , options , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . services . polybar ; 8opt = options . services . polybar ; 9 10eitherStrBoolIntList = with types ; 11either str ( either bool ( either int ( listOf str ))); 12 13# Convert a key/val pair to the insane format that polybar uses. 14# Each input key/val pair may return several output key/val pairs. 15convertPolybarKeyVal = key :val : 16# Convert { foo = [ "a" "b" ]; } 17# to { 18# foo-0 = "a"; 19# foo-1 = "b"; 20# } 21if isList val then 22concatLists ( imap0 ( i :convertPolybarKeyVal " ${ key } - ${ toString i } " ) val ) 23# Convert { 24# foo.text = "a"; 25# foo.font = 1; 26# } to { 27# foo = "a"; 28# foo-font = 1; 29# } 30else if isAttrs val && ! lib . isDerivation val then 31concatLists ( mapAttrsToList 32( k :convertPolybarKeyVal ( if k == "text" then key else " ${ key } - ${ k } " )) 33val ) 34# Base case 35else 36[ ( nameValuePair key val ) ]; 37 38convertPolybarSection = _ :attrs : 39listToAttrs ( concatLists ( mapAttrsToList convertPolybarKeyVal attrs )); 40 41# Converts an attrset to INI text, quoting values as expected by polybar. 42# This does no more fancy conversion. 43toPolybarIni = generators . toINI { 44mkKeyValue = key :value : 45let 46quoted = v : 47if hasPrefix " " v || hasSuffix " " v then ''" ${ v } "'' else v ; 48 49value' = if isBool value then 50( if value then "true" else "false" ) 51else if ( isString value && key != "include-file" ) then 52quoted value 53else 54toString value ; 55in " ${ key } = ${ value' } " ; 56}; 57 58configFile = let 59isDeclarativeConfig = cfg . settings != opt . settings . default || cfg . config 60!= opt . config . default || cfg . extraConfig != opt . extraConfig . default ; 61in if isDeclarativeConfig then 62pkgs . writeText "polybar.conf" '' 63${ toPolybarIni cfg . config } 64${ toPolybarIni ( mapAttrs convertPolybarSection cfg . settings )} 65${ cfg . extraConfig } 66'' 67else 68null ; 69 70in { 71options = { 72services . polybar = { 73enable = mkEnableOption "Polybar status bar" ; 74 75package = mkOption { 76type = types . package ; 77default = pkgs . polybar ; 78defaultText = literalExpression "pkgs.polybar" ; 79description = "Polybar package to install." ; 80example = literalExpression '' 81pkgs.polybar.override { 82i3GapsSupport = true; 83alsaSupport = true; 84iwSupport = true; 85githubSupport = true; 86} 87'' ; 88}; 89 90config = mkOption { 91type = types . coercedTo types . path 92( p :{ "section/base" = { include-file = " ${ p } " ; }; }) 93( types . attrsOf ( types . attrsOf eitherStrBoolIntList )); 94description = '' 95Polybar configuration. Can be either path to a file, or set of attributes 96that will be used to create the final configuration. 97See also {option}`services.polybar.settings` for a more nix-friendly format. 98'' ; 99default = { }; 100example = literalExpression '' 101{ 102"bar/top" = { 103monitor = "\''${env:MONITOR:eDP1}"; 104width = "100%"; 105height = "3%"; 106radius = 0; 107modules-center = "date"; 108}; 109 110"module/date" = { 111type = "internal/date"; 112internal = 5; 113date = "%d.%m.%y"; 114time = "%H:%M"; 115label = "%time% %date%"; 116}; 117} 118'' ; 119}; 120 121settings = mkOption { 122type = with types ; 123let ty = oneOf [ bool int float str ( listOf ty ) ( attrsOf ty ) ]; 124in attrsOf ( attrsOf ty // { description = "attribute sets" ; }); 125description = '' 126Polybar configuration. This takes a nix attrset and converts it to the 127strange data format that polybar uses. 128Each entry will be converted to a section in the output file. 129Several things are treated specially: nested keys are converted 130to dash-separated keys; the special `text` key is ignored as a nested key, 131to allow mixing different levels of nesting; and lists are converted to 132polybar's `foo-0, foo-1, ...` format. 133 134For example: 135```nix 136"module/volume" = { 137type = "internal/pulseaudio"; 138format.volume = "<ramp-volume> <label-volume>"; 139label.muted.text = "🔇"; 140label.muted.foreground = "#666"; 141ramp.volume = ["🔈" "🔉" "🔊"]; 142click.right = "pavucontrol &"; 143} 144``` 145becomes: 146```ini 147[module/volume] 148type=internal/pulseaudio 149format-volume=<ramp-volume> <label-volume> 150label-muted=🔇 151label-muted-foreground=#666 152ramp-volume-0=🔈 153ramp-volume-1=🔉 154ramp-volume-2=🔊 155click-right=pavucontrol & 156``` 157'' ; 158default = { }; 159example = literalExpression '' 160{ 161"module/volume" = { 162type = "internal/pulseaudio"; 163format.volume = "<ramp-volume> <label-volume>"; 164label.muted.text = "🔇"; 165label.muted.foreground = "#666"; 166ramp.volume = ["🔈" "🔉" "🔊"]; 167click.right = "pavucontrol &"; 168}; 169} 170'' ; 171}; 172 173extraConfig = mkOption { 174type = types . lines ; 175description = "Additional configuration to add." ; 176default = "" ; 177example = '' 178[module/date] 179type = internal/date 180interval = 5 181date = "%d.%m.%y" 182time = %H:%M 183format-prefix-foreground = \''${colors.foreground-alt} 184label = %time% %date% 185'' ; 186}; 187 188script = mkOption { 189type = types . lines ; 190description = '' 191This script will be used to start the polybars. 192Set all necessary environment variables here and start all bars. 193It can be assumed that {command}`polybar` executable is in the {env}`PATH`. 194 195Note, this script must start all bars in the background and then terminate. 196'' ; 197example = "polybar bar &" ; 198}; 199}; 200}; 201 202config = mkIf cfg . enable { 203assertions = [ 204( lib . hm . assertions . assertPlatform "services.polybar" pkgs 205lib . platforms . linux ) 206]; 207 208meta . maintainers = with maintainers ; [ h7x4 ]; 209 210home . packages = [ cfg . package ]; 211xdg . configFile . "polybar/config.ini" = 212mkIf ( configFile != null ) { source = configFile ; }; 213 214systemd . user . services . polybar = { 215Unit = { 216Description = "Polybar status bar" ; 217PartOf = [ "tray.target" ]; 218X-Restart-Triggers = mkIf ( configFile != null ) " ${ configFile } " ; 219}; 220 221Service = { 222Type = "forking" ; 223Environment = "PATH= ${ cfg . package } /bin:/run/wrappers/bin" ; 224ExecStart = 225let scriptPkg = pkgs . writeShellScriptBin "polybar-start" cfg . script ; 226in " ${ scriptPkg } /bin/polybar-start" ; 227Restart = "on-failure" ; 228}; 229 230Install = { WantedBy = [ "tray.target" ]; }; 231}; 232}; 233 234}