From 774066e53c85950a1453d6b5975c6eb5bdef00c1 Mon Sep 17 00:00:00 2001 From: Colin Date: Sun, 24 Mar 2024 09:21:07 +0000 Subject: [PATCH] swaync: factor out a "service-dispatcher" --- .../swaynotificationcenter/buttons.nix | 94 +++---------------- .../swaynotificationcenter/default.nix | 17 +++- .../swaync-service-dispatcher | 53 +++++++++++ 3 files changed, 82 insertions(+), 82 deletions(-) create mode 100755 hosts/common/programs/swaynotificationcenter/swaync-service-dispatcher diff --git a/hosts/common/programs/swaynotificationcenter/buttons.nix b/hosts/common/programs/swaynotificationcenter/buttons.nix index 68d207d1..d27a22b9 100644 --- a/hosts/common/programs/swaynotificationcenter/buttons.nix +++ b/hosts/common/programs/swaynotificationcenter/buttons.nix @@ -1,91 +1,23 @@ { pkgs }: let - # TODO: move ALL of the scripts into just one helper. - systemctl-toggle' = pkgs.writeShellApplication { - name = "systemctl-toggle"; - runtimeInputs = [ - pkgs.systemd - ]; - text = '' - if systemctl is-active "$@"; then - systemctl stop "$@" - else - systemctl start "$@" - fi - ''; - }; - systemctl-toggle = "${systemctl-toggle'}/bin/systemctl-toggle"; - - print-systemd-active' = pkgs.writeShellApplication { - name = "print-systemd-active"; - runtimeInputs = [ - pkgs.systemd - ]; - text = '' - if systemctl is-active "$@"; then - echo true - else - echo false - fi - ''; - }; - print-systemd-active = "${print-systemd-active'}/bin/print-systemd-active"; - - s6-toggle' = pkgs.writeShellApplication { - name = "s6-toggle"; - runtimeInputs = [ - pkgs.s6 - pkgs.s6-rc - ]; - text = '' - service="$1" - if [ "$(s6-svstat -o wantedup "$XDG_RUNTIME_DIR/s6/live/servicedirs/$service")" = "true" ]; then - s6-rc stop "$service" - else - s6-rc start "$service" - fi - ''; - }; - s6-toggle = "${s6-toggle'}/bin/s6-toggle"; - - print-s6-active' = pkgs.writeShellApplication { - name = "print-s6-active"; - runtimeInputs = [ - pkgs.s6 - ]; - text = '' - # s6-svstat's native output is "true"/"false" as expected by swaync - s6-svstat -o wantedup "$XDG_RUNTIME_DIR/s6/live/servicedirs/$1" - ''; - }; - print-s6-active = "${print-s6-active'}/bin/print-s6-active"; - - systemServiceButton = name: label: { - # XXX: this is surely broken, due to sandboxing/perms + serviceButton = svcType: name: label: { inherit label; type = "toggle"; - command = "/run/wrappers/bin/sudo ${systemctl-toggle} ${name}"; - update-command = "${print-systemd-active} ${name}.service"; - active = true; - }; - userServiceButton = name: label: { - inherit label; - type = "toggle"; - command = "${s6-toggle} ${name}"; - update-command = "${print-s6-active} ${name}"; + command = "swaync-service-dispatcher toggle ${svcType} ${name}"; + update-command = "swaync-service-dispatcher print ${svcType} ${name}"; active = true; }; in { - gps = systemServiceButton "eg25-control-gps" ""; # GPS services; other icons: gps, ⌖, 🛰, 🌎,  - cell-modem = systemServiceButton "eg25-control-powered" "󰺐"; # icons: 5g, 📡, 📱, ᯤ, ⚡, , 🌐, 📶, 🗼, 󰀂, , 󰺐, 󰩯 - vpn = systemServiceButton "wg-quick-vpn-servo" "vpn::hn"; + gps = serviceButton "systemd" "eg25-control-gps" ""; # GPS services; other icons: gps, ⌖, 🛰, 🌎,  + cell-modem = serviceButton "systemd" "eg25-control-powered" "󰺐"; # icons: 5g, 📡, 📱, ᯤ, ⚡, , 🌐, 📶, 🗼, 󰀂, , 󰺐, 󰩯 + vpn = serviceButton "systemd" "wg-quick-vpn-servo" "vpn::hn"; - gnome-calls = userServiceButton "gnome-calls" "SIP"; - geary = userServiceButton "geary" "󰇮"; # email (Geary); other icons: ✉, [E], 📧, 󰇮 - abaddon = userServiceButton "abaddon" "󰊴"; # Discord chat client; icons: 󰊴, 🎮 - dissent = userServiceButton "dissent" "󰊴"; # Discord chat client; icons: 󰊴, 🎮 - signal-desktop = userServiceButton "signal-desktop" "💬"; # Signal messenger; other icons: 󰍦 - dino = userServiceButton "dino" "XMPP"; # XMPP calls (jingle) - fractal = userServiceButton "fractal" "[m]"; # Matrix messages + gnome-calls = serviceButton "s6" "gnome-calls" "SIP"; + geary = serviceButton "s6" "geary" "󰇮"; # email (Geary); other icons: ✉, [E], 📧, 󰇮 + abaddon = serviceButton "s6" "abaddon" "󰊴"; # Discord chat client; icons: 󰊴, 🎮 + dissent = serviceButton "s6" "dissent" "󰊴"; # Discord chat client; icons: 󰊴, 🎮 + signal-desktop = serviceButton "s6" "signal-desktop" "💬"; # Signal messenger; other icons: 󰍦 + dino = serviceButton "s6" "dino" "XMPP"; # XMPP calls (jingle) + fractal = serviceButton "s6" "fractal" "[m]"; # Matrix messages } diff --git a/hosts/common/programs/swaynotificationcenter/default.nix b/hosts/common/programs/swaynotificationcenter/default.nix index 41a715cc..699f6172 100644 --- a/hosts/common/programs/swaynotificationcenter/default.nix +++ b/hosts/common/programs/swaynotificationcenter/default.nix @@ -21,6 +21,18 @@ let scripts = import ./scripts.nix { inherit pkgs; }; in { + sane.programs.swaync-service-dispatcher = { + packageUnwrapped = pkgs.static-nix-shell.mkBash { + pname = "swaync-service-dispatcher"; + srcRoot = ./.; + pkgs = [ + "s6" + "s6-rc" + "systemd" + ]; + }; + }; + sane.programs.swaynotificationcenter = { configOption = with lib; mkOption { type = types.submodule { @@ -77,7 +89,10 @@ in # the glib code which consumes this is `g_notification_backend_new_default`, calling into `_g_io_module_get_default_type`. env.GNOTIFICATION_BACKEND = "freedesktop"; - suggestedPrograms = [ "feedbackd" ]; + suggestedPrograms = [ + "feedbackd" + "swaync-service-dispatcher" #< used when toggling buttons + ]; fs.".config/swaync/style.css".symlink.target = ./style.css; fs.".config/swaync/config.json".symlink.text = builtins.toJSON { diff --git a/hosts/common/programs/swaynotificationcenter/swaync-service-dispatcher b/hosts/common/programs/swaynotificationcenter/swaync-service-dispatcher new file mode 100755 index 00000000..fb8a00b4 --- /dev/null +++ b/hosts/common/programs/swaynotificationcenter/swaync-service-dispatcher @@ -0,0 +1,53 @@ +#!/usr/bin/env nix-shell +#!nix-shell -i bash -p s6 -p s6-rc -p systemd + +action="$1" +type="$2" +service="$3" + +checkActive() { + case "$type" in + systemd) + systemctl is-active "$service.service" > /dev/null && echo true || echo false + ;; + s6) + s6-svstat -o wantedup "$XDG_RUNTIME_DIR/s6/live/servicedirs/$service" + ;; + esac +} +startService() { + case "$type" in + systemd) + /run/wrappers/bin/sudo systemctl start "$service" + ;; + s6) + s6-rc start "$service" + ;; + esac +} +stopService() { + case "$type" in + systemd) + /run/wrappers/bin/sudo systemctl stop "$service" + ;; + s6) + s6-rc stop "$service" + ;; + esac +} + +case "$action" in + print) + checkActive + ;; + toggle) + case "$(checkActive)" in + false) + stopService + ;; + true) + startService + ;; + esac + ;; +esac