Skip to content

python3Packages.virtualenv: make seeded pip prefer the system CA bundle - #576573

Open
zimbatm wants to merge 1 commit into
NixOS:stagingfrom
zimbatm:cacert-virtualenv
Open

zimbatm wants to merge 1 commit into
NixOS:stagingfrom
zimbatm:cacert-virtualenv

Conversation

@zimbatm

@zimbatm zimbatm commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

virtualenv ships a pip wheel in virtualenv/seed/wheels/embed and installs it into every venv it makes. That pip adds the cacert.pem of its vendored certifi to the trust store next to the system store. Thus a cacert update alone does not remove a distrusted root (for example emSign ECC Root CA - G3) from pip in such a venv.

Rewrite the embedded pip wheel with the same script and override as the cpython ensurepip wheel. The vendored certifi then returns NIX_SSL_CERT_FILE, SSL_CERT_FILE or /etc/ssl/certs/ca-certificates.crt when one exists. The vendored bundle stays as a fallback.

The two shared files are byte-identical to the ones from the cpython and python3Packages.pip changes, so the branches merge cleanly.

Tested on x86_64-linux in a venv made by the patched virtualenv (pip 26.1.2): certifi.where() returns /etc/ssl/certs/ca-certificates.crt; pip download six fails with CERTIFICATE_VERIFY_FAILED when SSL_CERT_FILE holds an unrelated self-signed cert, and works with the system bundle. The unpatched wheel downloads even with the bad cert. 237 tests pass. pip-certifi-override.py and ensurepip-certifi.py are byte-identical to #576524 and #576525, so the PRs merge without conflict.

Note: virtualenv on staging already fails to build without this change (filelock<4,>=3.24.2 not satisfied by version 4.1.2); with pythonRelaxDeps = [ "filelock" ] it builds and the tests pass. That needs a separate fix. Also, virtualenv's periodic update can download a newer, unpatched pip into its app-data; this change covers only the embedded wheels.

Related to: #576485
Related to: #576542

Assisted-by: Claude Code (claude-opus-5)

Things done

virtualenv ships a pip wheel in virtualenv/seed/wheels/embed and
installs it into every venv it makes. That pip adds the cacert.pem of
its vendored certifi to the trust store next to the system store. Thus
a cacert update alone does not remove a distrusted root (for example
emSign ECC Root CA - G3) from pip in such a venv.

Rewrite the embedded pip wheel with the same script and override as the
cpython ensurepip wheel. The vendored certifi then returns
NIX_SSL_CERT_FILE, SSL_CERT_FILE or /etc/ssl/certs/ca-certificates.crt
when one exists. The vendored bundle stays as a fallback.

The two shared files are byte-identical to the ones from the cpython
and python3Packages.pip changes, so the branches merge cleanly.

Part of NixOS#576542.

Assisted-by: Claude Code (claude-opus-5)
@nixpkgs-ci
nixpkgs-ci Bot requested a review from mweinelt October 20, 2026 22:30
@nixpkgs-ci nixpkgs-ci Bot added 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. 10.rebuild-darwin: 2501-5000 This PR causes many rebuilds on Darwin and should target the staging branches. llm-assisted Contributions that include code generation with LLMs 6.topic: python Python is a high-level, general-purpose programming language. labels Oct 20, 2026
@zimbatm zimbatm added the backport staging-26.05 Backport PR automatically label Oct 20, 2026

@mBornand mBornand left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Things that I think should be changed

Some additional thoughts:

  • In addition we could also remove the cacert.pem file completely.
  • At some point we are very close to replacing the whole file. (There is also a exit_cacert_ctx function, that might need to be created too)

This review applies also to #576525 #576524

I don't think I am a reference for this but wouldn't the review be easier if it where one PR instead of three?



def where() -> str:
return _nix_system_bundle() or _nix_vendored_where()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to fallback to the vendored certs? We could also fallback to the the cacert package.

@nixpkgs-ci nixpkgs-ci Bot added the 2.status: merge conflict This PR has merge conflicts with the target branch label Oct 23, 2026
hash = "sha256-8LOmA1Mhfqbl3hsGZa8tQutjfjEVeDlpOKyVut5rDVI=";
};

# Make the pip seeded into each venv prefer the system CA bundle over its vendored certifi.

@dotlambda dotlambda Oct 28, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we apply our certifi patch to the vendored certifi?

postPatch = ''
for whl in src/virtualenv/seed/wheels/embed/pip-*.whl; do
old=$(sha256sum "$whl" | cut -d' ' -f1)
${python.pythonOnBuildForHost.interpreter} ${../../interpreters/python/cpython/ensurepip-certifi.py} \

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this file not in .?

@dotlambda

Copy link
Copy Markdown
Member

Note: virtualenv on staging already fails to build without this change (filelock<4,>=3.24.2 not satisfied by version 4.1.2); with pythonRelaxDeps = [ "filelock" ] it builds and the tests pass. That needs a separate fix.

fixed by #577206

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: merge conflict This PR has merge conflicts with the target branch 6.topic: python Python is a high-level, general-purpose programming language. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 2501-5000 This PR causes many rebuilds on Darwin and should target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. backport staging-26.05 Backport PR automatically llm-assisted Contributions that include code generation with LLMs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants