1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-27 05:29:46 +01:00
home-manager/modules/services/polybar.nix

235 lines
7.1 KiB
Nix
Raw Normal View History

{ config, options, lib, pkgs, ... }:
2017-10-05 15:55:37 +02:00
with lib;
let
cfg = config.services.polybar;
opt = options.services.polybar;
2017-10-05 15:55:37 +02:00
2020-02-02 00:39:17 +01:00
eitherStrBoolIntList = with types;
either str (either bool (either int (listOf str)));
2018-12-05 11:54:52 +01:00
# Convert a key/val pair to the insane format that polybar uses.
# Each input key/val pair may return several output key/val pairs.
convertPolybarKeyVal = key: val:
# Convert { foo = [ "a" "b" ]; }
# to {
# foo-0 = "a";
# foo-1 = "b";
# }
if isList val then
concatLists (imap0 (i: convertPolybarKeyVal "${key}-${toString i}") val)
# Convert {
# foo.text = "a";
# foo.font = 1;
# } to {
# foo = "a";
# foo-font = 1;
# }
else if isAttrs val && !lib.isDerivation val then
concatLists (mapAttrsToList
(k: convertPolybarKeyVal (if k == "text" then key else "${key}-${k}"))
val)
# Base case
else
[ (nameValuePair key val) ];
convertPolybarSection = _: attrs:
listToAttrs (concatLists (mapAttrsToList convertPolybarKeyVal attrs));
# Converts an attrset to INI text, quoting values as expected by polybar.
# This does no more fancy conversion.
toPolybarIni = generators.toINI {
mkKeyValue = key: value:
2018-08-29 21:46:11 +02:00
let
quoted = v:
2020-02-02 00:39:17 +01:00
if hasPrefix " " v || hasSuffix " " v then ''"${v}"'' else v;
value' = if isBool value then
(if value then "true" else "false")
else if (isString value && key != "include-file") then
quoted value
else
toString value;
in "${key}=${value'}";
};
configFile = let
isDeclarativeConfig = cfg.settings != opt.settings.default || cfg.config
!= opt.config.default || cfg.extraConfig != opt.extraConfig.default;
in if isDeclarativeConfig then
pkgs.writeText "polybar.conf" ''
${toPolybarIni cfg.config}
${toPolybarIni (mapAttrs convertPolybarSection cfg.settings)}
${cfg.extraConfig}
''
else
null;
2017-10-05 15:55:37 +02:00
2020-02-02 00:39:17 +01:00
in {
2017-10-05 15:55:37 +02:00
options = {
services.polybar = {
enable = mkEnableOption "Polybar status bar";
2017-10-05 15:55:37 +02:00
package = mkOption {
type = types.package;
default = pkgs.polybar;
defaultText = literalExpression "pkgs.polybar";
description = "Polybar package to install.";
example = literalExpression ''
2017-10-05 15:55:37 +02:00
pkgs.polybar.override {
i3GapsSupport = true;
alsaSupport = true;
iwSupport = true;
githubSupport = true;
}
'';
};
config = mkOption {
2020-02-02 00:39:17 +01:00
type = types.coercedTo types.path
2017-10-05 15:55:37 +02:00
(p: { "section/base" = { include-file = "${p}"; }; })
2018-12-05 11:54:52 +01:00
(types.attrsOf (types.attrsOf eitherStrBoolIntList));
description = ''
2018-10-02 08:17:34 +02:00
Polybar configuration. Can be either path to a file, or set of attributes
2017-10-05 15:55:37 +02:00
that will be used to create the final configuration.
See also {option}`services.polybar.settings` for a more nix-friendly format.
2017-10-05 15:55:37 +02:00
'';
2020-02-02 00:39:17 +01:00
default = { };
example = literalExpression ''
2017-10-05 15:55:37 +02:00
{
"bar/top" = {
monitor = "\''${env:MONITOR:eDP1}";
width = "100%";
height = "3%";
radius = 0;
modules-center = "date";
};
"module/date" = {
type = "internal/date";
internal = 5;
date = "%d.%m.%y";
time = "%H:%M";
label = "%time% %date%";
};
}
'';
};
settings = mkOption {
type = with types;
let ty = oneOf [ bool int float str (listOf ty) (attrsOf ty) ];
in attrsOf (attrsOf ty // { description = "attribute sets"; });
description = ''
Polybar configuration. This takes a nix attrset and converts it to the
strange data format that polybar uses.
Each entry will be converted to a section in the output file.
Several things are treated specially: nested keys are converted
to dash-separated keys; the special `text` key is ignored as a nested key,
to allow mixing different levels of nesting; and lists are converted to
polybar's `foo-0, foo-1, ...` format.
For example:
```nix
"module/volume" = {
type = "internal/pulseaudio";
format.volume = "<ramp-volume> <label-volume>";
label.muted.text = "🔇";
label.muted.foreground = "#666";
ramp.volume = ["🔈" "🔉" "🔊"];
click.right = "pavucontrol &";
}
```
becomes:
```ini
[module/volume]
type=internal/pulseaudio
format-volume=<ramp-volume> <label-volume>
label-muted=🔇
label-muted-foreground=#666
ramp-volume-0=🔈
ramp-volume-1=🔉
ramp-volume-2=🔊
click-right=pavucontrol &
```
'';
default = { };
example = literalExpression ''
{
"module/volume" = {
type = "internal/pulseaudio";
format.volume = "<ramp-volume> <label-volume>";
label.muted.text = "🔇";
label.muted.foreground = "#666";
ramp.volume = ["🔈" "🔉" "🔊"];
click.right = "pavucontrol &";
};
}
'';
};
2017-10-05 15:55:37 +02:00
extraConfig = mkOption {
type = types.lines;
description = "Additional configuration to add.";
2017-10-05 15:55:37 +02:00
default = "";
example = ''
[module/date]
type = internal/date
interval = 5
date = "%d.%m.%y"
time = %H:%M
format-prefix-foreground = \''${colors.foreground-alt}
label = %time% %date%
'';
};
script = mkOption {
type = types.lines;
description = ''
2017-10-05 15:55:37 +02:00
This script will be used to start the polybars.
Set all necessary environment variables here and start all bars.
It can be assumed that {command}`polybar` executable is in the {env}`PATH`.
2017-10-05 15:55:37 +02:00
Note, this script must start all bars in the background and then terminate.
'';
example = "polybar bar &";
};
};
};
config = mkIf cfg.enable {
assertions = [
(lib.hm.assertions.assertPlatform "services.polybar" pkgs
lib.platforms.linux)
];
meta.maintainers = with maintainers; [ h7x4 ];
2017-10-05 15:55:37 +02:00
home.packages = [ cfg.package ];
xdg.configFile."polybar/config.ini" =
mkIf (configFile != null) { source = configFile; };
2017-10-05 15:55:37 +02:00
systemd.user.services.polybar = {
Unit = {
Description = "Polybar status bar";
PartOf = [ "tray.target" ];
X-Restart-Triggers = mkIf (configFile != null) "${configFile}";
2017-10-05 15:55:37 +02:00
};
Service = {
Type = "forking";
Environment = [ "PATH=${cfg.package}/bin:/run/wrappers/bin" ];
2019-03-21 00:39:36 +01:00
ExecStart =
2020-02-02 00:39:17 +01:00
let scriptPkg = pkgs.writeShellScriptBin "polybar-start" cfg.script;
in "${scriptPkg}/bin/polybar-start";
2019-09-03 23:51:23 +02:00
Restart = "on-failure";
2017-10-05 15:55:37 +02:00
};
Install = { WantedBy = [ "tray.target" ]; };
2017-10-05 15:55:37 +02:00
};
};
}