tyx's corner


Adding CalDAV calendars to org-agenda

Since I’m trying to use org-agenda more, I spent a bit of time setting it up to show my CalDAV calendars (usually work meetings) using vdirsyncer, khal, khalel and nix/home-manager. I didn’t find any HOWTO on the web, so I had to read the documentation of the tools (yes, I know, the horror :D) to set it up, hence this write-up.

Let’s start with home-manager to install and configure vdirsyncer (which sync CalDAV calendars locally) and khal (which is an interface for the local calendars). The home-manager community provides a nice way to configure calendars: accounts.calendar.

accounts.calendar = {
  basePath = ".calendars";
  accounts = {
    sula_personal = {
      name = "personal";

      khal = {
        enable = true;
        readOnly = false;
        type = "calendar";
      };

      vdirsyncer = {
        enable = true;
        auth = "basic";
        conflictResolution = "remote wins";
        timeRange = {
          end = "datetime.now() + timedelta(days=365)";
          start = "datetime.now() - timedelta(days=365)";
        };
      };

      primary = true;

      remote = {
        url = "https://nextcloud.fqdn/remote.php/dav/calendars/tyx/personal";
        passwordCommand = [ "gopass" "-o" "path/to/password" ];
        type = "caldav";
        userName = "tyx";
      };
    };
  };
};

Let’s not forget to enable those two programs, and to configure khal for khalel usage:

programs.vdirsyncer.enable = true;
programs.khal = {
  enable = true;
  locale = {
    timeformat = "%H:%M";
    dateformat = "%Y-%m-%d";
    longdateformat = "%Y-%m-%d %a";
    datetimeformat = "%Y-%m-%d %H:%M";
    longdatetimeformat = "%Y-%m-%d %H:%M";
  };
};

Now, onto the khalel configuration:

(use-package khalel
  :after org
  :config
  (khalel-add-capture-template)
  (setq khalel-capture-key "e")
  (setq khalel-import-org-file (concat org-directory "/" "calendar.org"))
  (setq khalel-import-org-file-confirm-overwrite nil)
  (setq khalel-import-start-date "-30d")
  (setq khalel-import-end-date "+30d"))

And adding the newly created calendar.org to the agenda:

(setq org-agenda-files
         (list
          "~/path/to/todo.org"
          "~/path/to/calendar.org"
          )
        )

Now that everything is set up, you can M-x khalel-import-events and you’ll see your events in you agenda (with C-c a a for example).