# Installing System Tools and TexLive Packages in a Nix Environment

## Introduction

More than 100’000 pieces of software are available through the Nix package manager. Nix’s repository of packages is called `nixpkgs` and it includes the entirety of CRAN and Bioconductor. `nixpkgs` is actually “just” a GitHub repository containing thousands upon thousands of Nix expressions. When installing a package, these expressions get evaluated, and the package in question gets installed. What “installed” means can vary: sometimes the package gets built from source, sometimes a pre-compiled binary package for your operating system gets downloaded and made available.

For example, here is the Nix expression that downloads and installs Quarto. This is an example of an expression that downloads the pre-compiled binary from Quarto’s own GitHub repository, and then installs it. The installation process in this case is essentially making sure that Quarto is able to find its dependencies, which also get installed from Nix, and some R and Python packages to make Quarto work well with both languages also get installed.

It is possible to use `rix()` to add tools to an environment and this vignette explains how.

## Adding tools to an environment

The call below generates a `default.nix` that defines an environment with the latest version of R available in `nixpkgs`. The R `{quarto}` package is also installed, as well as the `quarto` command line tool (required to edit Quarto documents from R using the `{quarto}` package) and git:

    path_default_nix <- tempdir()

    rix(
      r_ver = "latest-upstream",
      r_pkgs = c("quarto"),
      system_pkgs = c("quarto", "git"),
      git_pkgs = NULL,
      ide = "none",
      project_path = path_default_nix,
      overwrite = TRUE
    )

You can look for all the available software here. Simply look for the right package name, and add it to the `system_pkgs` argument of `rix()`. If you have trouble finding something, don’t hesitate to open an issue and ask for support!

## Installing TexLive packages

Whether you use Quarto, Rmarkdown, or Sweave, literate programming with R requires a TexLive distribution to be installed. You can use `rix()` to install a minimalist TexLive distribution and then add the packages that you require as you go. The basic use is to simply add a TexLive package to the `tex_pkgs` argument of `rix()` like this:

    path_default_nix <- tempdir()

    rix(
      r_ver = "latest-upstream",
      r_pkgs = c("quarto"),
      system_pkgs = "quarto",
      tex_pkgs = c("amsmath"),
      ide = "none",
      project_path = path_default_nix,
      overwrite = TRUE,
      print = TRUE
    )
    #> # This file was generated by the {rix} R package v0.7.1 on 2024-07-01
    #> # with following call:
    #> # >rix(r_ver = "12a9c0004bc987afb1ff511ebb97b67497a68e22",
    #> #  > r_pkgs = c("quarto"),
    #> #  > system_pkgs = "quarto",
    #> #  > tex_pkgs = c("amsmath"),
    #> #  > ide = "none",
    #> #  > project_path = path_default_nix,
    #> #  > overwrite = TRUE,
    #> #  > print = TRUE)
    #> # It uses nixpkgs' revision 12a9c0004bc987afb1ff511ebb97b67497a68e22 for reproducibility purposes
    #> # which will install R version latest.
    #> # Report any issues to https://github.com/ropensci/rix
    #> let
    #>  pkgs = import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/12a9c0004bc987afb1ff511ebb97b67497a68e22.tar.gz") {};
    #>
    #>   rpkgs = builtins.attrValues {
    #>     inherit (pkgs.rPackages)
    #>       quarto;
    #>   };
    #>
    #>   tex = (pkgs.texlive.combine {
    #>     inherit (pkgs.texlive)
    #>       scheme-small
    #>       amsmath;
    #>   });
    #>
    #>   system_packages = builtins.attrValues {
    #>     inherit (pkgs)
    #>       quarto
    #>       R
    #>       glibcLocales
    #>       nix;
    #>   };
    #>
    #> in
    #>
    #> pkgs.mkShell {
    #>   LOCALE_ARCHIVE = if pkgs.system == "x86_64-linux" then  "${pkgs.glibcLocales}/lib/locale/locale-archive" else "";
    #>   LANG = "en_US.UTF-8";
    #>    LC_ALL = "en_US.UTF-8";
    #>    LC_TIME = "en_US.UTF-8";
    #>    LC_MONETARY = "en_US.UTF-8";
    #>    LC_PAPER = "en_US.UTF-8";
    #>    LC_MEASUREMENT = "en_US.UTF-8";
    #>
    #>   buildInputs = [  rpkgs tex system_packages   ];
    #>
    #> }

This will automically add the small TexLive distribution available through `nixpkgs` with the `amsmath` LaTex package. To know more about setting up environments for literate programming, refer to the vignette `vignette("literate-programming")`.

## Installing IDEs

Environments built with Nix are not completely cut off from the rest of your system, and as such, you should be able to use your usual IDE to interact with Nix environments. The only exception is RStudio. Everything will be explained in greater detail in the vignette `vignette("configuring-ide")`.