1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#include "command.hh"
#include "store-api.hh"
#include "references.hh"
using namespace nix;
struct CmdMakeContentAddressable : StorePathsCommand
{
CmdMakeContentAddressable()
{
realiseMode = Build;
}
std::string name() override
{
return "make-content-addressable";
}
std::string description() override
{
return "rewrite a path or closure to content-addressable form";
}
Examples examples() override
{
return {
Example{
"To create a content-addressable representation of GNU Hello (but not its dependencies):",
"nix make-content-addressable nixpkgs.hello"
},
Example{
"To compute a content-addressable representation of the current NixOS system closure:",
"nix make-content-addressable -r /run/current-system"
},
};
}
void run(ref<Store> store, Paths storePaths) override
{
auto paths = store->topoSortPaths(PathSet(storePaths.begin(), storePaths.end()));
paths.reverse();
std::map<Path, Path> remappings;
for (auto & path : paths) {
auto oldInfo = store->queryPathInfo(path);
auto oldHashPart = storePathToHash(path);
auto name = storePathToName(path);
StringSink sink;
store->narFromPath(path, sink);
StringMap rewrites;
ValidPathInfo info;
for (auto & ref : oldInfo->references) {
if (ref == path)
info.references.insert("self");
else {
auto replacement = get(remappings, ref, ref);
// FIXME: warn about unremapped paths?
info.references.insert(replacement);
if (replacement != ref)
rewrites[storePathToHash(ref)] = storePathToHash(replacement);
}
}
*sink.s = rewriteStrings(*sink.s, rewrites);
HashModuloSink hashModuloSink(htSHA256, oldHashPart);
hashModuloSink((unsigned char *) sink.s->data(), sink.s->size());
info.narHash = hashModuloSink.finish().first;
info.narSize = sink.s->size();
replaceInSet(info.references, path, std::string("self"));
info.path = store->makeFixedOutputPath(true, info.narHash, name, info.references);
replaceInSet(info.references, std::string("self"), info.path);
info.ca = makeFixedOutputCA(true, info.narHash);
printError("rewrote '%s' to '%s'", path, info.path);
auto source = sinkToSource([&](Sink & nextSink) {
RewritingSink rsink2(oldHashPart, storePathToHash(info.path), nextSink);
rsink2((unsigned char *) sink.s->data(), sink.s->size());
rsink2.flush();
});
store->addToStore(info, *source);
remappings[path] = info.path;
}
}
};
static RegisterCommand r1(make_ref<CmdMakeContentAddressable>());
|