sync-ical2org.nix (1695B)
1 home: 2 { config, lib, pkgs, ... }: 3 let calendars = (import ../private.nix).calendars; 4 calendarArgs = with pkgs.lib; 5 let xs = mapAttrsToList (n: v: "'" + n + "=" + v.category + "=" + v.link + "'") calendars; 6 in concatStringsSep " " xs; 7 in { 8 systemd.services.sync-ical2org = { 9 description = "Sync gcal calendar to calendar.org"; 10 serviceConfig = { 11 Type = "oneshot"; 12 ExecStart = let script = pkgs.writeScript "ical2org-auto" '' 13 #!${pkgs.python35}/bin/python3 14 import os 15 import sys 16 from urllib.request import urlopen 17 import subprocess 18 caldir = "${home}/var/ical2org" 19 os.makedirs(caldir, exist_ok=True) 20 cat = lambda n: b"#+CATEGORY: " + bytes(n, "utf-8") 21 for arg in sys.argv[1:]: 22 [name, category, link] = arg.split("=") 23 ical = urlopen(link).read() 24 fname = os.path.join(caldir, name + ".org") 25 org = open(fname, "wb") 26 icalfd = open(os.path.join(caldir, name + ".ical"), "wb") 27 icalfd.write(ical) 28 icalfd.close() 29 # just download for now 30 #proc = subprocess.Popen("${pkgs.ical2org}/bin/ical2org", 31 # close_fds=True, 32 # stdin=subprocess.PIPE, 33 # stdout=subprocess.PIPE) 34 #out, err = proc.communicate(ical) 35 #org.write(out.replace(cat("google"), cat(category))) 36 #org.close() 37 ''; in "${script} ${calendarArgs}"; 38 39 }; 40 preStart = '' 41 export SSL_CERT_FILE="${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt" 42 ''; 43 restartIfChanged = false; 44 startAt = "*:0/10"; 45 }; 46 } 47