Pointing Emacs Org-Roam Directory to Windows Path

tl;dr

How do I differentiate directories between computers using org-roam?

What I’m Trying to Do

I am trying to use org-roam on Linux for my personal notes, but also have access to my work notes, and also use it on a work Windows PC with only access to my work notes. I am using syncthing to syncronize my org-roam directory with subdirectories Work/ and Personal/ and have an .stignore file with Personal listed in it so that it doesn’t get copied to my work machine.

Each machine is building and updating it’s own database, so the path is important, but the file is not.

On Linux, everything is pretty much working as far as I can tell, but Windows is pointing to the wrong folder.

Below is my org-roam config in my init.el. I am looking for a way for org-roam to point it’s directory and db-location to:
"C:\Users\Zonsopkomst\Documents\Syncthing\org-roam"

instead of:
"C:\Users\Zonsopkomst\AppData\Roaming\Syncthing\org-roam"

while keeping keeping my setup for my Linux personal laptop and desktops intact. That being said, I wouldn’t mind being able to call some lisp that would determine which machine is using what. I have been told that maybe a (cond (<test for computer 1> (setq ...)) (<test for computer 2> (setq ...))) or conditional + system-name (function) or system-type (variable) could do the trick. Unfortunately, I am brand new to emacs, lisp, and programming in general, and truth be told am only an armchair hobbyist, more or less hacking things together and learning as I go.

My init.el org-roam Config

"org-roam"
"Org-roam is a plain-text knowledge management system. It brings some of Roam's more powerful features into the Org-mode ecosystem."
[[https://github.com/org-roam/org-roam]]
(use-package org-roam
  :custom
  (org-roam-directory "~/Syncthing/org-roam/")
  (org-roam-db-location "~/Syncthing/org-roam/org-roam.db")
  :bind (("C-c n l" . org-roam-buffer-toggle)
         ("C-c n f" . org-roam-node-find)
         ("C-c n i" . org-roam-node-insert))
  :config
  (org-roam-setup))

Any help is much appreciated!

I know this isn’t correct yet, but trying to post some things as I go along and revise them:

(use-package org-roam
  :custom
  ;; Change path for PC
  (eval (if system-name *zonsopkomst*
        ;; use Linux path
        (org-roam-directory "~/Syncthing/org-roam/")
        ;; otherwise use Windows path
        (org-roam-directory "c:/Users/WORKUSER$/Documents/Syncthing/org-roam/")))
  (eval (if system-name *zonsopkomst*
        ;; use Linux database
          (org-roam-db-location "~/Syncthing/org-roam/org-roam.db")
        ;; otherwise use Windows database
        (org-roam-db-location "c:/Users/WORKUSER$/Documents/Syncthing/org-roam.db")
  ;;(org-roam-directory "~/Syncthing/org-roam/")
  ;;(org-roam-db-location "~/Syncthing/org-roam/org-roam.db")
  :bind (("C-c n l" . org-roam-buffer-toggle)
         ("C-c n f" . org-roam-node-find)
         ("C-c n i" . org-roam-node-insert))
  :config
  (org-roam-setup))

Okay, here’s what I’ve got so far. Not sure if this is the ideal way of doing it, but:

(use-package org-roam
  :custom
  ;; Change path for PC
  (cond (system-name 'zonsopkomst' (setq org-roam-directory "~/Syncthing/org-roam/"))
        (system-name 'leeuwarden' (setq org-roam-directory "~/Syncthing/org-roam/"))
        (system-name 'WORKUSER$' (setq org-roam-directory "c:/Users/WORKUSER$/Documents/Syncthing/org-roam/")))
  (cond (system-name 'zonsopkomst' (setq org-roam-db-location "~/Syncthing/org-roam/org-roam.db"))
        (system-name 'leeuwarden' (setq org-roam-db-location "~/Syncthing/org-roam/org-roam.db"))
        (system-name 'WORKUSER$' (setq org-roam-db-location "c:/Users/WORKUSER$/Documents/Syncthing/org-roam/org-roam.db")))
  ;;(org-roam-directory "~/Syncthing/org-roam/")
  ;;(org-roam-db-location "~/Syncthing/org-roam/org-roam.db")
  :bind (("C-c n l" . org-roam-buffer-toggle)
         ("C-c n f" . org-roam-node-find)
         ("C-c n i" . org-roam-node-insert))
  :config
  (org-roam-setup))

Which gives me an message of:

org-roam--directory-files-recursively: Opening directory: No such file or directory, c:/Users/WORKUSER$/AppData/Roaming/org-roam

So it looks like it is still pointing to the old directory, but I don’t see that path in the code anywhere.

I suggest you add C:\Users\Zonsopkomst\Documents to variable HOME in your environment variable. Emacs will be able to point to it as ~, so you don’t need cond.

This is documented in the manual (online). Or in-system, evaluate (info "(efaq-w32) Location of init file") – do you know how to evaluate this? I’d put the cursor right after it and do C-x C-e, whcih is, by default, to call eval-last-sexp. I hope your set-up of Emacs has the Info documentation for you…

Below my Windows has this. C:\Users\nobiot = ~ in Emacs.

1 Like

Just to add… If you set HOME like I suggest, you’d need to move your init file accordingly.

If you wish to keep going this way, try moving this code from :custom to :config.

The part starting with:custom is specific to user options and I don’t believe it lets you add arbitrary code like this, but :config should.


Sorry, one more as I cannot unsee it…

Try something like this:


(cond ((string-equal (system-name) "zonsopkomst")
       (setq org-roam-directory "~/Syncthing/org-roam/"))
      ((string-equal (system-name) "leeuwarden")
       (setq org-roam-directory "~/Syncthing/org-roam/"))
       ...)

The function system-name returns a string.

  1. To specify a string, you need double-qutation, not single like you do
  2. You will need a function to compare strings and return nil or non-nil like string-equal in this case

I tried the following:

(use-package org-roam
;;  :custom
;;  (org-roam-directory "~/Syncthing/org-roam/")
;;  (org-roam-db-location "~/Syncthing/org-roam/org-roam.db")
  :bind (("C-c n l" . org-roam-buffer-toggle)
         ("C-c n f" . org-roam-node-find)
         ("C-c n i" . org-roam-node-insert))
  :config
  (org-roam-setup)
  ;; Change path for PC
  (cond (string-equal (system-name) "zonsopkomst" (setq org-roam-directory "~/Syncthing/org-roam/"))
        (string-equal (system-name) "leeuwarden" (setq org-roam-directory "~/Syncthing/org-roam/"))
        (string-equal (system-name) "WORKUSER$" (setq org-roam-directory "c:/Users/WORKUSER$/Documents/Syncthing/org-roam/")))
  (cond (string-equal (system-name) "zonsopkomst" (setq org-roam-db-location "~/Syncthing/org-roam/org-roam.db"))
        (string-equal (system-name) "leeuwarden" (setq org-roam-db-location "~/Syncthing/org-roam/org-roam.db"))
        (string-equal (system-name) "WORKUSER$" (setq org-roam-db-location "c:/Users/WORKUSER$/Documents/Syncthing/org-roam/org-roam.db"))))

Which is giving the same error, so no progress yet.

org-roam--directory-files-recursively: Opening directory: No such file or directory, c:/Users/WORKUSER$/AppData/Roaming/org-roam

The use of cond is incorrect syntactically. You are missing a pair of parentheses… I suggest you read my example more carefully.

But… if you follow my earlier advice on HOME environmental variables, I don’t think you need to code anything.

I think you need to restart Emacs after setting HOME.

Thank you @nobiot. As of today, I have one database populated on my Linux laptop and one on my Windows work desktop. I fixed my sloppy parentheses as you pointed out and had to fix some more sloppy path and system-name issues on the Linux side, but they both at least have databases now. Next steps are for me to learn and use org-roam.

Just a note for anyone who is reading this for their issue, @nobiot 's other solution of editing the HOME environmental variables is probably the easiest way if you are running one or more Windows PC and you have access to the environmental variables.

For those managing multiple machines on different platforms: I personally wanted to learn how to declare the computers in the init.el, so that I can easily change and replicate machines. So that is why I went this route in the end.

1 Like

Congratulations! And thank you for detailing your reason for the approach you took and your solution for future reference. I am sure people will benefit from your efforts.