Skip to content

Commit 2acd0d1

Browse files
authored
Merge pull request #344 from DeterminateSystems/nar-info-cache-provenance
Store provenance info in the NAR info disk cache
2 parents 5217e30 + 9a41f11 commit 2acd0d1

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/libstore/nar-info-disk-cache.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "nix/util/sync.hh"
44
#include "nix/store/sqlite.hh"
55
#include "nix/store/globals.hh"
6+
#include "nix/store/provenance.hh"
67

78
#include <sqlite3.h>
89
#include <nlohmann/json.hpp>
@@ -36,6 +37,7 @@ create table if not exists NARs (
3637
deriver text,
3738
sigs text,
3839
ca text,
40+
provenance text,
3941
timestamp integer not null,
4042
present integer not null,
4143
primary key (cache, hashPart),
@@ -86,7 +88,7 @@ class NarInfoDiskCacheImpl : public NarInfoDiskCache
8688

8789
Sync<State> _state;
8890

89-
NarInfoDiskCacheImpl(Path dbPath = (getCacheDir() / "binary-cache-v7.sqlite").string())
91+
NarInfoDiskCacheImpl(Path dbPath = (getCacheDir() / "binary-cache-v8.sqlite").string())
9092
{
9193
auto state(_state.lock());
9294

@@ -109,14 +111,14 @@ class NarInfoDiskCacheImpl : public NarInfoDiskCache
109111
state->insertNAR.create(
110112
state->db,
111113
"insert or replace into NARs(cache, hashPart, namePart, url, compression, fileHash, fileSize, narHash, "
112-
"narSize, refs, deriver, sigs, ca, timestamp, present) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)");
114+
"narSize, refs, deriver, sigs, ca, provenance, timestamp, present) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1)");
113115

114116
state->insertMissingNAR.create(
115117
state->db, "insert or replace into NARs(cache, hashPart, timestamp, present) values (?, ?, ?, 0)");
116118

117119
state->queryNAR.create(
118120
state->db,
119-
"select present, namePart, url, compression, fileHash, fileSize, narHash, narSize, refs, deriver, sigs, ca from NARs where cache = ? and hashPart = ? and ((present = 0 and timestamp > ?) or (present = 1 and timestamp > ?))");
121+
"select present, namePart, url, compression, fileHash, fileSize, narHash, narSize, refs, deriver, sigs, ca, provenance from NARs where cache = ? and hashPart = ? and ((present = 0 and timestamp > ?) or (present = 1 and timestamp > ?))");
120122

121123
state->insertRealisation.create(
122124
state->db,
@@ -279,6 +281,8 @@ class NarInfoDiskCacheImpl : public NarInfoDiskCache
279281
for (auto & sig : tokenizeString<Strings>(queryNAR.getStr(10), " "))
280282
narInfo->sigs.insert(sig);
281283
narInfo->ca = ContentAddress::parseOpt(queryNAR.getStr(11));
284+
if (experimentalFeatureSettings.isEnabled(Xp::Provenance) && !queryNAR.isNull(12))
285+
narInfo->provenance = Provenance::from_json_str_optional(queryNAR.getStr(12));
282286

283287
return {oValid, narInfo};
284288
});
@@ -337,8 +341,10 @@ class NarInfoDiskCacheImpl : public NarInfoDiskCache
337341
narInfo && narInfo->fileHash)(
338342
narInfo ? narInfo->fileSize : 0, narInfo != 0 && narInfo->fileSize)(info->narHash.to_string(
339343
HashFormat::Nix32, true))(info->narSize)(concatStringsSep(" ", info->shortRefs()))(
340-
info->deriver ? std::string(info->deriver->to_string()) : "", (bool) info->deriver)(
341-
concatStringsSep(" ", info->sigs))(renderContentAddress(info->ca))(time(0))
344+
info->deriver ? std::string(info->deriver->to_string()) : "",
345+
(bool) info->deriver)(concatStringsSep(" ", info->sigs))(renderContentAddress(info->ca))(
346+
info->provenance ? info->provenance->to_json_str() : "",
347+
experimentalFeatureSettings.isEnabled(Xp::Provenance) && info->provenance)(time(0))
342348
.exec();
343349

344350
} else {

tests/functional/flakes/provenance.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ nix copy --to "file://$binaryCache" "$outPath"
8888

8989
clearStore
9090

91+
export _NIX_FORCE_HTTP=1 # force use of the NAR info disk cache
92+
93+
# Check that provenance is cached correctly.
94+
[[ $(nix path-info --json --json-format 1 --store "file://$binaryCache" "$outPath" | jq ".\"$outPath\".provenance") != null ]]
95+
[[ $(nix path-info --json --json-format 1 --store "file://$binaryCache" "$outPath" | jq ".\"$outPath\".provenance") != null ]]
96+
9197
nix copy --from "file://$binaryCache" "$outPath" --no-check-sigs
9298

9399
[[ $(nix path-info --json --json-format 1 "$outPath" | jq ".\"$outPath\".provenance") = $(cat <<EOF
@@ -124,6 +130,8 @@ nix copy --from "file://$binaryCache" "$outPath" --no-check-sigs
124130
EOF
125131
) ]]
126132

133+
unset _NIX_FORCE_HTTP
134+
127135
# Test `nix provenance show`.
128136
[[ $(nix provenance show "$outPath") = $(cat <<EOF
129137
$outPath

0 commit comments

Comments
 (0)