Nix shell with Python packages through pip

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):

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell rec {
    buildInputs = with pkgs; [
        # python
        python310
        python310Packages.pip
        python310Packages.virtualenvwrapper

        # pyside2 dependencies
        qt5.full stdenv.cc.cc.lib
        glibc glib libGL zlib bzip2 openssl libpng libjpeg
        ffmpeg libxkbcommon fontconfig freetype zstd dbus
        xorg.libXrender xorg.libxcb xorg.libX11 xorg.libXext 
        xorg.libXcursor xorg.xhost
    ];

    shellHook = ''
        export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${pkgs.lib.makeLibraryPath buildInputs}"
        export TMPDIR=/tmp  && export VENV=$(mktemp -d)
        virtualenv $VENV
        source $VENV/bin/activate
        pip install pyside2
    '';
}

I still don’t know what caused the segfault.

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):

{ pkgs ? import <nixpkgs> {} }:

pkgs.mkShell rec {
    buildInputs = with pkgs; [
        python310
        python310Packages.pip
        python310Packages.virtualenvwrapper
        python310Packages.pyside6
    ];

    shellHook = ''
        export TMPDIR=/tmp  && export VENV=$(mktemp -d)
        virtualenv --system-site-packages $VENV
        source $VENV/bin/activate
    '';
}

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.