Embedding nix environments in jupyter notebooks: nb_nix_env

Hey all. Had a fun quick project that I threw together that allow the idea of embedding a nix environment within a jupyter notebook. I was inspired by nix shebangs where you would specify a “virtual environment” for your script within the script.

Here is the repository: https://github.com/costrouc/nb_nix_env

This allows you to specify multiple nix environments for a single jupyter notebook and switching between them. Currently in a quite hackish state with a cell of javascript needing to be executed but a widget or jupyterlab extension would make this work quite seamlessly. The trick is that it base64 encodes a nix-shell expression as the kernel name. There is a custom kernel spec manager that knows how to launch the base64 encoded commands. It should work well with conda envs as well.

2 Likes