Skip to content

Substitution does not work for binary caches behind reverse proxies with compression #10275

Closed

Description

@xokdvium

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

  1. Spin up a binary cache
  2. Put it behind a reverse proxy with compression
  3. 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.

  1. All filters for libarchive are enabled in TarArchive in any case:

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);
}

  1. makeDecompressionSink is called both for fetching via curl and when copying to the store.

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);
});
}

  1. When the reverse proxy adds the Encoding-Type: gzip header makeDecompressionSink is called with method = "gzip" here

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.

auto decompressor = makeDecompressionSink(info->compression, tee);

Priorities

Add 👍 to issues you find important.

Activity

  1. thufschmitt commented on Mar 21, 2024

    @thufschmitt
    Member

    (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

  2. xokdvium commented on Mar 21, 2024

    @xokdvium
    ContributorAuthor

    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-url works in the same way: i.e. fetch via curl and then copy to store via two Source created with makeDecompressionSink. Feel free to mark this as duplicate

  3. thufschmitt commented on Apr 3, 2024

    @thufschmitt
    Member

    Fixed by #10341

  4. moved this from To triage to Done in Nix teamon Apr 3, 2024
  5. added a commit that references this issue on Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions