Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/libstore/include/nix/store/globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,17 @@ public:
mismatch if the build isn't reproducible.
)"};

Setting<unsigned int> ttlNarInfoCacheMeta{
this,
7 * 24 * 3600,
"narinfo-cache-meta-ttl",
R"(
The TTL in seconds for caching binary cache metadata (i.e.
`/nix-cache-info`). This determines how long information about a
binary cache (such as its store directory, priority, and whether it
wants mass queries) is considered valid before being refreshed.
)"};

Setting<bool> printMissing{
this, true, "print-missing", "Whether to print what paths need to be built or downloaded."};

Expand Down
5 changes: 1 addition & 4 deletions src/libstore/nar-info-disk-cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ class NarInfoDiskCacheImpl : public NarInfoDiskCache
/* How often to purge expired entries from the cache. */
const int purgeInterval = 24 * 3600;

/* How long to cache binary cache info (i.e. /nix-cache-info) */
const int cacheInfoTtl = 7 * 24 * 3600;

struct Cache
{
int id;
Expand Down Expand Up @@ -184,7 +181,7 @@ class NarInfoDiskCacheImpl : public NarInfoDiskCache
{
auto i = state.caches.find(uri);
if (i == state.caches.end()) {
auto queryCache(state.queryCache.use()(uri)(time(0) - cacheInfoTtl));
auto queryCache(state.queryCache.use()(uri)(time(0) - settings.ttlNarInfoCacheMeta));
if (!queryCache.next())
return std::nullopt;
auto cache = Cache{
Expand Down
3 changes: 3 additions & 0 deletions src/nix/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ static void disableNet()
settings.useSubstitutes = false;
if (!settings.tarballTtl.overridden)
settings.tarballTtl = std::numeric_limits<unsigned int>::max();
if (!settings.ttlNarInfoCacheMeta.overridden)
settings.ttlNarInfoCacheMeta = std::numeric_limits<unsigned int>::max();
if (!fileTransferSettings.tries.overridden)
fileTransferSettings.tries = 0;
if (!fileTransferSettings.connectTimeout.overridden)
Expand Down Expand Up @@ -568,6 +570,7 @@ void mainWrapped(int argc, char ** argv)
settings.tarballTtl = 0;
settings.ttlNegativeNarInfoCache = 0;
settings.ttlPositiveNarInfoCache = 0;
settings.ttlNarInfoCacheMeta = 0;
}

if (args.command->second->forceImpureByDefault() && !evalSettings.pureEval.overridden) {
Expand Down
21 changes: 21 additions & 0 deletions tests/functional/binary-cache.sh
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,24 @@ nix-store --delete "$outPath" "$docPath"
# -vvv is the level that logs during the loop
timeout 60 nix-build --no-out-link -E "$expr" --option substituters "file://$cacheDir" \
--option trusted-binary-caches "file://$cacheDir" --no-require-sigs


# Test that the narinfo-cache-meta-ttl causes nix-cache-info to be cached,
# and that --refresh overrides it.

# Populate the metadata cache by querying store info over HTTP.
_NIX_FORCE_HTTP=1 nix store info --store "file://$cacheDir"

# Remove nix-cache-info from the binary cache.
rm "$cacheDir/nix-cache-info"

# nix store info should still work because the metadata is cached
# (narinfo-cache-meta-ttl defaults to 7 days).
_NIX_FORCE_HTTP=1 nix store info --store "file://$cacheDir"

# But with --refresh, it should fail because nix-cache-info is gone
# and the cached metadata TTL is overridden to 0.
_NIX_FORCE_HTTP=1 expectStderr 1 nix store info --store "file://$cacheDir" --refresh | grepQuiet "uploading.*is not supported"

# Remove --refresh and it should work again.
_NIX_FORCE_HTTP=1 nix store info --store "file://$cacheDir"
Loading