summaryrefslogtreecommitdiff
path: root/modules/rtc-wakeup.nix
blob: 28c64399ced01040a347d451f2347f72e754adc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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) -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";
      };
    };
  };
}