• This note is about how to set up Nix on any WSL Linux distro.
  • This note is different from installing NixOS on WSL (which is more about how to install NixOS as a distribution on WSL).

According to this link, Nix can be installed on WSL in two ways:

  1. Multiuser installation
  2. Single-user installation

Prerequisites

I will be installing it for multiuser, but it requires WSL with Systemd enabled. Read this guide for how to configure systemd in WSL. But in short,

  • Ensure you are running WSL version 0.67.6 or higher. Confirm it by running wsl --version.

  • Make sure the following lines are added in /etc/wsl.conf (you might need to run your editor with sudo privileges, e.g.Β sudo nanoΒ /etc/wsl.conf):

    [boot]
    systemd=true
  • Close all the WSL distro Windows and run wslΒ --shutdown from PowerShell to restart your WSL instances.

  • Run the WSL distro again, you should have systemd running. You can confirm this by running:

    systemctl status

Installation

  1. Install Nix for the multiuser, using the following command:

    sh <(curl --proto '=https' --tlsv1.2 -L https://nixos.org/nix/install) --daemon

    The above command will do a couple of things:

    • Set up nix-daemon.service and nix-daemon.socket as Systemd services
    • Install Nix at /nix
    • Take backups of shell files
    • Create local users and a local group nixbld to run Nix builds
  2. Restart the WSL shell again to load the changes.

  3. Verify the Nix installation by running: nix --version.

Uninstallation

Read this guide on how to remove a multiuser installation from WSL / Linux.