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 # home-managers waybar module performs additional checks that are overly strict
xdg.configFile."waybar/config".text = xdg.configFile."waybar/config".text =
let 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 in
lib.generators.toJSON { } { lib.generators.toJSON { } {
layer = "top"; layer = "top";
@ -332,21 +345,11 @@ lib.mkIf nixosConfig.sbruder.gui.enable {
spacing = 5; spacing = 5;
}; };
"custom/redshift" = { "custom/redshift" = {
interval = 10; exec = watchUserUnitState
exec = pkgs.writeShellScript "waybar-redshift-state" '' "redshift"
if systemctl --user show redshift | grep -q ActiveState=active; then { class = "active"; }
echo '{"tooltip": "Redshift active", "class": "active"}' { class = "inactive"; };
else on-click = toggleUserUnitState "redshift";
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
'';
return-type = "json"; return-type = "json";
format = ""; format = "";
tooltip = false; tooltip = false;