Managing python virtualenvs + jupyterlab "the Nix way"

I recently started using NixOS and am trying to learn as much as I can while slowly migrating my setup from my previous OS.

As part of my workflow (on my previous OS), I used to manage multiple python virtual environments, install them using ipykernel and access them on jupyterlab to work on different projects.

I am pretty confident I could stick to my “old” way of doing all of this on my new NixOS installation as well, namely:

  1. $ python3 -m venv my-venv && source my-venv/bin/activate
  2. (my-venv)$ pip install numpy ipykernel
  3. (my-venv)$ python3 -m ipykernel install --name my-venv --user

I could then access the new kernel (my-venv) in jupyterlab (installed system-wide). However, I would be interested in doing all of this “the Nix way”, to have a reproducible setup. Something like:

  1. Defining my virtual environments (and possibly their paths) in either configuration.nix or home.nix
  2. Declare the virtual environments as kernels so that they are available in jupyterlab

This way, all my virtual environments would be generated upon successful rebuild, even on a new machine.
I am confident my workflow is somewhat quite common among python developers. Do you have any recommendations, recipes, or tips on how to achieve this on NixOS?

uv2nix solved most of my python problems, however I did not test it with jupyter notebooks.

Any good place for getting started with uv2nix?

There is a demo on the Website. For special things like NVIDIA packages I am searching on GitHub.

1 Like