Skip to content

Partially revert: "treewide: support NIX_SSL_CERT_FILE as an impureEnvVar" - #401917

Closed
TomaSajt wants to merge 2 commits into
NixOS:masterfrom
TomaSajt:nix-ssl-cert-file
Closed

TomaSajt wants to merge 2 commits into
NixOS:masterfrom
TomaSajt:nix-ssl-cert-file

Conversation

@TomaSajt

@TomaSajt TomaSajt commented Apr 26, 2025 •

Copy link
Copy Markdown
Contributor

Partial revert of #303307

Possible less-radical alternative: #345322 / #401942

CC: @timbertson

The change in question caused too many issues.
(Mostly on Darwin, though some Linux users also encountered issues with it)

Some issues:

People tried to solve it on the Nix side.


Note: this counts as a breaking change, and we have already entered the 25.05 breaking change restriction period.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions Bot added 6.topic: lib The Nixpkgs function library 8.has: documentation This PR adds or changes documentation labels Apr 26, 2025
@nix-owners
nix-owners Bot requested review from hsjobeki and infinisil April 26, 2025 00:25
@TomaSajt
TomaSajt force-pushed the nix-ssl-cert-file branch from 14a2ed5 to 6c25f39 Compare April 26, 2025 00:29
@TomaSajt
TomaSajt requested a review from timbertson April 26, 2025 00:30
@github-actions github-actions Bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Apr 26, 2025
@emilazy

emilazy commented Apr 26, 2025

Copy link
Copy Markdown
Member

cc @leona-ya for freeze matters

@TomaSajt
TomaSajt marked this pull request as draft April 26, 2025 01:43
@timbertson

Copy link
Copy Markdown
Contributor

I believe #345322 fixes this, and I'd be pretty sad to see this reverted when there's been a fix available for 6+ months. This is necessary in order to support authenticated resources, which is pretty common in corporate environments.

@timbertson

Copy link
Copy Markdown
Contributor

I've also created a simpler version of @lukegb's fix here: #401942

@TomaSajt

Copy link
Copy Markdown
Contributor Author

I would be okay with not reverting it, I mainly just wanted to start a discussion and speed things up.

I'm still experimenting with the viability of the Nix-side fix.

Also, @timbertson could you show us how you're configuring NIX_SSL_CERT_FILE to be used by the FOD builders? I'm assuming you're setting that env-var on the daemon in some way.

@timbertson

timbertson commented Apr 26, 2025 •

Copy link
Copy Markdown
Contributor

I'm assuming you're setting that env-var on the daemon in some way.

I'm not changing the daemon's permanent config, I'm using the configurable-impure-env functionality which is experimental (or it was last time I looked).

I wrote https://github.com/timbertson/netproxrc for precisely this purpose, it sets NIX_SSL_CERT_FILE and a few other envvars to inject itself as an https proxy, there's more details in the README.

@TomaSajt

TomaSajt commented Apr 26, 2025 •

Copy link
Copy Markdown
Contributor Author

Thank you for clarifying.
I see that it uses /tmp/netproxrc-cert.pem as the value for the env vars, e.g. for NIX_SSL_CERT_FILE.
AIUI, this path is readable by all the builder users.

This is all fine and good.

However, not all people who set the NIX_SSL_CERT_FILE variable of the daemon set it to something readable from the builder.

AFAICT some darwin setups configure the NIX_SSL_CERT_FILE env var of the daemon to be something like /etc/ssl/certs/ca-certificates.crt, I assume via nix-darwin.

I'm not sure why.
They might just want to override the ssl-cert-file option of nix.conf.

I'm still in the process of figuring out the internals of ssl-cert-file and NIX_SSL_CERT_FILE in the nix codebase.


Note: the default value of ssl-cert-file is /etc/ssl/certs/ca-certificates.crt, which is not readable from the builder...., but there's some relevant linux-only logic that might make it actually readable: https://github.com/NixOS/nix/blob/3f811c237312a91cfe9314a215ebb4b159bccb23/src/libstore/unix/build/derivation-builder.cc#L1947-L1950

Not sure. As I said, still investigating

@TomaSajt

TomaSajt commented Apr 26, 2025 •

Copy link
Copy Markdown
Contributor Author

Okay, I found something pretty interesting.

https://github.com/NixOS/nix/blob/3f811c237312a91cfe9314a215ebb4b159bccb23/src/libstore/unix/build/derivation-builder.cc#L1947-L1950
This code makes caFile (which is the value of ssl-cert-file but may be overridden via NIX_SSL_CERT_FILE) available under the path /etc/ssl/certs/ca-certificates.crt, (only in FODs).
(essentially, /etc/ssl/certs/ca-certificates.crt in the nix build sandbox is a "symlink" to caFile outside of the build sandbox)

This is pretty interesting.
Let's assume we're on Linux, where this logic runs.
Then, even without using any impureEnvVars, we could set NIX_SSL_CERT_FILE to just be /etc/ssl/certs/ca-certificates.crt and it would function correctly.
(Not saying we should do this, just an option)

Some packages, like openssl are explicitly told to use /etc/ssl/certs/ca-certificates.crt during runtime:
https://github.com/search?q=repo%3ANixOS%2Fnixpkgs%20%2Fetc%2Fssl%2Fcerts%2Fca-certificates.crt&type=code

This probably means that openssl can function without the cacert hook, and any impure env vars. I have not check if it actually does.

@timbertson

Copy link
Copy Markdown
Contributor

Oh, nice find.

Let's assume we're on Linux, where this logic runs.
Then, even without using any impureEnvVars, we could set NIX_SSL_CERT_FILE to just be /etc/ssl/certs/ca-certificates.crt and it would function correctly.
(Not saying we should do this, just an option)

Yeah, I think that would work? In the sandbox on linux at least, although surely not with sandboxing disabled, and it sounds like not on OSX?

I can't tell if it's possible from here, but a better solution would be if this same code path also exported NIX_SSL_CERT_FILE inside the builder environment. That way it's only activated if the path is mounted at that location, and it would then be impossible (under this code path) for NIX_SSL_CERT_FILE to point to an unreadable path.

If we're modifying the builder, it would also make sense to add a check that if NIX_SSL_CERT_FILE is set, and it won't be readable inside the sandbox, then unset it. That should never be the case on linux, but might be the case on OSX?

@TomaSajt

TomaSajt commented Apr 27, 2025 •

Copy link
Copy Markdown
Contributor Author

If we're talking about darwin, the sandboxing there is completely different from the sandboxing of nix. AIUI it works using an allowlist instead of chroot.
(The current issue on darwin is: because the caFile is not added to the allowlist, systems with sandboxing enabled are not able to read it).

Let's say we remove NIX_SSL_CERT_FILE from impureEnvVars and make it a special case inside nix by changing the nix code so that it guarantees that NIX_SSL_CERT_FILE points at a cert file readable from inside the builder and that file's contents are the same as the caFile.
It wouldn't guarantee an exact location, just that if you read the file, it has what we need.
On linux, it may be hardcoded, like it already is (since we can make "symlinks").
On darwin it would be the same path as the configured caFile but the sandbox rules would make sure it is in the allowlist.

Not sure what should happen if the caFile value is misconfigured.
Should it leave the env var unset?
Should it set the env var to /no-cert-file.crt? (This is what the stdenv does if the env var is unset)
Should it just fail?


All this would make the cacert not required in cases where NIX_SSL_CERT_FILE support was patched into the app. In cases where only other names, like SSL_CERT_FILE are supported, we'd still need the hook.


I don't 100% understand what the comment is saying but I'll link it here:

# Prevent SSL libraries from using certificates in /etc/ssl, unless set explicitly.
# Leave it in impure shells for convenience.
if [[ -z "${NIX_SSL_CERT_FILE:-}" && "${IN_NIX_SHELL:-}" != "impure" ]]; then
export NIX_SSL_CERT_FILE=/no-cert-file.crt
fi
# Another variant left for compatibility.
if [[ -z "${SSL_CERT_FILE:-}" && "${IN_NIX_SHELL:-}" != "impure" ]]; then
export SSL_CERT_FILE=/no-cert-file.crt
fi

I've been writing this message for too long, so I'll leave it at this for now.

@timbertson

Copy link
Copy Markdown
Contributor

Let's say we remove NIX_SSL_CERT_FILE from impureEnvVars and make it a special case inside nix by changing the nix code so that it guarantees that NIX_SSL_CERT_FILE points at a cert file readable from inside the builder and that file's contents are the same as the caFile.
It wouldn't guarantee an exact location, just that if you read the file, it has what we need.

I do think this would be great, but I also don't think it's a viable short-term direction, since it requires a new nix release, and potentially there would be debate about whether a nixpkgs concept belongs in nix (I'm not sure if it is a nixpkgs or pure nix concept, but either way I'm not convinced it's a trivial change).

Short-term I am still convinced that #401942 (or #345322) is a straighforward fix, and I'd like to put our efforts into getting that merged rather than waiting on nix changes.

@TomaSajt

Copy link
Copy Markdown
Contributor Author

Short-term I am still convinced that #401942 (or #345322) is a straighforward fix, and I'd like to put our efforts into getting that merged rather than waiting on nix changes.

I agree. Let's not get nix involved just yet.
This PR was mostly just a place where I could do some mindstorming.
Will review your PR soon.

@timbertson

Copy link
Copy Markdown
Contributor

Can we close this now that #401942 is merged? 🤞

@TomaSajt TomaSajt closed this May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: lib The Nixpkgs function library 8.has: documentation This PR adds or changes documentation 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants