Python: propagating environmental variables to the python environment

Hi all, I’m hoping someone with knowledge of python packaging in nix can help me.

I’m building a package with buildPythonPackage but its consumers need to have an environmental variable set in order to work. I tried putting this into the package’s setupHook but that doesn’t seem to be used by subsequent derivations like python3.withPackage. Is there a way to do this without requiring users of the package to set it manually?


1 Like

This seems like a pretty ride requirement for a library in my opinion and probably hard to deal with.

Would patching the library and adding something like this at the top be an option?

import os
os.environ.setdefault("ENV_VAR", "a sensible default value")

(Might need tweaks, I’m on my phone)


Hmm yes I suppose that’d do the trick. Thanks, that’s probably the neatest solution.

I wondered if there was a more Nixy way to propagate environment variables, but I guess built python packages aren’t treated like normal derivations.