This post is part of the R x Nix Nexus: Crafting Cohesive Environments series.

A short post on my current set-up for R with nixpkgs and emacs to auto-compile my system configuration.

Background

This is my third post on working with nixpkgs and R.

  • Part I covered ways of working effectively with R and nixpkgs
  • Part II dealt with composing dependent devtools packages in a per-package environment, with a focus on rethinking and tidybayes.rethinking

This final part is about automating the system-wide configuration using emacs. Specifically doom-emacs. Naturally, this is the most optimal way to work with nix packages as well.

System Configuration

After experimenting with a per-project layout, I decided to use the full system configuration instead of the per-project layout. So I simply set:

 1# $HOME/.config/nixpkgs/config.nix
 2{
 3  packageOverrides = super:
 4    let
 5      self = super.pkgs;
 6      rethinking = with self.rPackages;
 7        buildRPackage {
 8          name = "rethinking";
 9          src = self.fetchFromGitHub {
10            owner = "rmcelreath";
11            repo = "rethinking";
12            rev = "d0978c7f8b6329b94efa2014658d750ae12b1fa2";
13            sha256 = "1qip6x3f6j9lmcmck6sjrj50a5azqfl6rfhp4fdj7ddabpb8n0z0";
14          };
15          propagatedBuildInputs = [ coda MASS mvtnorm loo shape rstan dagitty ];
16        };
17      tidybayes_rethinking = with self.rPackages;
18        buildRPackage {
19          name = "tidybayes.rethinking";
20          src = self.fetchFromGitHub {
21            owner = "mjskay";
22            repo = "tidybayes.rethinking";
23            rev = "df903c88f4f4320795a47c616eef24a690b433a4";
24            sha256 = "1jl3189zdddmwm07z1mk58hcahirqrwx211ms0i1rzbx5y4zak0c";
25          };
26          propagatedBuildInputs =
27            [ dplyr tibble rlang MASS tidybayes rethinking rstan ];
28        };
29    in {
30      rEnv = super.rWrapper.override {
31        packages = with self.rPackages; [
32          tidyverse
33          devtools
34          modelr
35          purrr
36          forcats
37          ####################
38          # Machine Learning #
39          ####################
40          # MLR3
41          mlr3
42          mlr3viz
43          mlr3learners
44          mlr3pipelines
45          # Plotting tools
46          ggplot2
47          cowplot
48          ggrepel
49          RColorBrewer
50          # Stan Stuff
51          rstan
52          tidybayes
53          # Text Utilities
54          orgutils
55          latex2exp
56          kableExtra
57          knitr
58          data_table
59          printr
60          # Devtools Stuff
61          rethinking
62          tidybayes_rethinking
63        ];
64      };
65    };
66}

If any of these look strange, refer to the earlier posts.

Automation Pains

direnv, lorri and niv (the heroes of Part II) are not really useful for working with the system-wide configuration, but an elegant solution still exists, which leverages firestarter and after-save-hooks in emacs.

Firestarter

Firestarter is my favorite method of working with shell commands after saving things. My setup is simply:

1; packages.el
2(package! firestarter)

This is coupled with a simple configuration.

1; config.el
2(use-package! firestarter
3  :ensure t
4  :init
5  (firestarter-mode)
6  :config
7  (setq firestarter-default-type t)
8)

The default type corresponds to demanding the shell outupt for the commands.

Nix-R Stuff

To finalize this setup, we will need to modify our system configuration slightly. For brevity, we simply note the following local variables.

1# $HOME/.config/nixpkgs/config.nix
2# Local Variables:
3# firestarter: "nix-env -f '<nixpkgs>' -iA rEnv"
4# firestarter-default-type: (quote failure)
5# End:

The firestarter-default-type used here is to ensure that errors are displayed in a buffer.

To check what is being installed (if anything) simply run:

1nix-env -f "<nixpkgs>" -iA rEnv --dry-run

Conclusion

This is my current setup. It works out better than most of my other attempts and seems to be an optimal approach. The packages are versioned, everything is automated, and I can reproduce changes across all my machines. Will stick with this.


Series info

R x Nix Nexus: Crafting Cohesive Environments series

  1. Nix with R and devtools
  2. Statistical Rethinking and Nix
  3. Emacs for Nix-R <-- You are here!