Is there a way to add R packages straight from GitHub?

So I see that R packages are quasi-automatically generated from CRAN, the R package repository. But some packages aren’t yet on CRAN, but are only on GitHub. For example, the library nominatim is not yet in nixpkgs, but you can install it in R / RStudio using the devtools library, and running devtools::install_github("hrbrmstr/nominatim")

I’m guessing all that does is grab the package from that GitHub repo, and install it to my local R packages directory.

But it’d be really nice to be able to add GitHub-based R packages to my R module.

My R module currently looks like this:

{ config, lib, pkgs, ... }:

    let myRPackages = with pkgs.rPackages;
          [
            devtools
           ...
            xfun
            xml2
          ];
in {
  environment.systemPackages = with pkgs; [
     (rWrapper.override { packages = myRPackages; } )
     (rstudioWrapper.override { packages = myRPackages; } )
  ];
}

What would be really nice is to be able to specify additional packages from GitHub, and add them to this module. Is there a way to do that? I’m guessing it’d just be a matter of making a derivation and setting the paths correctly so that R can see the packages where they’re downloaded. But unfortunately I’m not that good at Nix yet, and am pretty lost with thinking about how that would happen. Does anyone have any ideas?

There is a pending PR for that:
https://github.com/NixOS/nixpkgs/pull/93883

In the mean time just use fetchFromGitHub and buildRPackage, e.g., something like:

let 
  pmt = pkgs.rPackages.buildRPackage {
    name = "pmt";
    src = pkgs.fetchFromGitHub {
      owner = "jbedo";
      repo = "pmt";
      rev = "f5af7ada6f419382415335f5ae283f8a4643f79c";
      sha256 = "ODIRNRKsqBOqrOINQpOBOET5izKmhnp2F8DCVl4BOQI=";
    };
  };

myRPackages = with pkgs.rPackages;
  [
    ...
    pmt
    ...
  ];
in ...

You’ll have to specify any dependencies for the package with propagatedBuildInputs = [ ... ];.

4 Likes

How to calculate “sha256” ?

I know now, use lib.fakeSha256.

I still have a question, for example, how to package DirichletMultinomial(https://github.com/mtmorgan/DirichletMultinomial), it need gsl library.
Below is what I got:

builder for '/nix/store/6762vmzms7l89852lzydhvv89ay0w8zj-r-DirichletMultinomial-1.40.0.drv' failed with exit code 1;
       last 10 log lines:
       > /nix/store/561wgc73s0x1250hrgp7jm22hhv7yfln-bash-5.2-p15/bin/bash: line 1: gsl-config: command not found
       > /nix/store/l0fvy72hpfdpjjs3dk4112f57x7r3dlm-gcc-wrapper-12.2.0/bin/cc -I"/nix/store/prmd8sh2bdnml14js0f7dx766w8jdlpy-R-4.2.2/lib/R/include" -DNDEBUG     `gsl-config --cflags` -DR_DIRICHLETMULTINOMIAL -fpic  -g -O2  -c dirichlet_fit_main.c -o dirichlet_fit_main.o
       > /nix/store/561wgc73s0x1250hrgp7jm22hhv7yfln-bash-5.2-p15/bin/bash: line 1: gsl-config: command not found
       > dirichlet_fit_main.c:1:10: fatal error: gsl/gsl_rng.h: No such file or directory
       >     1 | #include <gsl/gsl_rng.h>
       >       |          ^~~~~~~~~~~~~~~
       > compilation terminated.
       > make: *** [/nix/store/prmd8sh2bdnml14js0f7dx766w8jdlpy-R-4.2.2/lib/R/etc/Makeconf:169: dirichlet_fit_main.o] Error 1
       > ERROR: compilation failed for package 'DirichletMultinomial'
       > * removing '/nix/store/z86w78rdr13vxm452p3mmn29w7z48dbq-r-DirichletMultinomial-1.40.0/library/DirichletMultinomial'
       For full logs, run 'nix log /nix/store/6762vmzms7l89852lzydhvv89ay0w8zj-r-DirichletMultinomial-1.40.0.drv'.

Add in the dependency with buildInputs = [ pkgs.gsl ];.

1 Like

Thank you! It works.