From 54ae461302f1d397f5d395f0ac1a237261ee2e4f Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Sat, 11 Mar 2023 19:23:03 -0800 Subject: home/swaybar: add an indicator for systemd unit Reports the number of systemd units (user and systems) that are in failed state. --- home/wm/waybar/default.nix | 19 ++++++++++++++++--- home/wm/waybar/waybar-systemd.sh | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100755 home/wm/waybar/waybar-systemd.sh (limited to 'home') diff --git a/home/wm/waybar/default.nix b/home/wm/waybar/default.nix index e712585..717d4c0 100644 --- a/home/wm/waybar/default.nix +++ b/home/wm/waybar/default.nix @@ -1,5 +1,12 @@ -{ config, lib, ... }: -let isEnabled = config.my.home.wm.windowManager == "sway"; +{ config, lib, pkgs, ... }: +let + isEnabled = config.my.home.wm.windowManager == "sway"; + waybarSystemd = + pkgs.runCommandLocal "waybar-systemd" + { nativeBuildInputs = [ pkgs.makeWrapper ]; } + '' + makeWrapper ${./waybar-systemd.sh} $out/bin/waybar-systemd + ''; in { config = lib.mkIf isEnabled { @@ -9,13 +16,14 @@ in settings = [{ layer = "bottom"; + height = 25; position = "top"; margin-top = 0; margin-left = 0; margin-right = 0; margin-bottom = 0; modules-left = [ "sway/workspaces" "sway/mode" ]; - modules-right = [ "pulseaudio" "network" "battery" "clock" "tray" ]; + modules-right = [ "custom/systemd" "pulseaudio" "network" "battery" "clock" "tray" ]; "sway/workspaces" = { format = "{name}"; }; "sway/mode" = { format = "{}"; }; tray = { spacing = 10; }; @@ -29,6 +37,11 @@ in tooltip = true; tooltip-format = "{timeTo} ({capacity}%)"; }; + "custom/systemd" = { + exec = "${waybarSystemd}/bin/waybar-systemd"; + return-type = "json"; + interval = 10; + }; pulseaudio = { format = "vol:{volume}%"; format-bluetooth = "bt:{volume}%"; diff --git a/home/wm/waybar/waybar-systemd.sh b/home/wm/waybar/waybar-systemd.sh new file mode 100755 index 0000000..bf4e8b8 --- /dev/null +++ b/home/wm/waybar/waybar-systemd.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +failed_user="$(systemctl --plain --no-legend --user list-units --state=failed | awk '{ print $1 }')" +failed_system="$(systemctl --plain --no-legend list-units --state=failed | awk '{ print $1 }')" + +failed_systemd_count="$(echo -n "$failed_system" | grep -c '^')" +failed_user_count="$(echo -n "$failed_user" | grep -c '^')" + +text=$(( failed_systemd_count + failed_user_count )) + +if [ "$text" -eq 0 ]; then + printf '{"text": ""}\n' +else + tooltip="" + + [ -n "$failed_system" ] && tooltip="Failed system services:\n\n${failed_system}\n\n${tooltip}" + [ -n "$failed_user" ] && tooltip="Failed user services:\n\n${failed_user}\n\n${tooltip}" + + tooltip="$(printf "%s" "$tooltip" | perl -pe 's/\n/\\n/g' | perl -pe 's/(?:\\n)+$//')" + + printf '{"text": "%s", "tooltip": "%s" }\n' "$text" "$tooltip" +fi -- cgit 1.4.1