Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

WSL

Platforms: NixOS only.

A host that runs as a NixOS-WSL distro on Windows. WSL starts the distro inside a kernel Windows owns, and hands it its disk, its network and its resolver. NixOS-WSL turns off the kernel, the initrd and the bootloader to match.

Importing

The adapter is the separate export nixosModules.wsl, which neither nixosModules.default nor nixosModules.full imports. It is the only othrys module that writes into the wsl option namespace, so a WSL host imports the export together with the NixOS-WSL module:

# flake inputs
nixos-wsl.follows = "othrys/nixos-wsl";

# host modules
inputs.othrys.nixosModules.wsl
inputs.nixos-wsl.nixosModules.default

Options

  options.othrys.system.wsl = {
    enable = lib.mkEnableOption "running the host as a NixOS-WSL distro";

    defaultUser = lib.mkOption {
      type = lib.types.str;
      default = config.othrys.system.user.name;
      defaultText = lib.literalExpression "config.othrys.system.user.name";
      description = "The account WSL logs in as when the distro starts, written to `wsl.defaultUser`.";
    };

    windowsPath = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = ''
        Append the Windows PATH to the PATH of the distro. Off by default, so
        a name such as `git` or `node` resolves to the Nix package and not to
        a Windows binary of the same name. Interop stays on either way, and a
        Windows binary still runs by its full path, as in
        `/mnt/c/Windows/explorer.exe`.
      '';
    };

    usbip = {
      enable = lib.mkEnableOption "attaching Windows USB devices through usbipd-win";

      autoAttach = lib.mkOption {
        type = lib.types.listOf lib.types.str;
        default = [];
        example = ["4-1"];
        description = ''
          Bus IDs to attach at boot, as `usbipd list` prints them on Windows.
          Each device has to be bound once on Windows with
          `usbipd bind --busid <id>`.
        '';
      };

      hostAddress = lib.mkOption {
        type = lib.types.nullOr lib.types.str;
        default = null;
        example = "127.0.0.1";
        description = ''
          The address of the Windows host that runs usbipd-win. null keeps the
          NixOS-WSL default, which reads the gateway of `eth0`. Under
          `networkingMode=mirrored` the distro has no such route, so a
          mirrored host sets `127.0.0.1`.
        '';
      };
    };

    startMenuLaunchers = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = "Add a Windows Start menu shortcut for each GUI application the host installs.";
    };
  };

Usage

othrys.system.wsl = {
  enable = true;
  usbip = {
    enable = true;
    autoAttach = ["4-1"];
    hostAddress = "127.0.0.1";
  };
};

WSL logs in as othrys.system.user.name unless defaultUser names another account.

The Windows PATH stays out of the shell by default, so a name such as git resolves to the Nix package. Interop stays on, and a Windows binary runs by its full path under /mnt/c.

A USB device such as a YubiKey reaches the distro through usbipd-win. Each device is bound once on Windows with usbipd bind --busid <id>, and its bus ID goes in usbip.autoAttach. NixOS-WSL finds the Windows host from the gateway of eth0, which a distro under networkingMode=mirrored does not have, so a mirrored host sets usbip.hostAddress = "127.0.0.1".

Five othrys modules configure what WSL owns, and a WSL host that enables one is refused at evaluation. They are othrys.system.bootloader, othrys.system.disko, othrys.system.impermanence, othrys.system.kernel and othrys.system.networking.

Installing

NixOS-WSL builds an importable tarball from the host’s own configuration. On a Linux machine with the consuming flake:

sudo nix run .#nixosConfigurations.<host>.config.system.build.tarballBuilder

The builder writes nixos.wsl to the working directory. On Windows, the tarball is imported with wsl --import <host> <directory> nixos.wsl, or opened directly where the installed WSL supports it.