Daily Stoic Readings

A simple NixOS module that each day pops up today's entry in the book The Daily Stoic:

(Separate script, service, and timer files for non-NixOS folks.)

{ pkgs, ... }:
let

  book = pkgs.fetchurl {
    url =
      "https://archive.org/download/the-daily-stoic-366-meditations-on-wisdom-perseverance-and-the-art-of-living-pdfdrive.com/The-Daily-Stoic_-366-Meditations-on-Wisdom-Perseverance-and-the-Art-of-Living-PDFDrive.com-.pdf";
    hash = "sha256:0h6iacsg66fnvxwxwsk3mncf6g63miqw1jpvw77cn8f0vrg69z2w";
  };

  daily-stoic = pkgs.writeShellScript "daily-stoic" ''
    set -euo pipefail

    date=${pkgs.coreutils}/bin/date

    day=''${1:-$($date +%d)}
    month=''${2:-$($date +%m)}
    year=2004  # A leap year

    day_of_year=$($date -d "$year-$month-$day" +%j)
    day_of_year=''${day_of_year##0}
    day_of_year=''${day_of_year##0}

    quarter=$(( (month-1) / 4))

    page=$((13 + quarter + month + day_of_year))

    ${pkgs.evince}/bin/evince --page-index="$page" ${book}
  '';

in {
  systemd.user.services.daily-stoic = {
    description = "Daily Stoic reading";
    serviceConfig = {
      ExecStart = daily-stoic;
      TimeoutStartSec = 3600 * 23;
      Type = "oneshot";
    };
    wantedBy = [ "default.target" ];
    startAt = "8:00";
  };
}