NixOS on WSL2 Configuration Guide
This guide covers installing and configuring NixOS on Windows Subsystem for Linux 2 (WSL2) using this template, providing a full development environment that seamlessly integrates with Windows.
Table of Contents
- Overview
- Prerequisites
- Installation
- Configuration
- Windows Integration
- Development Environment
- Performance Optimization
- Troubleshooting
- Advanced Usage
Overview
NixOS on WSL2 provides a complete declarative Linux development environment that seamlessly integrates with Windows. This template includes comprehensive WSL2 support with specialized modules and optimizations.
Key Features
- Windows Integration - Seamless file system access, clipboard sharing, application launching
- Performance Optimizations - Memory management, network stack, filesystem tuning for WSL2
- Development Environment - Pre-configured with Zsh, development tools, and WSL2-specific utilities
- Modular Architecture - Specialized WSL2 modules for different aspects of integration
- Container Support - Docker/Podman with Windows integration
- GUI Application Support - X11 forwarding and WSLg compatibility
- Systemd Integration - Full systemd support with WSL2 optimizations
WSL2 Module System
The template includes dedicated WSL2 modules:
modules/wsl/interop.nix- Windows application integration, clipboard, file associationsmodules/wsl/networking.nix- Network optimizations, firewall configuration, diagnosticsmodules/wsl/optimization.nix- Performance tuning for memory, filesystem, servicesmodules/wsl/systemd.nix- Systemd service optimizations for WSL2 environment
Benefits
- Declarative Configuration - Reproducible development environments with version control
- Windows Compatibility - Native Windows tool integration with Linux development power
- Performance - Optimized for WSL2 with memory, network, and filesystem tuning
- Consistency - Same environment across different machines and team members
- Professional Development - Complete development stack with Windows interoperability
Prerequisites
Windows Requirements
- Windows 10 version 2004 and higher (Build 19041 and higher)
- Windows 11 (recommended)
- WSL 2 enabled with Virtual Machine Platform
- Administrator access for installation
Hardware Requirements
- CPU: x64 with virtualization support
- Memory: Minimum 4GB RAM (8GB+ recommended)
- Storage: 10GB+ available space
- GPU: Optional, for GUI applications and development
Software Prerequisites
-
Enable WSL 2:
# Run as Administrator wsl --install # or dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart -
Set WSL 2 as default:
wsl --set-default-version 2 -
Install Windows Terminal (recommended):
- From Microsoft Store or GitHub releases
Installation
Automated Installation (Recommended)
-
Download the template:
git clone https://github.com/yourusername/nixos-template cd nixos-template -
Run the installation script:
# Run PowerShell as Administrator .\scripts\install-wsl2.sh -
Follow the prompts:
- Choose username and password
- Wait for installation to complete (10-30 minutes)
Manual Installation
-
Download NixOS-WSL:
wget https://github.com/nix-community/NixOS-WSL/releases/latest/download/nixos-wsl-installer.tar.gz -
Import the distribution:
wsl --import NixOS-Template C:\WSL\NixOS-Template nixos-wsl-installer.tar.gz --version 2 -
Copy template configuration:
wsl -d NixOS-Template # Inside WSL: git clone https://github.com/yourusername/nixos-template /tmp/template sudo cp -r /tmp/template/hosts/wsl2-template/* /etc/nixos/ sudo nixos-rebuild switch
Configuration
System Configuration
The main system configuration is in /etc/nixos/configuration.nix:
# Core WSL2 settings from nixos-wsl
wsl = {
enable = true;
defaultUser = "nixos";
startMenuLaunchers = true; # Add apps to Windows Start Menu
interop = {
includePath = true; # Include Windows PATH in WSL PATH
register = true; # Register WSL interop
};
useWindowsDriver = true; # Use Windows OpenGL driver for better GPU performance
docker-desktop.enable = false; # Enable if using Docker Desktop
usbip.enable = false; # Enable for USB device access
};
# Template-specific WSL2 modules
modules.wsl = {
interop = {
enable = true; # Windows application integration
windowsApps = true; # Enable Windows app aliases (explorer.exe, code.exe)
clipboard = true; # Bidirectional clipboard sharing
fileAssociations = true; # Open files with Windows applications
};
networking = {
enable = true;
dnsConfig = "auto"; # "auto" | "wsl" | "custom"
firewallConfig = "disabled"; # "disabled" | "minimal" | "standard"
networkOptimizations = true; # TCP and network performance tuning
portForwarding = { # Custom port forwarding for development
ssh = 22;
web = 3000;
api = 8080;
};
};
optimization = {
enable = true;
memory = {
swappiness = 10; # Lower swap usage (0-100)
cacheOptimization = true;
hugepages = false; # Enable for memory-intensive workloads
};
filesystem = {
mountOptimizations = true; # Optimize Windows drive mounts
tmpfsSize = "2G"; # Size for /tmp tmpfs
noCOW = true; # Disable copy-on-write for large files
};
services = {
disableUnneeded = true; # Disable services not needed in WSL
optimizeSystemd = true; # Faster boot and service management
};
development = {
fastBuild = true; # Optimize for development builds
cacheNix = true; # Nix store optimizations
};
};
systemd = {
enable = true; # Systemd optimizations for WSL2
networkOptimizations = true; # Network service optimizations
serviceOptimizations = true; # General service optimizations
};
};
Module Configuration Details
Windows Interop Module (modules.wsl.interop)
Controls Windows integration features:
- windowsApps: Creates shell aliases for Windows applications
- clipboard: Enables clipboard sharing between Windows and WSL2
- fileAssociations: Allows opening files with appropriate Windows applications
- windowsPath: Includes Windows PATH in WSL environment
Networking Module (modules.wsl.networking)
Optimizes network performance and configuration:
- dnsConfig: DNS resolution strategy (auto-detect, use WSL default, or custom)
- firewallConfig: Firewall configuration level (disabled for dev, minimal, or standard)
- networkOptimizations: TCP window scaling, BBR congestion control
- portForwarding: Automatic port forwarding setup for development servers
Optimization Module (modules.wsl.optimization)
Performance tuning for WSL2 environment:
- Memory Management: Swappiness, cache pressure, transparent hugepages
- Filesystem: tmpfs configuration, mount optimizations, COW settings
- Services: Disable unnecessary services, optimize systemd timeouts
- Development: Parallel builds, Nix store optimizations, compiler flags
Systemd Module (modules.wsl.systemd)
Systemd service optimizations:
- Service Timeouts: Faster start/stop timeouts for WSL2
- Network Services: Optimized DNS and network service configuration
- Performance Services: Background services for performance monitoring
User Configuration
Home Manager configuration is in ~/.config/home-manager/home.nix:
# Import role-based configurations
imports = [
../../home/roles/developer.nix
../../home/profiles/headless.nix
];
# WSL2-specific shell aliases
programs.zsh.shellAliases = {
explorer = "explorer.exe";
code = "code.exe";
wsl-open = "explorer.exe .";
};
Customization
-
Edit system configuration:
sudo nano /etc/nixos/configuration.nix -
Edit user configuration:
nano ~/.config/home-manager/home.nix -
Apply changes:
sudo nixos-rebuild switch home-manager switch
Windows Integration
File System Access
- Windows drives: Mounted at
/mnt/c,/mnt/d, etc. - WSL home: Accessible from Windows at
\\wsl$\NixOS-Template\home\username - Performance tip: Keep development files on WSL filesystem (
/home)
Application Integration
# Open current directory in Windows Explorer
wsl-open .
# Edit file in VS Code
wsl-edit myfile.txt
# Run Windows applications
explorer.exe
notepad.exe file.txt
code.exe project/
Clipboard Integration
The template includes clipboard sharing between WSL and Windows:
# Copy to Windows clipboard
echo "text" | clip.exe
# In tmux/vim, clipboard operations work automatically
PATH Integration
Windows executables are available in WSL PATH:
# These work automatically
code .
explorer .
powershell.exe
cmd.exe
Development Environment
Included Development Tools
- Shell: Zsh with Oh-My-Zsh and Starship prompt, WSL2-specific functions
- Editor: Vim with optimized configuration and clipboard integration
- Version Control: Git with Windows credential integration and performance optimizations
- Languages: Node.js, Python, Rust, Go with WSL2 performance tuning
- Package Managers: npm, pip, cargo with parallel build optimizations
- Container Tools: Docker/Podman with Windows integration and GPU support
- Development Utilities: tmux with clipboard sync, fzf, jq, curl, wget, WSL-specific tools
WSL2-Specific Utilities
The template includes custom utilities for WSL2 integration:
System Information and Monitoring
wsl-info- Comprehensive WSL2 system informationwsl-network-info- Network configuration and diagnosticswsl-performance-tune- Performance analysis and optimizationsystem-info- Detailed hardware and software informationperformance-monitor- Real-time system performance monitoring
Windows Integration Utilities
wsl-open <path>- Open files/directories in Windows Explorerwsl-edit <file>- Edit files in Windows applications (VS Code, Notepad)wsl-ports- Show listening ports and serviceswsl-network-diagnostics- Network troubleshooting and connectivity tests
Development Environment Helpers
dev-start- Start development environment servicesdev-stop- Stop development servers and processesdev-env-setup- Initialize development directory structurewsl-restart- Restart WSL2 instance from within WSLwsl-shutdown- Shutdown WSL2 instance safely
Development Workflow
-
Clone projects to WSL filesystem:
cd ~ git clone https://github.com/user/project.git cd project -
Use integrated development tools:
# Open project in VS Code code . # Start development server npm run dev # Access from Windows browser: http://localhost:3000 -
Container development:
# Docker works with Windows Docker Desktop docker run -p 8080:80 nginx # Or use Podman podman run -p 8080:80 nginx
Project Structure
Recommended project organization:
~/Development/
├── projects/ # Main development projects
│ ├── web/ # Web development
│ ├── mobile/ # Mobile development
│ └── desktop/ # Desktop applications
├── tools/ # Development tools and utilities
└── scripts/ # Custom scripts and automation
Performance Optimization
WSL2 Configuration
Create or edit C:\Users\Username\.wslconfig:
[wsl2]
# Memory allocation (adjust based on your system)
memory=8GB
# Processor count
processors=4
# Swap file size
swap=2GB
# Networking mode
networkingMode=mirrored
# GUI applications
guiApplications=true
System Optimizations
The template includes several performance optimizations:
- Memory Management: Optimized swappiness and caching
- Network Performance: TCP optimizations for WSL2
- File System: tmpfs for
/tmp, optimized mount options - Service Management: Disabled unnecessary services
- Build Optimization: Parallel builds, Nix store optimization
Performance Monitoring
# System information and performance
wsl-info
wsl-performance-tune
# Resource monitoring
htop
iotop
performance-monitor
Best Practices
- File Location: Keep development files on WSL filesystem (
/home) - Memory Management: Use
wsl --shutdownperiodically to free memory - Network: Use
wsl-network-diagnosticsto troubleshoot connectivity - Storage: Regular cleanup with
nix-collect-garbage
Troubleshooting
Common Issues
WSL2 Won’t Start
-
Check WSL status:
wsl --status wsl --list --verbose -
Restart WSL service:
# As Administrator Restart-Service LxssManager -
Reset WSL2:
wsl --shutdown wsl --unregister NixOS-Template # Reinstall from backup
Network Issues
-
Check network configuration:
wsl-network-diagnostics -
Reset network settings:
sudo systemctl restart systemd-networkd sudo systemctl restart systemd-resolved -
Windows network reset:
# As Administrator netsh winsock reset netsh int ip reset
Performance Issues
-
Check resource usage:
performance-monitor htop -
Optimize WSL2 settings:
wsl-performance-tune -
Free up memory:
wsl --shutdown # Wait 8 seconds, then restart wsl -d NixOS-Template
GUI Applications
-
Install VcXsrv or similar X server on Windows
-
Set DISPLAY variable:
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0.0 -
Test GUI application:
# Install a simple GUI app nix-shell -p xeyes --run xeyes
Configuration Issues
NixOS Rebuild Fails
-
Check configuration syntax:
sudo nixos-rebuild dry-run -
Rollback to previous generation:
sudo nixos-rebuild switch --rollback -
Clean Nix store:
sudo nix-collect-garbage -d
Home Manager Issues
-
Check configuration:
home-manager build -
Reset Home Manager:
rm -rf ~/.local/state/home-manager/gcroots home-manager switch
Getting Help
-
Check system logs:
sudo journalctl -u multi-user.target sudo journalctl -f # Follow logs in real-time -
WSL logs:
# In PowerShell Get-EventLog -LogName Application -Source "Microsoft-Windows-Subsystem-Linux" -
Template-specific diagnostics:
wsl-systemd-status wsl-systemd-optimize
Advanced Usage
Custom Modules
Create custom WSL2-specific modules:
# modules/custom/wsl-dev.nix
{ config, lib, pkgs, ... }:
{
# Custom development environment for WSL2
environment.systemPackages = with pkgs; [
# Your custom packages
];
# Custom services
systemd.user.services.my-dev-service = {
# Service configuration
};
}
Multiple WSL Distributions
You can run multiple NixOS instances:
# Export current installation
wsl --export NixOS-Template nixos-backup.tar
# Create new instance
wsl --import NixOS-Dev C:\WSL\NixOS-Dev nixos-backup.tar
# Customize each instance separately
wsl -d NixOS-Dev
# Edit /etc/nixos/configuration.nix with different settings
Integration with IDEs
Visual Studio Code
-
Install WSL extension
-
Open project in WSL:
code . -
Configure VS Code settings for WSL development
JetBrains IDEs
- Use JetBrains Gateway for remote development
- Configure SSH connection to WSL2
- Set up project interpreter pointing to WSL2
Backup and Migration
Backup Configuration
# Backup system configuration
sudo cp -r /etc/nixos ~/nixos-backup
# Backup Home Manager configuration
cp -r ~/.config/home-manager ~/home-manager-backup
# Export WSL distribution
wsl --export NixOS-Template nixos-full-backup.tar
Migration
# On new system, import distribution
wsl --import NixOS-Template C:\WSL\NixOS-Template nixos-full-backup.tar
# Apply configurations
sudo nixos-rebuild switch
home-manager switch
Container Integration
Docker Desktop Integration
The template works with Docker Desktop for Windows:
# Docker commands work transparently
docker run hello-world
docker-compose up
Podman Alternative
Or use Podman for a fully Linux solution:
# Podman with similar Docker experience
podman run hello-world
podman-compose up
Networking
Port Forwarding
WSL2 handles port forwarding automatically, but for advanced cases:
# Manual port forwarding (if needed)
netsh interface portproxy add v4tov4 listenport=3000 listenaddress=0.0.0.0 connectport=3000 connectaddress=WSL_IP
Custom Networking
Configure custom networking in your NixOS configuration:
# Advanced network configuration
networking = {
firewall = {
enable = true;
allowedTCPPorts = [ 22 80 443 8080 ];
};
# Custom DNS
nameservers = [ "1.1.1.1" "8.8.8.8" ];
};
Resources
- NixOS Manual: https://nixos.org/manual/nixos/stable/
- NixOS-WSL Project: https://github.com/nix-community/NixOS-WSL
- Home Manager Manual: https://nix-community.github.io/home-manager/
- WSL Documentation: https://docs.microsoft.com/en-us/windows/wsl/
- Template Repository: https://github.com/yourusername/nixos-template
Contributing
To improve WSL2 support in this template:
- Fork the repository
- Make improvements to WSL2 modules or configuration
- Test thoroughly in WSL2 environment
- Submit a pull request with detailed description
Common areas for contribution:
- Performance optimizations
- Windows integration improvements
- Additional development tools
- Better GUI application support
- Documentation improvements