The bare minimum code needed to set up a Python environment using nix flakes.
In this setup, the Python packages are not installed using nix. Instead, nix is only used to build the desired Python version, then this Python version is used to create a virtual environment, and inside the virtual environment you are free to install whatever you whish using pip.
In this case, nix is effectively replacing pyenv, with the benefit that you could include non-Python packages (e.g.: jq) and pin their versions.
nix is capable of directly installing Python packages as well. However, at this point in my nix journey, I find the process too rigid for my taste.
nix develop
python -m venv .venv
. .venv/bin/activate
pip install -r requirements.txtThe first line spawns a nix shell:
nix developThe first time it takes a bit (~7min) to build. Bear in mind that this step is build your desired Python version from source. After the first run, the build is cached, so it takes ~1s to load.
At this point you are inside a nix shell, and the desired Python version should be available to you:
which python
# /nix/store/sxazgaidqcw9gzarla17qka6071vwgv0-python3-3.10.1-env/bin/python
python -V
# Python 3.10.1You could be now tempted to use pip to install Python packages - after all, we have Python installed, right? Not quite. Try to find the version of pip:
pip -VYou will either (a) get an error saying pip command is not found, or (b) you had pip installed system-wide (outside this nix flake, that is). Use which pip to confirm.
Regardless of you observing (a) or (b), if you haven't modified flake.nix, pip should not have been installed with the nix flake alongside the Python you chose. This is desired. Why? You won't be able to install packages as usual if you install pip using the nix flake. See Can I install pip via nix flake? for a more detailed explanation.
How can you install Python packages then? From within the nix shell, create a Python virtual environment, and follow your preferred way of installing dependencies inside this Python virtual environment:
python -m venv .venv
. .venv/bin/activate
pip install -r requirements.txtLet's give it a go and see what happens, shall we?
-
Make sure you are not in any previous nix shell.
-
Edit
flake.nixto includepipas a nix package of themyPythonnix package.pkgs = import nixpkgs { inherit system; }; myPython = nixpkgs-python.packages.${system}.${pythonVersion}; + myPythonWithPackages = myPython.withPackages (ps: with ps; [ + pip + ]); in { devShells.${system}.default = pkgs.mkShell { buildInputs = [ - myPython + myPythonWithPackages ]; shellHook = '' -
Rebuild and jump into the shell:
nix develop
-
Confirm
pipis installed via nix shell - must have the/nix/storeprefix below with a different hash:which pip # /nix/store/sxazgaidqcw9gzarla17qka6071vwgv0-python3-3.10.1-env/bin/pip -
Install any Python package using the
pipinstalled via nix flake:pip install ipdb # Collecting ipdb # (...) # ERROR: Could not install packages due to an OSError: [Errno 13] Permission denied: '/nix/store/sxazgaidqcw9gzarla17qka6071vwgv0-python3-3.10.1-env/lib/python3.10/site-packages/wcwidth' # Check the permissions.
Ta-da! 🎉 In the nix world, you are not supposed to use pip directly to install Python packages. Instead, you wrap those Python packages to convert nix packages, and you install those wrapped nix packages. See docs.
Thank you nixpkgs-python for making it so accessible.