sway/waybar: Add abstraction for unit management

This also passes the unit state as a stream instead of requiring waybar
to poll for the state.
This commit is contained in:
Simon Bruder 2021-03-02 12:34:11 +01:00
parent 2bbe4e715b
commit 05d2529db7
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC

View file

@ -293,6 +293,19 @@ lib.mkIf nixosConfig.sbruder.gui.enable {
# home-managers waybar module performs additional checks that are overly strict
xdg.configFile."waybar/config".text =
let
watchUserUnitState = unit: started: stopped: pkgs.writeShellScript "watch-user-unit-${unit}-state" ''
${pkgs.systemd}/bin/journalctl --user -u ${unit} -t systemd -o cat -f \
| ${pkgs.gnugrep}/bin/grep --line-buffered -Eo '^(Started|Stopped)' \
| ${pkgs.jq}/bin/jq --unbuffered -Rc 'if . == "Started" then ${builtins.toJSON started} else ${builtins.toJSON stopped} end'
'';
toggleUserUnitState = unit: pkgs.writeShellScript "toggle-user-unit-${unit}-state" ''
if ${pkgs.systemd}/bin/systemctl --user show ${unit} | ${pkgs.gnugrep}/bin/grep -q ActiveState=active; then
${pkgs.systemd}/bin/systemctl --user stop ${unit}
else
${pkgs.systemd}/bin/systemctl --user start ${unit}
fi
'';
in
lib.generators.toJSON { } {
layer = "top";
@ -332,21 +345,11 @@ lib.mkIf nixosConfig.sbruder.gui.enable {
spacing = 5;
};
"custom/redshift" = {
interval = 10;
exec = pkgs.writeShellScript "waybar-redshift-state" ''
if systemctl --user show redshift | grep -q ActiveState=active; then
echo '{"tooltip": "Redshift active", "class": "active"}'
else
echo '{"tooltip": "Redshift inactive", "class": "inactive"}'
fi
'';
on-click = pkgs.writeShellScript "waybar-redshift-toggle" ''
if systemctl --user show redshift | grep -q ActiveState=active; then
systemctl --user stop redshift
else
systemctl --user start redshift
fi
'';
exec = watchUserUnitState
"redshift"
{ class = "active"; }
{ class = "inactive"; };
on-click = toggleUserUnitState "redshift";
return-type = "json";
format = "";
tooltip = false;