Setting environment variables for pytest runs in buildPythonPackage

Basics:

I’m working on a package that uses buildPythonPackage, and I would like to set some environment variables during the pytest run in the pytestCheckPhase. Is there a way to do that? If so, how?

What I’ve tried so far:

Approach 1: set EV in preCheck

 preCheck = ''
    export TSS2_FAPICONF=${tpm2-tss.out}/etc/tpm2-tss/fapi-test-config.json
    echo "In preCheck, set TSS2_FAPICONF to: $TSS2_FAPICONF"
  '';

Approach 2: use pyenv and --override-ini to set environment variables

  pytestFlags = [
    "--override-ini env=\"TSS2_FAPICONF=${tpm2-tss.out}/etc/tpm2-tss/fapi-test-config.json\""
  ];

Context and Background:

The package in question is tpm2-pytss from nixpkgs. When attempting to learn a bit about TPMs, I found the UX of tpm2-tools to be quite rough. Investigating further, it seemed like the root cause is the way that a config file (fapi-config.json) needed by many parts of the tpm2 ecosystem was being treated in packaging.

At runtime, most parts of the ecosystem find the config file by using a location set at compile time, (defaulting to /etc/tpm2-tss/fapi-config.json), or taking an override of that location using an environment variable, TSS2_FAPICONF. The packaging was pointing the compile-time location to a location in the nix store as part of the tpm2-tss package, but that’s not a good place to put it, because the file is meant to be user-configurable, and there are plenty of reasons a user may want to change it after compiling tpm2-tss. Additionally, the content of that file was not really usable as it was set up by packaging, and there was no way in packaging to configure the values of it.

I changed the tpm2-tss packaging to:

  1. put a file suitable for test usage in the nix store alongside tpm2-tss
  2. added generation of a config file to the nixos module tpm2, which would be put in the nix store and then linked to from /etc/tpm2-tss/fapi-conf.json
  3. changed the default path of the file set at compile time to /etc/tpm2-tss/fapi-conf.json

This allows the FAPI config to be changed independently of the tpm2-tss build, and gives an ergonomic way to set it via nixos modules. The remaining problem is that a config suitable for actual usage is NOT suitable for test usage. So I need to configure the test suite to use a test config file, which as I mentioned earlier, I put in the nix store in tpm2-tss. The way to do this would be environment variables, but I can’t figure out how to set environment variables that affect the pytest run.

If you want all the gory details, here’s a draft PR: DRAFT: Fix tpm2 fapi by scottstephens · Pull Request #1 · scottstephens/nixpkgs · GitHub

What I’ve got there (patching the python code to find the test file) actually works for testing, but would cause problems with actual usage of tpm2-pytss. You can see methods I tried to set environment variables in the commented out pytestFlags and preCheck sections.

As it turns out, this was caused by the python code in the tests messing with the environment variables. I think my attempts actually did set the EVs, it’s just the pytss was undoing it.