What best practices have you found using Nix in Python projects?

I’d love to gather brief advice on best practices you have learned when using Nix to manage Python projects.

my background to this question

I have very little Python experience but love the community and its creations.

I’m currently working on a mkdocs technical documentation project.
I’m using flakes. It has been going quite well but I’ve run into trouble when adding an extension.
I’ll save those technical weeds and struggle for another post.

some example questions

A few of these have floated through my mind.

  • how do you mix declarative management with nix and imperative with pip?
  • should I use pip? poetry? pipenv?
  • what is your experience using mach-nix? It looks great. Though I don’t understand how to point to a pypi package that isn’t in mach-nix’s linked database.

Thank you for reading and considering!

4 Likes

Just checking in to +1 to this question, as someone who gave up Nix on this regard. Maybe something new is on the block. :eyes:

Late edit: I am not blaming nix in any way. Python packaging is completely FUBAR, just waiting for a magical solution like the idea of nix itself maybe…

2 Likes

It took a little massaging, but I was able to make this work with similar requirements. I can’t share all of the code, but at a high level, our python.nix does a few things:

  1. pull in all of the requirements files as their own source, like req_source = inputs.pkgs.lib.sourceByRegex ../. [ "requirements_.*\.txt$" ];
  2. write a ~filter (just a bash function + runCommand) to filter out lines that mach-nix can’t cope with (and recursively expand -r requirements_blah.txt lines, but you may not need this)
  3. build the private dependencies with Nix and let mach-nix know about them via overridesPre

Took a little fiddling, but pretty happy with how this works. Before that I used pypi2nix or something for my dev environment, but it felt way too fragile to share w/ anyone else on the project.

Some occasional issues with trying to find good versions of nixpkgs/mach-nix to jump up to during update cycles, but I imagine that’ll smooth out over time.

4 Likes