diff --git a/modules/default.nix b/modules/default.nix index a9d3552f2..ab7b1065e 100644 --- a/modules/default.nix +++ b/modules/default.nix @@ -44,6 +44,7 @@ let ./services/keepassx.nix ./services/network-manager-applet.nix ./services/owncloud-client.nix + ./services/polybar.nix ./services/random-background.nix ./services/redshift.nix ./services/screen-locker.nix diff --git a/modules/misc/news.nix b/modules/misc/news.nix index 613cc595c..ebda61cf8 100644 --- a/modules/misc/news.nix +++ b/modules/misc/news.nix @@ -295,6 +295,13 @@ in A new module is available: 'xsession.windowManager.xmonad'. ''; } + + { + time = "2017-10-06T08:21:43+00:00"; + message = '' + A new service is available: 'services.polybar'. + ''; + } ]; }; } diff --git a/modules/services/polybar.nix b/modules/services/polybar.nix new file mode 100644 index 000000000..29f268ff2 --- /dev/null +++ b/modules/services/polybar.nix @@ -0,0 +1,139 @@ +{ config, lib, pkgs, ... }: + +with lib; +with import ../lib/dag.nix { inherit lib; }; + +let + + cfg = config.services.polybar; + + configFile = pkgs.writeText "polybar.conf" + (generators.toINI {} cfg.config + "\n" + cfg.extraConfig); + + script = '' + #!${pkgs.stdenv.shell} + + ${cfg.script} + ''; + +in + +{ + options = { + services.polybar = { + enable = mkEnableOption "Polybar status bar"; + + package = mkOption { + type = types.package; + default = pkgs.polybar; + defaultText = "pkgs.polybar"; + description = "Polybar package to install."; + example = literalExample '' + pkgs.polybar.override { + i3GapsSupport = true; + alsaSupport = true; + iwSupport = true; + githubSupport = true; + } + ''; + }; + + config = mkOption { + type = types.coercedTo + types.path + (p: { "section/base" = { include-file = "${p}"; }; }) + (types.attrsOf types.attrs); + description = '' + Polybar configuration. Can be either path to a file, or set of attibutes + that will be used to create the final configuration. + ''; + default = {}; + example = literalExample '' + { + "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%"; + }; + } + ''; + }; + + extraConfig = mkOption { + type = types.lines; + description = "Additional configuration to add."; + 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 = '' + This script will be used to start the polybars. + Set all necessary environment variables here and start all bars. + It can be assumed that polybar executable is in the PATH. + + Note, this script must start all bars in the background and then terminate. + ''; + example = "polybar bar &"; + }; + }; + }; + + config = mkIf cfg.enable { + home.packages = [ cfg.package ]; + home.file.".config/polybar/config".source = configFile; + + systemd.user.services.polybar = { + Unit = { + Description = "Polybar status bar"; + After = [ "graphical-session-pre.target" ]; + PartOf = [ "graphical-session.target" ]; + }; + + Service = { + Type = "forking"; + Environment = "PATH=${cfg.package}/bin"; + ExecStart = ''${pkgs.writeScriptBin "polybar-start" script}/bin/polybar-start''; + }; + + Install = { + WantedBy = [ "graphical-session.target" ]; + }; + }; + + home.activation.checkPolybar = dagEntryBefore [ "linkGeneration" ] '' + if ! cmp --quiet \ + "${configFile}" \ + "$HOME/.config/polybar/config"; then + polybarChanged=1 + fi + ''; + + home.activation.applyPolybar = dagEntryAfter [ "reloadSystemD" ] '' + if [[ -v polybarChanged && -v DISPLAY ]]; then + echo "Restarting polybar" + systemctl --user restart polybar.service + fi + ''; + }; + +}