Posted on

Table of Contents

Get WSL

Firstly, install WSL on Windows using the following command:

wsl --install --no-distribution

After installation, a system restart might be required to apply the changes effectively.

Get NixOS For WSL

Download the latest version of NixOS from the Github Page .

Import NixOS into WSL with:

wsl --import NixOS .\NixOS\ nixos-wsl.tar.gz --version 2

The system will be install in .\NixOS

Enter WSL using:

wsl -d NixOS

Upon entry, you should be greeted with the familiar bash prompt. Initially, update the system with:

After first install we need to update the system:

sudo nix-channel --update
nixos-rebuild

Install Flake

Set the default Nix channel to nixpkgs-unstable and begin by enabling flakes in /etc/nixos/configuration.nix:

  nix.settings.experimental-features=["nix-command" "flakes"];

Initialize a templates flake config with:

nix flake init -t templates#full

First, download a flake templates. Modify the templates a little bit to accept our old config.

{
  description = "A template that shows all standard flake outputs";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
  };

  # Work-in-progress: refer to parent/sibling flakes in the same repository
  # inputs.c-hello.url = "path:../c-hello";

  outputs = { self, nixpkgs, ... }@inputs: {
    # Used with `nixos-rebuild --flake .#<hostname>`
    # nixosConfigurations."<hostname>".config.system.build.toplevel must be a derivation
    nixosConfigurations.wsl = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [./configuration.nix] ;
    };
  };
}

Add Home-Manager:

{config,pkgs,...}:

{
  home.username = "nixos";
  home.homeDirectory = "/home/nixos";
  home.packages = with pkgs; [
  p7zip
  neofetch
  ];
  home.stateVersion = "23.11";
  programs.home-manager.enable = true;
}

Then add these configs to flake:

{
  description = "A template that shows all standard flake outputs";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
    home-manager = {
      url="github:nix-community/home-manager/release-23.11";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  # Work-in-progress: refer to parent/sibling flakes in the same repository
  # inputs.c-hello.url = "path:../c-hello";

  outputs = inputs@{ nixpkgs,home-manager, ... }: {
    # Used with `nixos-rebuild --flake .#<hostname>`
    # nixosConfigurations."<hostname>".config.system.build.toplevel must be a derivation
    nixosConfigurations.nixos = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        ./configuration.nix
        home-manager.nixosModules.home-manager
        {
          home-manager.useGlobalPkgs = true;
          home-manager.useUserPackages = true;
          home-manager.users.nixos = import ./home.nix;
        }
      ];
    };
  };
}

Now we are good to go.