Sharing an org-roam database between different contexts

I have a custom configuration to quickly switch between different contexts, which has their own GTD files, notes, etc. I still want to share some things though, especially many org-roam notes which isn’t tied to a specific context or is sensitive.

In order to do this, I set up a dummy context called “common”, and added a symlink to this folder in the other contexts. This means I have common/notes, and <some-profile>/notes/common as a symlink to common/notes.

The problem arise whenever I do a change in the common context as org-roam makes sure it only tracks files in a subfolder of the org-roam-directory, but buffer-file-name returns the canonical name, and thus isn’t a subfolder. Calling org-roam-db-sync is possible, but it’s slow and easy to forget.

To get around this, I used an :around advice for org-roam-descendant-of-p which matches my directory pattern and returns T when the context has the common symlink even though it isn’t a descendant.

Gotta love that this is actually possible! Awful hack or not, it let’s me have this workflow/setup even though org-roam doesn’t support it.

The code is littered with referenced to unpublished code, but the gist of it should hopefully make sense; return T for the common notes.

(defun sijo-profile--common-is-an-org-roam-descendant (orig &rest args)
  "Advice for `org-roam-descendant-of-p' to support a the 'common' profile notes
symlink.

This is important for autosyncing to work as it otherwise wouldn't see notes in
the common symlink directory as a descendant of the current profile notes
directory, and thus wouldn't syncronize the file.

I.e. if the current profile notes directory 'A/notes/common' is a symlink to
profile notes directory 'common/notes', return `T'"
  (let* ((needle (cl-first args))
         (common-path (sijo-profile--with-active-profile 'common (sijo-profile-notes-path)))
         (profile-has-common-subdir (cl-equalp (file-truename (sijo-profile-notes-path "common")) common-path))
         (needle-is-common (s-contains? common-path needle)))
    (if (and needle-is-common profile-has-common-subdir)
        t
      (apply orig args))))

(advice-add 'org-roam-descendant-of-p :around 'sijo-profile--common-is-an-org-roam-descendant)

Date: 2023-07-28 Fri 00:00

Author: Simen Endsjø

Created: 2023-07-28 Fri 19:54