summaryrefslogtreecommitdiff
path: root/modules/rtc-wakeup.nix
diff options
context:
space:
mode:
authorKjetil Orbekk <kj@orbekk.com>2021-07-13 07:34:56 -0400
committerKjetil Orbekk <kj@orbekk.com>2021-07-13 07:34:56 -0400
commitfc71030a7d898df838183648b9a01444ec39dad8 (patch)
tree3cba7476738795e63629fd2e303fa9cc8dd776cd /modules/rtc-wakeup.nix
parent19553a4e96be8c32011c50f3126e0b4ee4fa72f2 (diff)
Enable rtc wakeup
Diffstat (limited to 'modules/rtc-wakeup.nix')
-rw-r--r--modules/rtc-wakeup.nix45
1 files changed, 45 insertions, 0 deletions
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";
+ };
+ };
+ };
+}