Skip to content

nixos/ca: set {NIX_,}SSL_CERT_FILE - #578494

Open
MarcelCoding wants to merge 1 commit into
NixOS:staging-nixosfrom
MarcelCoding:certifi-use-system-ca
Open

MarcelCoding wants to merge 1 commit into
NixOS:staging-nixosfrom
MarcelCoding:certifi-use-system-ca

Conversation

@MarcelCoding

@MarcelCoding MarcelCoding commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

Right now, nss-cacert updates need to go though staging and changes to security.pki.caCertificateBlacklist (like in #576390 or like custom user preferences) are not directly picked up by applications using the cacert package.

This environment variable is patched-in in packages like pythons certifi to override the ca bundle at runtime.

Without this change certifi still uses an old version of the ca bundle:

❯ nix-shell -p python3Packages.certifi --command 'diff -u $(python -m certifi) /etc/ssl/certs/ca-bundle.crt'
--- /nix/store/6x3vic5vm4jzp29yna1qkp0xmf3qm35i-nss-cacert-3.126/etc/ssl/certs/ca-bundle.crt    1970-01-01 01:00:01.000000000 +0100
+++ /etc/ssl/certs/ca-bundle.crt        1970-01-01 01:00:01.000000000 +0100
@@ -1758,8 +1758,15 @@

 Comodo AAA Services root
 Traditional PEM block omitted: this certificate is not trusted for authenticating servers.
-Trusted for:
+Rejected for:
+  - 1.3.6.1.5.5.7.3.1 (RFC5280: serverAuth key usage)
+  - 1.3.6.1.5.5.7.3.2 (RFC5280: clientAuth key usage)
+  - 1.3.6.1.5.5.7.3.3 (RFC5280: codeSigning key usage)
   - 1.3.6.1.5.5.7.3.4 (RFC5280: emailProtection key usage)
+  - 1.3.6.1.5.5.7.3.8 (RFC5280: timeStamping key usage)
+  - 1.3.6.1.5.5.7.3.5 (RFC2459: ipsecEndSystem key usage)
+  - 1.3.6.1.5.5.7.3.6 (RFC2459: ipsecTunnel key usage)
+  - 1.3.6.1.5.5.7.3.7 (RFC2459: ipsecUser key usage)

<and some more>

With this change certifi uses the same bundle as the module system
configures:

❯ nix-shell -p python3Packages.certifi --command 'diff -u $(NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-bundle.crt python -m certifi) /etc/ssl/certs/ca-bundle.crt' <no diff>

Things done

@MarcelCoding MarcelCoding added 1.severity: security Issues which raise a security issue, or PRs that fix one backport release-26.05 Backport PR automatically labels Oct 24, 2026
nixpkgs-branch-check[bot]

This comment was marked as outdated.

@nixpkgs-ci nixpkgs-ci Bot added 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 9.needs: reviewer This PR currently has no reviewers requested and needs attention. 10.rebuild-nixos-tests This PR causes rebuilds for all NixOS tests and should normally target the staging branches. 7.no default reviewers This PR targets a package or module with no existing maintainers; roving reviewers, take pity! 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: module (update) This PR changes an existing module in `nixos/` labels Oct 24, 2026
@MarcelCoding
MarcelCoding changed the base branch from master to staging-nixos October 24, 2026 20:53
@nixpkgs-ci nixpkgs-ci Bot closed this Oct 24, 2026
@nixpkgs-ci nixpkgs-ci Bot reopened this Oct 24, 2026
@nixpkgs-branch-check
nixpkgs-branch-check Bot dismissed their stale review October 24, 2026 21:02

Review dismissed automatically

@MarcelCoding MarcelCoding added backport staging-nixos-26.05 Backport PR automatically and removed backport release-26.05 Backport PR automatically labels Oct 26, 2026
Comment thread nixos/modules/security/ca.nix Resolved

@dotlambda dotlambda left a comment

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.

Should we also set $SSL_CERT_FILE?

@nixpkgs-ci nixpkgs-ci Bot removed the 9.needs: reviewer This PR currently has no reviewers requested and needs attention. label Nov 10, 2026
@SuperSandro2000

Copy link
Copy Markdown
Member

That's a good question. Possibly yes, but I have tested NIX_SSL_CERT_FILE myself for a longer time and didn't discover any incompatiblies, so it would be the safe bet.

@booxter

booxter commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

To add some color: I was looking at a similar change in nix-darwin: nix-darwin/nix-darwin#1881 and there's a similar Darwin specific change in home-manager in review: nix-community/home-manager#10112

In my change, I'm trying to retain a user setting if it's explicitly set. Not sure if it applies here, but something to consider.

@khaneliman with this PR in mind, does Home Manager need to adjust its systemd integration too?

@Skyb0rg007 Skyb0rg007 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.

Could you explain a bit more why we shouldn't instead patch each app to query the more standard SSL_CERT_FILE environment variable? Ie. why does this need to be nix-specific.

Comment thread nixos/modules/security/ca.nix Resolved
@MarcelCoding

MarcelCoding commented Nov 11, 2026 •

Copy link
Copy Markdown
Member Author

Could you explain a bit more why we shouldnn't instead patch each app to query the more standard SSL_CERT_FILE environment variable? Ie. why does this need to be nix-specific.

It was more like a solution for the current state. I think yes, we probable should define some kid of standard, so its not only compatible with nixos but also with homemanager on non-nixos distros or nix-darwin.

I have to research for the reason the NIX_ version was introduced in the first place.

@Skyb0rg007

Copy link
Copy Markdown
Contributor

I think yes, we probable should define some kid of standard

Maybe we should have a soft-rule that derivations should avoid depending on the cacert derivation, and instead look in ${SSL_CERT_PATH:-/etc/ssl/certs/ca-certificates.crt}; we already have a hook to set that variable when cacert is in nativeCheckInputs for running tests.
Information about web certificates is a good example of "we are interacting with the outside world, we really shouldn't be 'pure' here".

I had to research for the reason the NIX_ version was introduced in the first place.

Can you link that discussion? I'm unfamiliar.

@MarcelCoding

Copy link
Copy Markdown
Member Author

These two PRs seems to be the importent once about the introduction about NIX_SSL_CERT_FILE. But i dont quite see why it is used instead of just only SSL_CERT_FILE.

Comment thread nixos/modules/security/ca.nix Resolved
@MarcelCoding
MarcelCoding force-pushed the certifi-use-system-ca branch 2 times, most recently from 919c029 to 6af8088 Compare November 12, 2026 16:52
Comment thread nixos/modules/security/ca.nix Outdated
Comment on lines +119 to +123
systemd.units."service.d/10-SSL_CERT_FILE.conf".text = /* ini */ ''
[Service]
Environment=SSL_CERT_FILE=/etc/ssl/certs/ca-bundle.crt
Environment=NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-bundle.crt
'';

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.

ofc there is an option for this: systemd.globalEnvironment

@nixpkgs-ci nixpkgs-ci Bot added the 12.approvals: 1 This PR was reviewed and approved by one person. label Nov 13, 2026
@booxter booxter changed the title nixos/ca: set NIX_SSL_CERT_FILE nixos/ca: set {NIX_,}SSL_CERT_FILE Nov 14, 2026
@booxter

booxter commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

I am not convinced this is safe in the current version.


There may be scenarios where /etc/ bundle is not available, e.g. if the service is confined (RootDirectory).

A user may have the variable specifically pointing to a different bundle to make the confined service work, but if we override it with a drop-in, their Environment won't have an impact. This will probably be addressed if Sandro's globalEnvironment suggestion is adopted.


Since you are setting these variables unconditionally, users with a custom SSL_CERT_FILE set (like the linked example above) will lose their override because NIX_SSL_CERT_FILE may take precedence (depending on whether a particular SSL library was patched to honor NIX_ variable, or not).

It would be saf-er if we would avoid setting the variables if any of the two are detected in Environment.

Obviously, this would complicate the implementation - since now we would have to peek into the environment at eval time. And while it would mostly work, it would not be exhaustive regardless (e.g. EnvironmentFile is not available at this point). (One can go further and detect it at runtime - which is what I'm doing in nix-darwin counterpart... But it's a lot more work.)


How far one has to go to support these situations is of course debatable, so I'm leaving this for consideration, not as a blocking comment.

Right now, nss-cacert updates need to go though staging and
changes to `security.pki.caCertificateBlacklist` (like in NixOS#576390
or like custom user preferences) are not directly picked up
by applications using the `cacert` package.

This `NIX_` environment variable is patched-in in packages like pythons `certifi`
to override the ca bundle at runtime. Additionally also the SSL_CERT_FILE
is defined too, for packages supporting it out of the box.

Without this change `certifi` still uses an old version of the ca bundle:
```
❯ nix-shell -p python3Packages.certifi --command 'diff -u $(python -m certifi) /etc/ssl/certs/ca-bundle.crt'
--- /nix/store/6x3vic5vm4jzp29yna1qkp0xmf3qm35i-nss-cacert-3.126/etc/ssl/certs/ca-bundle.crt    1970-01-01 01:00:01.000000000 +0100
+++ /etc/ssl/certs/ca-bundle.crt        1970-01-01 01:00:01.000000000 +0100
@@ -1758,8 +1758,15 @@

 Comodo AAA Services root
 Traditional PEM block omitted: this certificate is not trusted for authenticating servers.
-Trusted for:
+Rejected for:
+  - 1.3.6.1.5.5.7.3.1 (RFC5280: serverAuth key usage)
+  - 1.3.6.1.5.5.7.3.2 (RFC5280: clientAuth key usage)
+  - 1.3.6.1.5.5.7.3.3 (RFC5280: codeSigning key usage)
   - 1.3.6.1.5.5.7.3.4 (RFC5280: emailProtection key usage)
+  - 1.3.6.1.5.5.7.3.8 (RFC5280: timeStamping key usage)
+  - 1.3.6.1.5.5.7.3.5 (RFC2459: ipsecEndSystem key usage)
+  - 1.3.6.1.5.5.7.3.6 (RFC2459: ipsecTunnel key usage)
+  - 1.3.6.1.5.5.7.3.7 (RFC2459: ipsecUser key usage)

<and some more>
 ```

With this change `certifi` uses the same bundle as the module system
configures:

```
❯ nix-shell -p python3Packages.certifi --command 'diff -u $(NIX_SSL_CERT_FILE=/etc/ssl/certs/ca-bundle.crt python -m certifi) /etc/ssl/certs/ca-bundle.crt'
<no diff>
```

Co-authored-by: Sandro Jäckel <[email protected]>
@MarcelCoding

MarcelCoding commented Nov 14, 2026 •

Copy link
Copy Markdown
Member Author

Thx, @SuperSandro2000 fix actually not only fixes the override of Environment but also of EnvironmentFile:

(ofc this still has the issue that we set both variables and the application may prefere the one the user did not override)

systemd.units."service.d/10-SSL_CERT_FILE.conf".text = lib.mkForce "";
systemd.globalEnvironment.NIX_SSL_CERT_FILE = "hallo";
systemd.services.test = {
  wantedBy = [ "default.target" ];
  serviceConfig.EnvironmentFile = pkgs.writeText "env" "NIX_SSL_CERT_FILE=test";
  script = ''
    echo "$NIX_SSL_CERT_FILE"
  '';
};
[root@test-vm:~]# systemctl cat test
# /etc/systemd/system/test.service -> /nix/store/4dryg1v485hgn604rxjhxz3zj0vifwgh-unit-test.service/test.service
[Unit]

[Service]
Environment="LOCALE_ARCHIVE=/nix/store/rbvrn9kch96rr5nm7jdp3913d51nm09g-glibc-locales-2.44-57/lib/locale/locale-archive"
Environment="NIX_SSL_CERT_FILE=hallo"
Environment="PATH=/nix/store/a50ml7za6h61m4mv4cdbvkl33c6sx5yn-coreutils-9.12/bin:/nix/store/5zsc280i2i7bqgjn7kln87w1ss6li22f-findutils-4.11.0/bin:/nix/store/5sgjjiqn90zmvlvlgadw7ss00nn3hpjb-gnugrep-3.12/bin:/nix/store/x275j18p7dx6kv7vfj>
Environment="TZDIR=/nix/store/s92hlmg73b3zlw38dfgm13v9cah2bm80-tzdata-2026f/share/zoneinfo"
EnvironmentFile=/nix/store/d5h4yxlcy9aialjwd1n5m99ymm6sllx8-env
ExecStart=/nix/store/h6wyql7msi7fvhbvkckdk2k23zjpax2f-unit-script-test-start/bin/test-start

[Install]
WantedBy=default.target

# /nix/store/w7c6jv0qdhcyw5gvfgv5726g03mv3inx-unit-service.d-10-SSL_CERT_FILE.conf/service.d/10-SSL_CERT_FILE.conf is a mask.


[root@test-vm:~]# systemctl status test
○ test.service
     Loaded: loaded (/etc/systemd/system/test.service; enabled; preset: ignored)
    Drop-In: /nix/store/w7c6jv0qdhcyw5gvfgv5726g03mv3inx-unit-service.d-10-SSL_CERT_FILE.conf/service.d
             └─10-SSL_CERT_FILE.conf
     Active: inactive (dead) since Sat 2026-11-14 13:32:58 UTC; 39s ago
   Duration: 34ms
 Invocation: 6a01adde71ba428b8f6193f2038ae04a
    Process: 545 ExecStart=/nix/store/h6wyql7msi7fvhbvkckdk2k23zjpax2f-unit-script-test-start/bin/test-start (code=exited, status=0/SUCCESS)
   Main PID: 545 (code=exited, status=0/SUCCESS)
         IP: 0B in, 0B out
         IO: 0B read, 0B written
   Mem peak: 2.6M
        CPU: 9ms

Nov 14 13:32:58 test-vm systemd[1]: Started test.service.
Nov 14 13:32:58 test-vm test-start[545]: test
Nov 14 13:32:58 test-vm systemd[1]: test.service: Deactivated successfully.

@Skyb0rg007

Skyb0rg007 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

If certifi is the main problem package, I have an alternative solution here which I think makes more sense.

Edit: the linked PR is now ready for review

@SuperSandro2000 SuperSandro2000 added the backport staging-nixos-26.11 Backport PR automatically label Nov 25, 2026
@SuperSandro2000

Copy link
Copy Markdown
Member

Even if we remove it from certifi, this is still very valuable to get updates quickly into the system.

@NixOS/security-review Can you take a look?

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

Labels

1.severity: security Issues which raise a security issue, or PRs that fix one 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 7.no default reviewers This PR targets a package or module with no existing maintainers; roving reviewers, take pity! 8.has: module (update) This PR changes an existing module in `nixos/` 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-nixos-tests This PR causes rebuilds for all NixOS tests and should normally target the staging branches. 12.approvals: 1 This PR was reviewed and approved by one person. backport staging-nixos-26.05 Backport PR automatically backport staging-nixos-26.11 Backport PR automatically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants