I’ve tried just about every type of setup I can find for a nix shell with python.
I don’t want to purely use nixpkgs for a lack of some packages and broken packages. I’m trying to use pyside6, but not everything in pyside6 is provided by the package, e.g. tools like uic.
Attempting to use a venv as normal leads to a disconnect between the env and system with libstdc++.so.6 unable to be found. There are a various different flakes I’ve tried to use like the-nix-way/dev-templates#python and others from forum discussions which add stdenv.cc.cc.lib to no avail.
I think the farthest I’ve gotten is with poetry/poetry2nix, where auto-patchelf warns about missing libQt6 libraries. Running with nix run fails to ‘find all the required dependencies’ even when adding qt6.qtbase or qt6.full to the packages. This is that flake, taken from the poetry2nix github with an added devshell:
{
description = "Python application packaged using poetry2nix";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
poetry2nix.url = "github:nix-community/poetry2nix";
};
outputs = { self, nixpkgs, poetry2nix }:
let
system = "x86_64-linux"; # Adjust for your system
pkgs = nixpkgs.legacyPackages.${system};
inherit (poetry2nix.lib.mkPoetry2Nix { inherit pkgs; }) mkPoetryApplication;
in {
packages.${system}.default = mkPoetryApplication {
projectDir = ./.;
};
apps.${system}.default = {
type = "app";
program = "${self.packages.${system}.default}/bin/app";
};
devShells.${system}.default = pkgs.mkShell {
packages = [ pkgs.poetry ];
buildInputs = [ pkgs.qt6.qtbase pkgs.qt6.full pkgs.qt6.wrapQtAppsHook ];
};
};
}
It seems kind of hopeless to get it working on NixOS. Does anyone have a working setup I could use for inspiration, or any other tips? I love the nix paradigm, but I’m honestly considering distrohopping with all of the trouble.
I’ve had the same problem running numpy. Shockingly with a library so popular I havent found a way to make an environment with it work. I also had the most success with poetry, so I think you’re on the right track.
Funny story, the full project uses pandas which pulls in numpy and I was running into issues there too. I think it was saying I was trying to import numpy from within it’s own source tree, which was weird.
Although I couldn’t get any venv approach working with it, having the pandas package in my flake allowed it to import.
There’s just such a mix of different issues with each approach that it’s so hard to navigate. I feel close with poetry2nix though, just gotta get it runnable lol
Pff, if pandas gets me numpy that works that may not be a bad hack. I’ll try this! Sorry I dont know how to fix qt!
From this page it seems you need libclang 13+, then you need to set the LLVM_INSTALL_DIR appropriately. There’s
llvmPackages_13.libclang
in nixpkgs, and some later versions too.Unfortunately this will probably not fix your missing qt lib issues. Dumb thing to try there, only have full, not both full and qtbase.
I only tried adding
qt6.full
because of the missing libs. Withoutqtbase
, though, the shell fails to build withwrapQtAppsHook qtHostPathHook: qtPluginPrefix is unset. hint: add qt6.qtbase to buildInputs
I would assume the full package would basically be like base plus more, but I don’t know lol
This doesn’t surprise me in the least. For the longest time the only way to update Python was to compile it from source… They just don’t care too much about making their tooling work nicely. And that’s before you even add the complexity of Nix.
I would maybe just not use Nix for this at all and try something like Rye, which is a third party attempt to fix the Python mess. It lets you specify a Python version and supports lock files so in theory everything is actually reproducible… so it’s at least part way to what you’d have with Nix.
Here’s a little repo I made with various ways of doing it. There’s poetry2nix and some minimal nix shell stuff around venv. I’m going to add distrobox as a fallback too. https://github.com/ghomasHudson/python_nix_dev_examples
Poetry2nix is your best bet imo, and read through this doc carefully https://github.com/nix-community/poetry2nix/blob/master/docs/edgecases.md. The option with
pypkgs-build-requirements
was how i managed to build a mid-sized django project successfully. For IDE i have a devshell set up that pulls in a configured nvim with a python LSP, formatter, debugger,… Works fine for small scripts but i would just use Pycharm in your devshell if you’re on a graphical machine and then also use nix to pull in python with packages. If you use venv i think you will need to also use https://github.com/Mic92/nix-ld