← Últimos Posts del Blog

🎵 Podcast en Spotify

When considering the Windows Subsystem for Linux (WSL), most developers tend to opt for distributions like Ubuntu or Fedora, given their near-zero learning curve and extensive documentation and community support. However, for those seeking a more robust, reproducible, and declarative work environment, NixOS emerges as a fascinating alternative on WSL. It transforms WSL into an environment that can be rebuilt, cloned, and rolled back with a single command, maintaining the same GUI, GPU, and USB functionality that Ubuntu and Fedora offer in modern WSL versions.

NixOS's most distinctive feature is its declarative approach. The entire configuration of a NixOS installation is specified in a single text file, typically configuration.nix or a flake.nix that imports it. By running nixos-rebuild switch, the operating system is built directly from the contents of that file. This eliminates reliance on interactive history or partially remembered package commands, ensuring bit-for-bit reproducibility. A setup can be reinstalled on another PC or in Continuous Integration (CI) environments and result in an identical configuration. Furthermore, Git history can record the addition, author, and reason for each package. This same approach can be applied to different platforms, such as WSL, a bare-metal server, a cloud VM, or macOS via nix-darwin. A crucial differentiator is that you can copy the configuration.nix to any NixOS machine and replicate the exact system with a single rebuild command, synchronizing multiple computers.

Another significant technical benefit is the process of atomic updates. When you adjust the configuration and run nixos-rebuild switch, Nix builds the new system state in the background. The transition to the new state occurs only after everything is ready, preventing partial updates that could leave the setup in a broken state. If an update introduces a bug, such as a driver failure, you can simply reboot into a previous "generation" to fix the issue. Even on WSL, NixOS maintains a complete history of system states, versioning each rebuild, which allows for easy rollbacks without debugging or reinstallation – something not natively offered by apt or dnf in typical WSL distributions.

Development environment management is dramatically enhanced with Nix's per-project development shells. For developers working on multiple projects with conflicting dependencies (e.g., different Node versions or Python libraries), Nix eliminates the need for Docker containers or polluting the base system. The nix develop tool allows you to create an environment with exactly the tools needed for that specific project, and nothing persists after exiting the shell. This represents a significant shift from the traditional workflow in Ubuntu or Fedora, where packages would be installed and uninstalled, or tools like virtualenv and nvm used to simulate isolation.

NixOS's integration with WSL is exceptionally seamless. It works perfectly with all key WSL2 functionalities. The latest version of NixOS-WSL even includes native systemd support built into WSL, allowing Linux services to run in the background without needing hacks. Previously, the absence of PID 1 and systemd in WSL was a significant limitation. Now, a NixOS WSL instance boots with systemd like a regular Linux system, enabling features like timers and socket-activated services. Hardware and UI integration is also perfect; NixOS WSL offers an option to use the Windows host's OpenGL graphics driver for GPU acceleration in Linux applications. Furthermore, accessing NixOS files from Windows applications is straightforward, via the \\wsl.localhost\NixOS path.

This synergy between NixOS and WSL is particularly powerful for unifying development environments across diverse platforms. It's possible to run NixOS on a headless Linux server for deployments, and also use Nix via Home Manager and nix-darwin on a Mac. By employing NixOS on WSL on a Windows machine, your development configurations across Windows, Linux servers, and a macOS laptop can remain synchronized with the exact same software versions.

In summary, NixOS on WSL offers unprecedented control over the development environment, transforming it into something fully reproducible, configurable via a single declarative file, and resilient to update failures due to its atomic and rollback capabilities. For developers seeking a setup that can be easily replicated, audited via version control, and kept consistent across different machines and projects, NixOS on WSL is a powerful tool and a step ahead of traditional approaches. It offers a solid foundation for building a more efficient and fault-tolerant workflow.