From 749516aea61939bd22a69e9e3ebf9da2439158b7 Mon Sep 17 00:00:00 2001 From: Anish Lakhwara Date: Sun, 18 Dec 2022 00:49:01 +1000 Subject: [PATCH] add calendar alarms --- hosts/profiles/desktop/cal-alarms.nix | 65 +++++++++++++++++++++++++++ hosts/profiles/desktop/default.nix | 5 ++- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 hosts/profiles/desktop/cal-alarms.nix diff --git a/hosts/profiles/desktop/cal-alarms.nix b/hosts/profiles/desktop/cal-alarms.nix new file mode 100644 index 00000000..a8bc63ce --- /dev/null +++ b/hosts/profiles/desktop/cal-alarms.nix @@ -0,0 +1,65 @@ +{ pkgs, ... }: + +{ + systemd.services.cal-alarms = { + serviceConfig.Type = "simple"; + path = [ + pkgs.libnotify + pkgs.dunstify + pkgs.khal + ]; + startAt = "*08:00:00"; + script = '' + #!/usr/bin/env bash + + cache_file="${XDG_CACHE_HOME:-$HOME/.cache}/khal-events" + # events starting in the next TIMESPAN will trigger an alarm + timespan='60min' + + # this is needed for cronjobs and Xorg + export DISPLAY=":0" + snooze_string='💤 Snooze all.' + dismiss_string='✅ Dismiss all.' + # only show events with ⏰ symbol (=has alarm set) + # remove arrows for all-day events to only have one notification + # remove ⏰ symbol because it is obvious + events="$(khal list --format \ + '{cancelled}{start-style}{to-style}{end-style} {title}{description-separator}{description}{location}{repeat-symbol}{alarm-symbol}' \ + now "${timespan}" \ + | sed -ne '/⏰/ p' \ + | sed -e 's/⏰//g' -e 's/\(↦\|↔\|⇥\)[^[:blank:]]*/all-day/g' \ + | column -tl 3)" + + # get events that are new compared to cache file + new_events="$(diff -NwU 0 "${cache_file}" - << EOF | sed -ne '/^\+[^+]/ p' \ + | sed -ne 's/^\+\([[:print:]].*\)$/\1/p' + ${events} + EOF + )" + + [ -z "${new_events}" ] && exit 0 + + action="$(dunstify --action="default,Dismiss" --action="snooze,Snooze" "${new_events}")" + + case "${action}" in + "snooze") + ;; + "default") + printf '%s' "${events}" > "${cache_file}" + ;; + *) + printf '%s' "${events}" | grep -Fve "${input}" > "${cache_file}" + ;; + esac + ''; + serviceConfig = { + User = "anish"; + Environment = "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus"; + }; + }; + systemd.timers.cal-alarms = { + wantedBy = [ "timers.target" ]; + partOf = [ "cal-alarms.service" ]; + timerConfig.OnCalendar = [ "*:0/10" ]; + }; +} diff --git a/hosts/profiles/desktop/default.nix b/hosts/profiles/desktop/default.nix index 6dca6dd1..d186e4bd 100644 --- a/hosts/profiles/desktop/default.nix +++ b/hosts/profiles/desktop/default.nix @@ -16,7 +16,10 @@ let in { # TODO modularize - imports = [ ./battery-low-timer.nix ]; + imports = [ + ./battery-low-timer.nix + ./cal-alarms.nix + ]; # Secrets used by home-manager modules age.secrets.fastmail.file = "${self}/secrets/fastmail.age";