swaync: factor out a "service-dispatcher"

This commit is contained in:
Colin 2024-03-24 09:21:07 +00:00
parent 86400f45d6
commit 774066e53c
3 changed files with 82 additions and 82 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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