How to set up R and Rstudio on Nix OS

I’m trying to get Rstudio desktop (or an alternative GUI app for R) working on Nix OS, but I must be doing something wrong.

On other Linux distros, or even Windows, the general practice is to first install R as a language on your OS, and then install whatever app (like Rstudio) you want to add on top of it. I’ve followed this 2-step process on a normal distro (Fedora) and it works as expected.

I don’t see a similar process for Nix, after having checked the Wiki and the Nix OS Manual. I have added the rstudio package under environment.systemPackages, plus a few other R packages like rPackages.learnr, rPackages.shiny and rPackages.tidyverse.

Rstudio launches, but none of the additional rPackages I’ve added show as installed and they cannot be loaded. I’ve tried rstudioWrapper but that doesn’t make any difference.

It feels like I’ve missed some step, like first installing the R language as a whole (like on other distros).

Are there other things that need to be included in a Nix config to use R, with or without a GUI app? Is there any special procedure to get R working on Nix OS, regardless of if you want to add a GUI to it or not?

I’ve tried searching Discourse, but unfortunately the letter R is too short to generate search results!!!

See R - NixOS Wiki for an example of how to correctly override RStudio.
You have to override the rstudio package to make it see the needed R packages.

Thank you.

I’ve tried adding the code exactly as it is in the wiki, but I keep getting this error:

error: undefined variable ‘rstudioWrapper’

Is there a particular way this needs to be added to the config, in terms of the peculiarities of the Nix expression language itself?

Ah, yeah, I see where your confusion is coming from.
The examples above assumed with pkgs; at the top of the file.
This is not really recommended, since it can confuse people where values are coming from, like in this case. (Maybe the example needs to be improved).

Here’s a more solid example of what you’d need to add to your environment.systemPackages

    (pkgs.rstudioWrapper.override {
      packages = with pkgs.rPackages; [
        ggplot2
        dplyr
        xts
      ];
    })
1 Like

Yes! That’s it!

Thank you so much. Now it works exactly as I’d hoped.

Thanks for the help.