TLDR
-
Do you have any experience with using
PyO3
(and mabyematurin
) in Nix-controlled projects? -
Would you recommend using
maturin
or avoiding it, in a Nix context? -
Do you have any words of wisdom about using
PyO3
to call Python from Rust, in Nix-controlled projects?
Background ramblings
PyO3
is a tool for creating bindings between Rust and Python. For quite some time, I have had an ad-hoc flake which provides the Rust toolchain and a Python interpreter with whatever packages I require, and allows me to use PyO3
to write a Python module in Rust and import it in Python. It’s a bit hacky, but it solved my short-term problems.
I now need to do the reverse: call Python from Rust. PyO3
has this covered, but in practice my initial attempts run into link-time errors.
Looking at the PyO3
docs, I see that, nowadays, PyO3
recommends maturin
as the easiest way to get started with PyO3
, and I’m wondering whether using maturn
would be better than trying to lay everything out myself. What discourages me is that maturin
uses Python’s virtualenv
s, and the docs suggest all sorts of state-modifying CLI incantations, which make my Nix-attuned mind tremble with fear.
If my dependencies are fully controlled by Nix, I am confident that checking out an arbitrary commit will result in direnv
automatically updating the versions of all dependencies to the ones with which that commit was tested. Admittedly, I allow (and trust) cargo
to take care of this for the Rust dependencies. For a few years now, I’ve been using Nix to protect me from the nightmare that is Python packaging, so I have a (perhaps irrational) distrust of the Python tools. I certainly don’t trust the Python tools to do the right thing, to the extent that I trust cargo
.
I was first made aware of maturin
when I tried to install polars
in Nix, and was confronted with a maturin failed
error message. Progress on the polars
package in nixpkgs
has been limited to it getting marked as broken, and the experience has tainted maturin
(perhaps unfairly) as guilty, by association.