How to have device-specific values in .dir-locals.el

*
planted: 25/04/2025last tended: 26/04/2025

I use Emacs on my laptop and on my phone (in Termux), and I use org-roam on both devices.

I have two org-roam-based projects. One is this public digital garden, one is a private space. Because I have two projects, I have to use the .dir-locals.el file to set the location of org-roam-directory and org-roam-db-location per project.

The locations of the org-roam directories and their DBs are different on my laptop and my phone. So, I need a way to have device-specific values in .dir-locals.el.

I have one .dir-locals.el synced between the devices with syncthing. It sets values other than just these ones.

I was doing this fairly naively for a while, having a line for each and commenting it out each time I was working on a different device:

((nil
  (org-mode ngm-journal-mode))
 (org-mode
  ;(org-roam-directory . "/home/neil/org/Personal/")
  (org-roam-directory . "~/storage/shared/org/Personal/")
  ;(org-roam-db-location . "/home/neil/org/Personal/org-roam.db")))
  (org-roam-db-location . "~/storage/shared/org/Personal/org-roam.db")))

But that got a bit annoying.

Now I've got a conditional checking for the hostname and setting the variables accordingly:

((nil
  (org-mode ngm-journal-mode))
 (org-mode
  (eval . (cond
           ;; Laptop configuration
           ((string= system-name "neil-ThinkPad-T450s")
            (setq-local org-roam-directory "/home/neil/org/Personal/")
            (setq-local org-roam-db-location "/home/neil/org/Personal/org-roam.db"))
           ;; Phone configuration
           ((string= system-name "localhost")
            (setq-local org-roam-directory "~/storage/shared/org/Personal/")
            (setq-local org-roam-db-location "~/storage/shared/org/Personal/org-roam.db"))))))

That's working, although I'm not totally happy with it. Seems a bit crufty - the duplication doesn't feel great.

When I get chance, I'll investigate making use of .dir-locals-2.el.

You can also use .dir-locals-2.el; if found in the same directory as .dir-locals.el, Emacs loads it in addition to .dir-locals.el. This is useful when .dir-locals.el is under version control in a shared repository and can’t be used for personal customizations.

Directory Variables (GNU Emacs Manual)

I should also double-check whether I could just use relative paths, which would solve this specific problem.

1. Elsewhere

1.1. In my garden

Notes that link to this note (AKA backlinks).

1.3. Mentions

Recent changes. Source. Peer Production License.