Skip to content

Instantly share code, notes, and snippets.

@bew
Last active November 8, 2020 20:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bew/b0cd44d1d202cecc57d614a4acff71e9 to your computer and use it in GitHub Desktop.
Save bew/b0cd44d1d202cecc57d614a4acff71e9 to your computer and use it in GitHub Desktop.
WSL2 - Basic but working python (with pip) managed by Nix

WSL2 - Basic but working python (with pip) managed by the best Nix package manager

Setup WSL2 using Debian to be able to install Nix

  • On WSL2, install the Debian distribution from the microsoft store, then open wsl.exe in a terminal. I recommand using the Microsoft Terminal (this feels weird to say, since I'm more a linux user than a windows user, but when you NEED to use windows....)

    It should open the Debian distrib, with a small bootstrapping script to create a user.

  • Now update the apt package repositories with sudo apt update

  • Install curl and xz-utils, necessary to download and run the Nix installer: sudo apt install curl xz-utils

Install the Nix package manager

  • Install the latest Nix package manager: curl -L https://nixos.org/nix/install | sh

    (More info about Nix at https://nixos.org/)

    This will make a user install of Nix on the system (in the Debian distribution in WSL)

  • Edit your ~/.bashrc to add the line mentioned at the end of the Nix installation, used to load Nix into the shell on start.

    The line for me was: . /home/bew/.nix-profile/etc/profile.d/nix.sh

  • Restart your shell using exec bash or simply run the above line to load Nix into the running shell.

Verify Nix setup

  • You can verify that Nix is properly setup by installing and running neofetch using:

    nix run nixpkgs.neofetch -c neofetch

    This shows the power of Nix, it will download and run neofetch, but not install it. The next time the Nix's garbage collector will run (can be triggered manually with nix-collect-garbage), the files downloaded for neofetch will be deleted!

    If everything goes well you'll see some basic system info about your setup, like this:

           _,met$$$$$gg.          bew@myhostname
        ,g$$$$$$$$$$$$$$$P.       ---------------
      ,g$$P"     """Y$$.".        OS: Debian GNU/Linux 10 (buster) on Windows 10 x86_64
     ,$$P'              `$$$.     Kernel: 4.4.0-18362-Microsoft
    ',$$P       ,ggs.     `$$b:   Uptime: 3 mins
    `d$$'     ,$P"'   .    $$$    Packages: 175 (dpkg), 107 (nix-user)
     $$P      d$'     ,    $$P    Shell: bash 4.4.23
     $$:      $$.   -    ,d$$'    Terminal: Windows Terminal
     $$;      Y$b._   _,d$P'      CPU: Intel i5-6300HQ (4) @ 2.301GHz
     Y$$.    `.`"Y$$$$P"'         Memory: 7419MiB / 16258MiB
     `$$b      "-.__
      `Y$$
       `Y$$.
         `$$b.
           `Y$$b.
              `"Y$b._
                  `"""
    

Setup python with pip in a python virtual env

Now the interesting part! Let's use Nix to create a python virtual env where pip is available, and start hacking stuff!

  • Using this shell alias:

    alias py='nix run "(import <nixpkgs> {}).python3.withPackages (pyPkgs: [pyPkgs.pip])" -c python3'

    We get a py alias that will run python from a Nix environment that has the pip package installed.

    This is important because it means that when we create a virtual env with this python, pip will be available!

    NOTE that this Nix expression is installed in an ephemeral way, running nix-garbage-collect would remove important files. For pip this is not a problem (see the next step) but for the python interpreter this can be a problem if no other package uses it, and it gets deleted.. You'll need to make sure python is installed permanently (TODO: explain how to!)

  • Let's do that, create a python virtual env (using the builtin venv module of python), in the directory ./venv/:

    py -m venv -- ./venv

    NOTE that at this point, pip's source code has been copied to the new virtual env, meaning that if the Nix env we used to create this virtual env disappears, pip will still be usable from the virtual env!

  • Now activate this python environment in the current shell with:

    source ./venv/bin/activate

    If we check the path of python or pip, we can see that they come from the virtual environment:

    which python
      /path/to/venv/bin/python
    
    which pip
      /path/to/venv/bin/pip
  • When the python environment is active, we can install python packages in this environment!

    pip install requests
    # write some python
    # execute and enjoy!

Obligatory notes

NOTE 1: This is not the Nix recommended way to manage python packages (see https://nixos.org/manual/nixpkgs/stable/#python) but for my case, which is to use Nix to manage the python interpreter and pip, and handle python dependencies myself, this is enough!

NOTE 2: This setup is NOT reproducible, since we use the currently installed nixpkgs version using (import <nixpkgs> {}). To have a truely reproducible setup of the interpreter, we would need to pin the nixpkgs version to a specific version but this requires more Nix knowledge that I won't explain here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment