51 lines
1.5 KiB
Nix
51 lines
1.5 KiB
Nix
{
|
|
pkgs,
|
|
lib,
|
|
}:
|
|
let
|
|
inherit (pkgs) writeShellScript;
|
|
inherit (lib) replaceString optionalString;
|
|
inherit (builtins) toJSON;
|
|
in
|
|
{
|
|
mkToggleScript =
|
|
{
|
|
service,
|
|
start,
|
|
stop,
|
|
icon ? "",
|
|
notify-icon ? "",
|
|
extra ? { },
|
|
}:
|
|
let
|
|
extraJson = replaceString "\"" "\\\"" (toJSON extra);
|
|
in
|
|
writeShellScript "${service}-toggle.sh" ''
|
|
SERVICE_NAME=${service}
|
|
EXTRA_JSON="${extraJson}"
|
|
|
|
case $1 in toggle)
|
|
if systemctl --user is-active --quiet "$SERVICE_NAME"; then
|
|
systemctl --user stop "$SERVICE_NAME"
|
|
notify-send ${
|
|
optionalString (notify-icon != "") "-i ${notify-icon}"
|
|
} "${icon} ''\${SERVICE_NAME^}" "stopping"
|
|
else
|
|
systemctl --user start "$SERVICE_NAME"
|
|
notify-send ${
|
|
optionalString (notify-icon != "") "-i ${notify-icon}"
|
|
}"${icon} ''\${SERVICE_NAME^}" "starting"
|
|
fi
|
|
esac
|
|
|
|
if systemctl --user is-active --quiet "$SERVICE_NAME"; then
|
|
json1=$(jq -nc --arg text "${icon} ''\${SERVICE_NAME^} starting" --arg class "${start}" \
|
|
'{text: $text, tooltip: $text, class: $class, alt: $class}')
|
|
else
|
|
json1=$(jq -nc --arg text "${icon} ''\${SERVICE_NAME^} stopped" --arg class "${stop}" \
|
|
'{text: $text, tooltip: $text, class: $class, alt: $class}')
|
|
fi
|
|
|
|
jq -nc --argjson a "$json1" --argjson b "$EXTRA_JSON" '$a + $b'
|
|
'';
|
|
}
|