0

I'd like to use ESS (Emacs Speaks Statistics) with the nix version of R and the packages I need.

Here's what I can do via the shell

nix-shell shell.nix -I nixpkgs=/Users/dom/nixpkgs --run R

shell.nix ensures I have R and the R packages available without have to do install.packages. E.g. I get the version of ggplot2 in nix:

R version 3.4.3 (2017-11-30) -- "Kite-Eating Tree"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin17.3.0 (64-bit)


> installed.packages()["ggplot2","LibPath"]
installed.packages()["ggplot2","LibPath"]
[1] "/nix/store/4nm5rs5d0ywpw7ksd0hblminim4kvnr0-r-ggplot2-2.2.1/library"

It has been suggested that I use nix-buffer (https://github.com/shlevy/nix-buffer). I have the following in the directory in which I am trying to run R

dir-locals.nix:

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

When I do M-x nix-buffer I can see that a file called /nix/store/43r2rms3j5y9qclwd4kipbvribkjbr4d-dir-locals.el is loaded and this contains

(require 'inherit-local "/nix/store/5pgm72fjmkzi09qncvffx18dr9957lds-emacs-inherit-local-1.1.1/share/emacs/site-lisp/elpa/inherit-local-1.1.1/inherit-local.elc")


; Only set up nixpkgs buffer handling when we have some buffers active
(defvar nixpkgs--buffer-count 0)
(when (eq nixpkgs--buffer-count 0)
  (make-variable-buffer-local 'nixpkgs--is-nixpkgs-buffer)
  ; When generating a new temporary buffer (one whose name starts with a space), do inherit-local inheritance and make it a nixpkgs buffer
  (defun nixpkgs--around-generate (orig name)
    (if (and nixpkgs--is-nixpkgs-buffer (eq (aref name 0) ?\s))
        (let ((buf (funcall orig name)))
          (progn
            (inherit-local-inherit-child buf)
            (with-current-buffer buf
              (setq nixpkgs--buffer-count (1+ nixpkgs--buffer-count))
              (add-hook 'kill-buffer-hook 'nixpkgs--decrement-buffer-count nil t)))
          buf)
      (funcall orig name)))
  (advice-add 'generate-new-buffer :around #'nixpkgs--around-generate)
  ; When we have no more nixpkgs buffers, tear down the buffer handling
  (defun nixpkgs--decrement-buffer-count ()
    (setq nixpkgs--buffer-count (1- nixpkgs--buffer-count))
    (when (eq nixpkgs--buffer-count 0)
      (advice-remove 'generate-new-buffer #'nixpkgs--around-generate)
      (fmakunbound 'nixpkgs--around-generate)
      (fmakunbound 'nixpkgs--decrement-buffer-count))))
(setq nixpkgs--buffer-count (1+ nixpkgs--buffer-count))
(add-hook 'kill-buffer-hook 'nixpkgs--decrement-buffer-count nil t)


; Add packages to PATH and exec-path
(make-local-variable 'process-environment)
(put 'process-environment 'permanent-local t)
(inherit-local 'process-environment)
; setenv modifies in place, so copy the environment first
(setq process-environment (copy-tree process-environment))
(setenv "PATH" (concat "/nix/store/cz79yxnzsi2262i972pbfgvd93lf9y5k-R-3.4.3/bin:" (getenv "PATH")))
(inherit-local-permanent exec-path (append '("/nix/store/cz79yxnzsi2262i972pbfgvd93lf9y5k-R-3.4.3/bin") exec-path))


(setq nixpkgs--is-nixpkgs-buffer t)
(inherit-local 'nixpkgs--is-nixpkgs-buffer)

If I do M-x getenv PATH I see

/Users/dom/.nix-profile/bin:/Users/dom/.nix-profile/sbin:/Users/dom/.nix-profile/lib/kde4/libexec:/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:/nix/var/nix/profiles/default/lib/kde4/libexec:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin

But if I do M-x nix-buffer and M-x getenv PATH I see

/nix/store/cz79yxnzsi2262i972pbfgvd93lf9y5k-R-3.4.3/bin:/Users/dom/.nix-profile/bin:/Users/dom/.nix-profile/sbin:/Users/dom/.nix-profile/lib/kde4/libexec:/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:/nix/var/nix/profiles/default/lib/kde4/libexec:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin

So I feel ESS should now be able to find R but if I do I get

Type C-h m for help on ESS version 17.11
apply: Searching for program: No such file or directory, R

and now the path is missing the location of R if I now do M-x getenv PATH

/Users/dom/.nix-profile/bin:/Users/dom/.nix-profile/sbin:/Users/dom/.nix-profile/lib/kde4/libexec:/nix/var/nix/profiles/default/bin:/nix/var/nix/profiles/default/sbin:/nix/var/nix/profiles/default/lib/kde4/libexec:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin
  • What is nix R and why do you want it? Give us just a little help to understand your question. ESS works with "R". I suggest also ask in the ESS email list. It is more active than this site. ESS authors hang out there – pauljohn32 Feb 7 '18 at 4:22
  • nix is a package manager; with it you can load R and all the packages you need. Yes I can get ESS to work with R installed via e.g. home-brew but then I can't manage packages the way I would like. I have asked on the ESS mailing list. I think my only way forward is to understand ESS' internals. – idontgetoutmuch Feb 8 '18 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.