Repository navigation
Description
Describe the bug
When a self-hosted binary cache is put behind a reverse proxy with some compression enabled.
E.g. Encoding-Type: gzip in nginx and traefik. With such setups nix fails to substitute a path with the following error:
error: input compression not recognized
Steps To Reproduce
- Spin up a binary cache
- Put it behind a reverse proxy with compression
- Observe substitution failures for all paths
Expected behavior
Nix substitution succeeds for all paths present in the cache.
Additional context
I've debugged this issue and I'm quite certain I understand what is going on. Let's assume the binary cache
uses zstd.
- All filters for libarchive are enabled in
TarArchivein any case:
Lines 45 to 55 in 346f92a
| this->archive = archive_read_new(); | |
| this->source = &source; | |
| if (!raw) { | |
| archive_read_support_filter_all(archive); | |
| archive_read_support_format_all(archive); | |
| } else { | |
| archive_read_support_filter_all(archive); | |
| archive_read_support_format_raw(archive); | |
| archive_read_support_format_empty(archive); | |
| } |
makeDecompressionSinkis called both for fetching via curl and when copying to the store.
nix/src/libutil/compression.cc
Lines 202 to 213 in 346f92a
| std::unique_ptr<FinishSink> makeDecompressionSink(const std::string & method, Sink & nextSink) | |
| { | |
| if (method == "none" || method == "") | |
| return std::make_unique<NoneSink>(nextSink); | |
| else if (method == "br") | |
| return std::make_unique<BrotliDecompressionSink>(nextSink); | |
| else | |
| return sourceToSink([&](Source & source) { | |
| auto decompressionSource = std::make_unique<ArchiveDecompressionSource>(source); | |
| decompressionSource->drainInto(nextSink); | |
| }); | |
| } |
- When the reverse proxy adds the
Encoding-Type: gzipheadermakeDecompressionSinkis called withmethod = "gzip"here
nix/src/libstore/filetransfer.cc
Line 163 in 346f92a
| decompressionSink = makeDecompressionSink(encoding, finalSink); |
But looking at 1 & 2 it becomes apparent that because all filters are enabled the compressed gzip stream (which is a NAR) gets
completely decompressed (applying the filter to the actual compressed NAR). Then when makeDecompressionSink is called once again to copy to the store, the NAR is uncompressed. But the narinfo reports the method to be e.g. ztd. Then the exception gets thrown from here because Source is not in fact a ztd compressed file.
nix/src/libstore/binary-cache-store.cc
Line 404 in 346f92a
| auto decompressor = makeDecompressionSink(info->compression, tee); |
Priorities
Add 👍 to issues you find important.
Activity
thufschmitt commentedon Mar 21, 2024
(I didn't have time to read the whole issue yet, apologies if this is nonsense, but: ) could that have the same root issue as #10254 ? That looks fairly similar in the symptoms.
/cc @a-n-n-a-l-e-e
xokdvium commentedon Mar 21, 2024
Indeed, that seems to be the exact same issue. I've spent some time debugging nix under gdb, so I'm quite sure the root cause is what I've described in this issue. I suppose
nix-prefetch-urlworks in the same way: i.e. fetch via curl and then copy to store via twoSourcecreated withmakeDecompressionSink. Feel free to mark this as duplicatethufschmitt commentedon Apr 3, 2024
Fixed by #10341
fix(nar): trust the narinfo Compression header when the URL carries n…
docs(test): unstale the #1470 attic test's comments (#1491)