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 --install

You can find further installation notes at this official MS documentation1.

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 entry2:

# in WSL2 Ubuntu shell

sudo -i
nano /etc/wsl.conf

This will open /etc/wsl.conf in nano, a command line text editor. Add the following line:

[boot]
systemd=true

Save the file with CTRL-O and then quit nano with CTRL-X. Then, type the following line in PowerShell:

wsl --shutdown

and 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 VirtualBox3 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 Systems4, 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):

curl --proto '=https' --tlsv1.2 -sSf \
    -L https://install.determinate.systems/nix | \
    sh -s -- install --no-confirm --extra-conf "
trusted-users = root $USER
substituters = https://cache.nixos.org https://rstats-on-nix.cachix.org
trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= rstats-on-nix.cachix.org-1:vdiiVgocg6WeJrODIqdprZRUrhi1JzhBnXv7aWI6+F0="

This single command installs Nix and configures everything T needs:

  • --no-confirm runs the installer non-interactively, so you don’t have to answer any prompts.
  • --extra-conf injects configuration directly into your Nix config during installation. The three settings it applies are:
  • trusted-users = root $USER: Marks your user as a trusted Nix user. This is required for binary caches and certain Nix operations to work without permission errors.
  • substituters = ...: Tells Nix to fetch pre-built packages from the NixOS cache and the rstats-on-nix Cachix cache (used by T for R and Python packages), avoiding long builds from source.
  • trusted-public-keys = ...: The cryptographic keys Nix uses to verify the authenticity of packages from those caches.

Because the installer handles all of this in one step, there are no manual post-install configuration steps when using the Determinate Systems installer. If you installed Nix through other means, follow along below.

3.2 Already Have Nix?

If you installed Nix through a method other than the Determinate Systems installer (e.g., the official Nix installer5, Homebrew, your Linux distribution’s package manager, or you’re on NixOS), you need to manually configure two more things.

3.2.1 Step 1: Add yourself as a trusted user

T uses binary caches that require your user to be in the trusted-users list. Without this, you’ll get “ignoring untrusted substituter” errors.

If you’re on NixOS, add this to your /etc/nixos/configuration.nix:

nix.settings.trusted-users = [ "root" "your-username" ];

Then rebuild:

sudo nixos-rebuild switch

On non-NixOS Linux, WSL or macOS, edit /etc/nix/nix.conf:

# Add your username to the trusted-users line
# If the line exists, append your username to it:
sudo sed -i 's/^trusted-users = .*/& your-username/' /etc/nix/nix.conf

# Or if no trusted-users line exists, add one:
echo "trusted-users = root $(whoami)" | sudo tee -a /etc/nix/nix.conf

Then restart the Nix daemon:

# Linux (systemd)
sudo systemctl restart nix-daemon

# macOS (launchd)
sudo launchctl kickstart -k system/org.nixos.nix-daemon

3.2.2 Step 2: Add the binary cache

T relies on pre-built R and Python packages from the rstats-on-nix Cachix cache. Without this cache, nix develop will try to build everything from source, which can take a very long time.

On NixOS, add this to your /etc/nixos/configuration.nix:

nix.settings = {
  substituters = [
    "https://cache.nixos.org"
    "https://rstats-on-nix.cachix.org"
  ];
  trustedPublicKeys = [
    "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY="
    "rstats-on-nix.cachix.org-1:vdiiVgocg6WeJrODIqdprZRUrhi1JzhBnXv7aWI6+F0="
  ];
};

Then rebuild:

sudo nixos-rebuild switch

On non-NixOS Linux, WSL or macOS, add these lines to /etc/nix/nix.conf:

echo "substituters = https://cache.nixos.org https://rstats-on-nix.cachix.org" | sudo tee -a /etc/nix/nix.conf
echo "trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= rstats-on-nix.cachix.org-1:vdiiVgocg6WeJrODIqdprZRUrhi1JzhBnXv7aWI6+F0=" | sudo tee -a /etc/nix/nix.conf

Then restart the Nix daemon (same commands as above).

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 sl

you will likely see something like this:

which: no sl in ....

now run this:

nix shell nixpkgs#sl

and then again:

which sl

this time you should see something like:

/nix/store/cndqpx74312xkrrgp842ifinkd4cg89g-sl-5.05/bin/sl

This 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 Why a text editor (and not a notebook)?

Before we configure an IDE, a quick note on what we’ll be writing in it. You might expect data science to happen in a Jupyter notebook. We’ll mostly be working in plain-text files instead, for a few reasons.

A notebook is a recording of a conversation between you and the computer: run a cell, look at the output, change something, run it again, restart the kernel, run everything. That’s a fine way to explore, but a poor way to describe a system. Notebooks are JSON rather than plain text, which makes version control, code review, and collaboration painful, and they encourage messy code that mixes data, logic, and results in one file.

Plain text is the opposite: the simplest, most enduring interface in computing. Git can diff it, and it is easy to review and collaborate on. It also matters for the AI agents we’ll increasingly work with: notebooks carry hidden state, side effects, and non-linear execution, exactly what makes life hard for a model (more on this in Chapter 4).

3.4.2 Prerequisites

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 here6).

If you want to use VS Code proper, you can follow all the instructions here, but you need to install the REditorSupport7 and the Python8 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, we’ll see how to use Nix to install RStudio.

Other editors work well with Nix too. Emacs users can use the envrc9 or emacs-direnv10 packages to automatically load Nix environments. Neovim users can use direnv.nvim11. The key is that any editor with direnv support will work with the setup described below.

3.4.3 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 flake.nix, which will be generated by T. I haven’t talked about flake.nix files but essentially, a flake.nix file is a file that contains the specification of our development environments. direnv 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 profile install nixpkgs#direnv

This will install direnv and make it available even outside of Nix shells!

Then, I highly recommend installing the nix-direnv extension:

nix profile install nixpkgs#nix-direnv

It 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 step12 to make your terminal detect and load direnv automatically.

Then, in Positron, install the direnv13 extension. Finally, add a file called .envrc and simply write the following line in it (this .envrc file should be in the same folder as your project’s flake.nix):

use flake

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 use Nix to start building the environment. If that particular environment hasn’t been built and cached yet, it might take some time before you 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.4 In summary and next steps

We now have everything we need to start using T to set up reproducible data science projects. T, thanks to Nix, will handle the installation of all the required tools: R, Python, RStudio if you want it, and any other tool.


  1. https://learn.microsoft.com/en-us/windows/wsl/install↩︎

  2. https://ubuntu.com/blog/ubuntu-wsl-enable-systemd↩︎

  3. https://www.virtualbox.org/wiki/Downloads↩︎

  4. https://github.com/DeterminateSystems/nix-installer↩︎

  5. https://nixos.org/download/↩︎

  6. https://positron.posit.co/managing-interpreters.html↩︎

  7. https://marketplace.visualstudio.com/items?itemName=REditorSupport.r↩︎

  8. https://marketplace.visualstudio.com/items?itemName=ms-python.python↩︎

  9. https://github.com/purcell/envrc↩︎

  10. https://github.com/wbolster/emacs-direnv↩︎

  11. https://github.com/direnv/direnv.vim↩︎

  12. https://direnv.net/docs/hook.html↩︎

  13. https://github.com/direnv/direnv-vscode↩︎