Using nix-buffer with Emacs

13 Dec 2019

I do a lot of note-taking in Emacs, and have recently started using org-babel within my notes. Being on NixOS, this proved to be slightly problematic: org-babel requires the executables to be in PATH. In NixOS this is concerning: we don’t want the executables to be available globally.

My traditional approach would be to use the direnv integration in Emacs, populating my path in a certain directory. However, in this case I keep my notes in a flat hierarchy:

/home/jethro/.org/braindump/org/
├── artificial_intelligence.org
├── auto
├── bayesian_inference.org
├── bittorrent.org
├── blockchain.org
├── cite
├── coding_interview.org
├── computer_organization.org
├── computer_vision.org

In this case direnv doesn’t let me load different environments for different files. Here I turn to nix-buffer, which by default loads a dir-locals.nix file of this form:

let pkgs = import <nixpkgs> {}; in
  pkgs.nixBufferBuilders.withPackages [ pkgs.rWrapper ]

To make it load a specific Nix file per org-file, all that’s left is to set a file-local variable for nix-buffer-root-file. And that’s it!