pythonvisual-studio-codewindows-subsystem-for-linuxnixnix-flake

How to select a NixOS WSL Python interpreter(from a virtual environment) in Visual Studio Code on Windows


I'm using NixOS on WSL2 and Visual Studio Code on Windows with the WSL extension. I've created a virtual environment in NixOS, and I want to use the python command of the virtual environment as the Python interpreter in VS Code. However, I'm encountering issues selecting the correct interpreter.

My setup:

NixOS installed on WSL2

Visual Studio Code on Windows with the WSL extension (to access all file in WSL)

Python virtual environment created in NixOS (see flake at the end of the question)

When I run which python inside my virtual environment in NixOS, I get:

/nix/store/24w9ckmkq0asaif83c13z53yraq584lm-python3-3.14.0a4/bin/python

I've tried to select this path as my interpreter in VS Code by adapting it to the Windows WSL path format:

\wsl.localhost\NixOS\nix\store\24w9ckmkq0asaif83c13z53yraq584lm-python3-3.14.0a4\bin\python

However, VS Code only allows me to choose executable (.exe) files. This makes sense, as the Linux executable can't be run directly from Windows. Is it possible to select this NixOS WSL Python interpreter for use in VS Code on Windows? If so, how can I do it? I understand that a Linux executable can't be run directly from Windows, but I'm hoping there's a way to configure VS Code to use the WSL Python interpreter seamlessly. Any guidance on how to properly set this up would be greatly appreciated.

My flake file bring nothing to understand the problem. It is to make the problem reproductible:

{
  description = "Python and Manim development environment";

  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
  };

  outputs = { self, nixpkgs, flake-utils }@inputs:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = nixpkgs.legacyPackages.${system};
      in {
        devShells.default = pkgs.mkShell {
          buildInputs = with pkgs; [
            python314
            manim
          ];
          
          shellHook = ''
            echo "Development environment for Python and Manim"
            echo "Run 'python' to start Python interpreter"
            echo "Run 'manim' to start Manim"
          '';
        };
      }
    );
}

Solution

  • After trying to launch vscode from WSL, I got this message.

    To use Visual Studio Code with the Windows Subsystem for Linux, please install Visual Studio Code in Windows and uninstall the Linux version in WSL. You can then use the code command in a WSL terminal just as you would in a normal command prompt. Do you want to continue anyway? [y/N] y

    First I did as they said. But I couldn't choose an interpreter who wasn't an .exe file

    There I decide to keep vscode installed on WSL

    How my /etc/nixos/configuration.nix looks like:

    { config, lib, pkgs, ... }:
    
    {
      imports = [
        # include NixOS-WSL modules
        <nixos-wsl/modules>
      ];
    
      wsl.enable = true;
      wsl.defaultUser = "nixos";
    
      nixpkgs.config.allowUnfree = true;
    
      system.stateVersion = "24.05";
       environment.systemPackages = [
        pkgs.vscode
      ];
      nix.settings.experimental-features = [ "nix-command" "flakes" ];
    
    }
    

    sudo nixos-rebuild switch works fine after that.

    I tried to load the python interpreter of my virtual environment. I works fine, I didn't had to looks for the interpreter path. It was already proposed.

    If this is not the case. You have to write which python in your virtual environment and you get the path