3 Setting Up Your Environment
In this chapter, we will install Nix, configure the rstats-on-nix binary cache to speed up installations, and set up our development environment (Positron, VS Code, or Emacs) to work seamlessly with reproducible Nix environments.
3.1 Installing Nix
3.1.1 For Windows Users: WSL2 Prerequisites
If you are on Windows, you need the Windows Subsystem for Linux 2 (WSL2) to run Nix. If you are on a recent version of Windows 10 or 11, you can simply run this as an administrator in PowerShell:
wsl --installYou can find further installation notes at this official MS documentation.
I recommend activating systemd in Ubuntu WSL2, mainly because this supports users other than root running Nix. To set this up, please follow this official Ubuntu blog entry:
# in WSL2 Ubuntu shell
sudo -i
nano /etc/wsl.confThis will open /etc/wsl.conf in nano, a command line text editor. Add the following line:
[boot]
systemd=trueSave the file with CTRL-O and then quit nano with CTRL-X. Then, type the following line in powershell:
wsl --shutdownand then relaunch WSL (Ubuntu) from the start menu. For those of you running Windows, we will be working exclusively from WSL2 now. If that is not an option, then I highly recommend you set up a virtual machine with Ubuntu using VirtualBox for example, or dual-boot Ubuntu.
3.1.2 The Determinate Systems installer
Installing (and uninstalling) Nix is quite simple, thanks to the installer from Determinate Systems, a company that provides services and tools built on Nix, and works the same way on Linux (native or WSL2) and macOS.
Do not use your operating system’s package manager to install Nix. Instead, simply open a terminal and run the following line (on Windows, run this inside WSL, and after the prerequisites listed above):
{sh parsermd-chunk-1, eval = FALSE} curl --proto '=https' --tlsv1.2 -sSf \ -L https://install.determinate.systems/nix | \ sh -s -- install
Just follow the instructions on screen, and in no time Nix will be available on your machine!
3.2 Configuring the Cache
Next, install the cachix client and configure the rstats-on-nix cache: this will install binary versions of many R packages which will speed up the building process of environments:
nix-env -iA cachix -f https://cachix.org/api/v1/installthen use the cache:
cachix use rstats-on-nixYou only need to do this once per machine you want to use {rix} on. Many thanks to Cachix for sponsoring the rstats-on-nix cache!
If you get this warning when trying to install software with Nix:
warning: ignoring the client-specified setting 'trusted-public-keys', because it is a restricted setting and you are not a trusted user
warning: ignoring untrusted substituter 'https://rstats-on-nix.cachix.org', you are not a trusted user.
Run `man nix.conf` for more information on the `substituters` configuration option.
warning: ignoring the client-specified setting 'trusted-public-keys', because it is a restricted setting and you are not a trusted user
Then this means that configuration was not successful. You need to add your user to /etc/nix/nix.custom.conf:
sudo nano /etc/nix/nix.custom.confthen simply add this line in the file:
trusted-users = root YOURUSERNAME
where YOURUSERNAME is your current login user name.
3.3 Verifying installation with Temporary Shells
You now have Nix installed; before continuing, let’s see if everything works (close all your terminals and reopen them) by dropping into a temporary shell with a tool you likely have not installed on your machine.
Open a terminal and run:
which slyou will likely see something like this:
which: no sl in ....now run this:
nix-shell -p sland then again:
which slthis time you should see something like:
/nix/store/cndqpx74312xkrrgp842ifinkd4cg89g-sl-5.05/bin/slThis is the path to the sl binary installed through Nix. The path starts with /nix/store: the Nix store is where all the software installed through Nix is stored. Now type sl and see what happens!
Temporary shells are quite useful, especially if you want to simply run a command using some tool that you only need infrequently. But this is not how we are going to be using Nix.
3.4 Configuring your IDE
3.4.1 Pre-requisites
We now need to configure an IDE to use our Nix shells as development environments. You are free to use whatever IDE you want but the instructions below are going to focus on Positron, which is a fork of VS Code geared towards data science. It works well with both Python and R and makes it quite easy to choose the right R or Python interpreter (which you’ll have to do to make sure you’re using the one provided by Nix, see here).
If you want to use VS Code proper, you can follow all the instructions here, but you need to install the REditorSupport and the Python extension. You will also need to add the {languageserver} R package to your Nix shells.
On Windows, you need to install Positron on Windows, not inside WSL.
If you want to use RStudio, you can, but you will need to install it through Nix: an RStudio installed through the usual means for your system is not going to be able to interact with Nix! This is a limitation of RStudio and there is currently no workaround. If you want to use RStudio, just skip the rest of the chapter, as it’s irrelevant to you. Later, when we use {rix} to set up our first Nix development environment, I’ll tell you what to do to use RStudio.
Other editors work well with Nix too. Emacs users can use the envrc or emacs-direnv packages to automatically load Nix environments. Neovim users can use direnv.nvim. The key is that any editor with direnv support will work with the setup described below.
3.4.2 direnv
Once Positron is installed, you need to install a piece of software called direnv: direnv will automatically load Nix shells when you open a project that contains a default.nix file in an editor.1 It works on any operating system and many editors support it, including Positron. If you’re using Windows, install direnv in WSL (even though you’ve just installed Positron for Windows). To install direnv run this command:
nix-env -f '<nixpkgs>' -iA direnvThis will install direnv and make it available even outside of Nix shells!
Then, I highly recommend to install the nix-direnv extension:
nix-env -f '<nixpkgs>' -iA nix-direnvIt is not mandatory to use nix-direnv if you already have direnv, but it’ll make loading environments much faster and seamless.
Finally, if you haven’t used direnv before, don’t forget this last step to make your terminal detect and load direnv automatically.
Then, in Positron, install the direnv extension. Finally, add a file called .envrc and simply write the following two lines in it (this .envrc file should be in the same folder as your project’s default.nix):
use nix
mkdir $TMPin it. On Windows, remotely connect to WSL first, but on other operating systems, simply open the project’s folder using File > Open Folder... and you will see a pop-up stating direnv: /PATH/TO/PROJECT/.envrc is blocked and a button to allow it. Click Allow and then open an R script. You might get another pop-up asking you to restart the extension, so click Restart. Be aware that at this point, direnv will run nix-shell and so will start building the environment. If that particular environment hasn’t been built and cached yet, it might take some time before Code will be able to interact with it. You might get yet another popup, this time from the R Code extension complaining that R can’t be found. In this case, simply restart Positron and open the project folder again: now it should work every time.
3.4.3 In summary and next steps
For a new project, simply repeat this process:
- Generate the project’s
default.nixfile (we will see how in the next chapter); - Build it using
nix-build; - Create an
.envrcand write the two lines from above in it; - Open the project’s folder in Positron and click allow when prompted;
- Restart the extension and Positron if necessary.
Another option is to create the .envrc file and write the two required lines, then open a terminal, navigate to the project’s folder, and run direnv allow. Doing this before opening Positron should not prompt you anymore.
If you’re on Windows, using Positron like this is particularly interesting, because it allows you to install Positron on Windows as usual, and then you can configure it to interact with a Nix shell, even if it’s running from WSL. This is a very seamless experience.
A
default.nixfile is a file that contains the specification of our development environments, and is the file that will be automatically generated by{rix}↩︎