NixOS: Installing Python packages with optional dependencies

I’m trying to figure out how to install black with the daemon option in NixOS, and looking at the package definition, it looks like it needs an optional dependency to work.

passthru.optional-dependencies = {
  d = [ aiohttp aiohttp-cors ];
  colorama = [ colorama ];
  uvloop = [ uvloop ];

Looking at the documentation in the Nixpkgs manual in § Optional extra dependencies (can’t link directly), it’s not clear how to access these through something like environment.systemPackages or home.packages.

Just adding black to those lists does install blackd but when I try to run it, it raises an ImportError exception because aiohttp isn’t present. I also tried to add that one separately, but it doesn’t seem to find it (the same error happens).

This is the first time I’ve run into this feature (didn’t realize it was Python specific at first), and I’ve gotten pretty good about figuring out how things work, but this doesn’t seem to be clearly documented, or I’m missing something obvious.

Thanks in advance.

The optional-dependencies attribute is just a mapping with derivations. You can do e.g.

myenv = python3.withPackages(ps: with ps; [ mypkg ] ++ mypkg.optional-dependencies.d )

to get your mypkg and the list of optional dependencies that have as key d.

Python libraries should always be used with withPackages/buildEnv for creating a runtime environment. The resulting derivation can be added to e.g. environment.systemPackages or home.packages.


Ah, that makes sense. I probably should be installing things like black and flake8 in project specific environments, but I figured making them global wouldn’t make that big a difference. I didn’t consider I might need to do it this way.

Thanks again!