I am trying to create a small dev environment for a project involving Python, using Nix shells on Ubuntu. For now, I want to rely on pip to manage my Python dependencies, not nixpkgs. The project has a few large dependencies, such as PySide2. I noticed that I should use Python virtual environments in Nix shells to avoid permission issues when installing packages, so this is what I do. My shell.nix:
I launch this shell with nix-shell --pure, which works, but when I run python -c "import PySide2", I am greeted with a segmentation fault. I am assuming this problem is not particularly related to PySide2, rather a more fundamental setup issue, but I don’t know. What are possible causes for this, and how would I troubleshooting these kinds of issues? Thanks!
I have a few python projects where other developers aren’t using nix, I typically handle them like the snippet below. But mine seem to have simpler requirements than yours.
pkgs.mkShell {
# The Nix packages provided in the environment
packages = [
pkgs.python311
pkgs.python311Packages.pip
# Whatever other packages are required
];
shellHook = ''
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
'';
};
I am basically doing the same thing, but PySide2 won’t work out of the box after pip installing it.
After some digging, I managed to satisfy all the system library dependencies that PySide2 has, at least to an extent where my application runs (note this includes some more than what’s strictly necessary):
For other people on a similar mission, there are wrappers for PySide2 and PySide6 in nixpkgs which will also ensure dependencies are satisfied, so you can do something like this (notice --system-site-packages when creating the virtual environment):
but I was looking for a more generic solution. I spend enough time debugging the PySide2 Python package that I don’t feel like adding another wrapper that can fail.