1R packages 2========== 3 4## Installation 5 6Define an environment for R that contains all the libraries that you'd like to 7use by adding the following snippet to your $HOME/.config/nixpkgs/config.nix file: 8 9```nix 10{ 11 packageOverrides = super: let self = super.pkgs; in 12 { 13 14 rEnv = super.rWrapper.override { 15 packages = with self.rPackages; [ 16 devtools 17 ggplot2 18 reshape2 19 yaml 20 optparse 21 ]; 22 }; 23 }; 24} 25``` 26 27Then you can use `nix-env -f "<nixpkgs>" -iA rEnv` to install it into your user 28profile. The set of available libraries can be discovered by running the 29command `nix-env -f "<nixpkgs>" -qaP -A rPackages`. The first column from that 30output is the name that has to be passed to rWrapper in the code snipped above. 31 32However, if you'd like to add a file to your project source to make the 33environment available for other contributors, you can create a `default.nix` 34file like so: 35```nix 36let 37 pkgs = import <nixpkgs> {}; 38 stdenv = pkgs.stdenv; 39in with pkgs; { 40 myProject = stdenv.mkDerivation { 41 name = "myProject"; 42 version = "1"; 43 src = if pkgs.lib.inNixShell then null else nix; 44 45 buildInputs = with rPackages; [ 46 R 47 ggplot2 48 knitr 49 ]; 50 }; 51} 52``` 53and then run `nix-shell .` to be dropped into a shell with those packages 54available. 55 56## RStudio 57 58RStudio uses a standard set of packages and ignores any custom R 59environments or installed packages you may have. To create a custom 60environment, see `rstudioWrapper`, which functions similarly to 61`rWrapper`: 62 63```nix 64{ 65 packageOverrides = super: let self = super.pkgs; in 66 { 67 68 rstudioEnv = super.rstudioWrapper.override { 69 packages = with self.rPackages; [ 70 dplyr 71 ggplot2 72 reshape2 73 ]; 74 }; 75 }; 76} 77``` 78 79Then like above, `nix-env -f "<nixpkgs>" -iA rstudioEnv` will install 80this into your user profile. 81 82Alternatively, you can create a self-contained `shell.nix` without the need to 83modify any configuration files: 84 85```nix 86{ pkgs ? import <nixpkgs> {} 87}: 88 89pkgs.rstudioWrapper.override { 90 packages = with pkgs.rPackages; [ dplyr ggplot2 reshape2 ]; 91} 92 93``` 94 95Executing `nix-shell` will then drop you into an environment equivalent to the 96one above. If you need additional packages just add them to the list and 97re-enter the shell. 98 99## Updating the package set 100 101```bash 102nix-shell generate-shell.nix 103 104Rscript generate-r-packages.R cran > cran-packages.nix.new 105mv cran-packages.nix.new cran-packages.nix 106 107Rscript generate-r-packages.R bioc > bioc-packages.nix.new 108mv bioc-packages.nix.new bioc-packages.nix 109``` 110 111`generate-r-packages.R <repo>` reads `<repo>-packages.nix`, therefor the renaming. 112 113 114## Testing if the Nix-expression could be evaluated 115 116```bash 117nix-build test-evaluation.nix --dry-run 118``` 119 120If this exits fine, the expression is ok. If not, you have to edit `default.nix`