From fc71030a7d898df838183648b9a01444ec39dad8 Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Tue, 13 Jul 2021 07:34:56 -0400 Subject: Enable rtc wakeup --- modules/rtc-wakeup.nix | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 modules/rtc-wakeup.nix (limited to 'modules/rtc-wakeup.nix') diff --git a/modules/rtc-wakeup.nix b/modules/rtc-wakeup.nix new file mode 100644 index 0000000..9477838 --- /dev/null +++ b/modules/rtc-wakeup.nix @@ -0,0 +1,45 @@ +{ 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) == "4" ]]; 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"; + }; + }; + }; +} -- cgit v1.2.3