From 05d2529db700b27a8f2794d6615e4bb86e39f328 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Tue, 2 Mar 2021 12:34:11 +0100 Subject: [PATCH] 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. --- users/simon/modules/sway.nix | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/users/simon/modules/sway.nix b/users/simon/modules/sway.nix index 24ac1fc..f419ea4 100644 --- a/users/simon/modules/sway.nix +++ b/users/simon/modules/sway.nix @@ -293,6 +293,19 @@ lib.mkIf nixosConfig.sbruder.gui.enable { # home-manager’s 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;