{ config, lib, pkgs, ... }: let cfg = config.orbekk.rtc-wakeup; in { options = { orbekk.rtc-wakeup = { enable = lib.mkEnableOption "Enable automatic wakeup using rtc"; weekdayWakeupTime = lib.mkOption { type = lib.types.str; default = "05:55"; description = "Wakeup time on weekdays"; }; }; }; config = lib.mkIf cfg.enable { systemd.services.schedule-rtc-wakeup = { description = "Schedule next wakeup time"; script = '' if [[ ! $(cat /sys/class/rtc/rtc0/wakealarm) < $(date +%s) ]]; then echo "No need to reschedule. Current alarm:" cat /proc/driver/rtc fi if [[ $(date +%w) -ge "5" ]]; then WAKEUP_TIME='monday ${cfg.weekdayWakeupTime}' else WAKEUP_TIME='tomorrow ${cfg.weekdayWakeupTime}' fi date -d "$WAKEUP_TIME" +%s > /sys/class/rtc/rtc0/wakealarm echo "Scheduled wakeup time" cat /proc/driver/rtc ''; }; systemd.timers.schedule-rtc-wakeup = { wantedBy = [ "multi-user.target" ]; timerConfig = { Persistent = true; OnBootSec = "1m"; OnUnitActiveSec = "24h"; }; }; }; }