DEV Community

John  Ajera
John Ajera

Posted on

Using Nix on Windows (the Right Way)

Using Nix on Windows (the Right Way)

Yes, you can use Nix on Windows, and the best way to do it is through WSL2. This gives you a real Linux environment where Nix behaves exactly as designed.

Running Nix directly on Windows without WSL is not practical for real development work.


1. Overview

Architecture:

Windows → WSL2 → Linux distro (Ubuntu recommended) → Nix

Why Ubuntu? It is the default and most commonly documented WSL distro, so things usually work with less friction. Nix works fine on other distros too.

Key Benefits:

  • Native Linux Environment: Full Linux compatibility for Nix
  • Reproducible Environments: Share configurations across Linux systems
  • Performance: Near-native performance when working in Linux filesystem
  • Professional Workflow: Common setup for DevOps and platform engineers

2. Prerequisites

Before starting, ensure you have:

  • Windows 10 (version 2004 or later) or Windows 11
  • Administrator access to enable WSL2
  • Internet connection for downloading WSL2 and Ubuntu
  • Basic familiarity with Linux command line

3. Install WSL2

Run in PowerShell:

wsl --install
Enter fullscreen mode Exit fullscreen mode

If prompted for elevation, accept the UAC prompt. WSL will download, enable the VirtualMachinePlatform feature, and may say a reboot is required. This is normal.

When the command finishes, reboot your computer to complete WSL2 setup. Ubuntu will install by default on first launch.

Example install output:

PS C:\Users\user> wsl --install
The requested operation requires elevation.
Downloading: Windows Subsystem for Linux 2.6.3
Installing: Windows Subsystem for Linux 2.6.3
Windows Subsystem for Linux 2.6.3 has been installed.
Installing Windows optional component: VirtualMachinePlatform

Deployment Image Servicing and Management tool
Version: 10.0.26100.5074

Image Version: 10.0.26200.7623

Enabling feature(s)
[==========================100.0%==========================]
The operation completed successfully.
The requested operation is successful. Changes will not be effective until the system is rebooted.
The requested operation is successful. Changes will not be effective until the system is rebooted.
Enter fullscreen mode Exit fullscreen mode

After reboot, running wsl --install again (or launching Ubuntu) may show this while the distro finishes setting up:

PS C:\Users\user> wsl --install
Downloading: Ubuntu
Installing: Ubuntu
Distribution successfully installed. It can be launched via 'wsl.exe -d Ubuntu'
Launching Ubuntu...
Provisioning the new WSL instance Ubuntu
This might take a while...
Create a default Unix user account: user
Enter fullscreen mode Exit fullscreen mode

You will be asked to create a Linux username and password. This account is your normal user inside Ubuntu.


4. Install Nix inside WSL

Open your Ubuntu terminal and run:

sh <(curl -L https://nixos.org/nix/install) --daemon
Enter fullscreen mode Exit fullscreen mode

Then load Nix into your shell:

. /etc/profile.d/nix.sh
Enter fullscreen mode Exit fullscreen mode

Verify installation:

nix-env --version
Enter fullscreen mode Exit fullscreen mode

You now have a full Nix environment running on Windows via Linux.


5. Quick Hello World with Nix

Now try a simple flake based dev shell demo with visible output.

In an empty folder inside WSL Ubuntu:

mkdir -p ~/nix-hello && cd ~/nix-hello

cat > flake.nix <<'EOF'
{
  description = "Hello Nix dev shell";

  inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

  outputs = { self, nixpkgs }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs { inherit system; };
    in {
      devShells.${system}.default = pkgs.mkShell {
        packages = [ pkgs.hello ];
      };
    };
}
EOF
Enter fullscreen mode Exit fullscreen mode

Run a command inside the dev environment:

nix develop -c hello
Enter fullscreen mode Exit fullscreen mode

You should see:

Hello, world!
Enter fullscreen mode Exit fullscreen mode

This proves flakes, dev shells, and package environments are all working correctly.


6. Performance Notes

With WSL2, performance is generally very good:

  • CPU performance is close to native
  • Memory performance is strong
  • Disk performance is fast when working inside the Linux filesystem

Important Tip

Do development work inside your Linux home directory, for example:

/home/youruser/projects
Enter fullscreen mode Exit fullscreen mode

Avoid working from Windows-mounted paths like:

/mnt/c/Users/yourname/...
Enter fullscreen mode Exit fullscreen mode

Accessing files through /mnt/c is slower because it crosses the Windows–Linux filesystem boundary.


7. Sharing Environments with Other Linux Systems

One of Nix’s biggest strengths is reproducibility.

If your project uses files like:

  • flake.nix
  • shell.nix
  • default.nix

You can use the same configuration on:

  • WSL Ubuntu on Windows
  • Native Ubuntu
  • NixOS
  • CI/CD runners

Example command:

nix develop
Enter fullscreen mode Exit fullscreen mode

This works consistently across machines, giving you identical toolchains and dependencies everywhere.


8. Popularity and Use Cases

Nix is especially popular among:

  • DevOps and platform engineers
  • Teams focused on reproducible builds
  • Polyglot projects needing multiple toolchains

It is more niche than Docker, but highly respected in engineering teams that value consistency and automation.


9. When Nix on Windows Is Not Ideal

This setup may not be the best fit if you:

  • Depend heavily on Windows-only GUI tools
  • Need tight integration with Visual Studio or Windows-native SDKs
  • Expect traditional Windows-style package managers

Nix in WSL shines for:

  • Development environments
  • Compilers and language toolchains
  • Reproducible CI environments

10. A Common Pro Setup

Many engineers use this workflow:

  • Windows as the primary OS
  • WSL2 Ubuntu for development
  • Nix flakes for reproducible environments
  • VS Code with the Remote – WSL extension

This setup feels close to native Linux while keeping access to Windows applications when needed.


11. Best Practices Summary

  • Use WSL2: Always use WSL2 for Nix on Windows, not native Windows installation
  • Work in Linux Filesystem: Keep projects in /home/youruser/projects, not /mnt/c/...
  • Use Flakes: Leverage Nix flakes for modern, reproducible environments
  • VS Code Remote WSL: Use the Remote – WSL extension for seamless development
  • Share Configurations: Use the same flake.nix or shell.nix across all Linux systems

12. References

Top comments (0)