nix store prefetch-tarball -> nix flake prefetch

This commit is contained in:
Eelco Dolstra 2021-01-11 12:36:39 +01:00
parent 7480f2bf20
commit 93ad6430ed
4 changed files with 89 additions and 87 deletions

28
src/nix/flake-prefetch.md Normal file
View file

@ -0,0 +1,28 @@
R""(
# Examples
* Download a tarball and unpack it:
```console
# nix flake prefetch https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.10.5.tar.xz
Downloaded 'https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.10.5.tar.xz?narHash=sha256-3XYHZANT6AFBV0BqegkAZHbba6oeDkIUCDwbATLMhAY='
to '/nix/store/sl5vvk8mb4ma1sjyy03kwpvkz50hd22d-source' (hash
'sha256-3XYHZANT6AFBV0BqegkAZHbba6oeDkIUCDwbATLMhAY=').
```
* Download the `dwarffs` flake (looked up in the flake registry):
```console
# nix flake prefetch dwarffs --json
{"hash":"sha256-VHg3MYVgQ12LeRSU2PSoDeKlSPD8PYYEFxxwkVVDRd0="
,"storePath":"/nix/store/hang3792qwdmm2n0d9nsrs5n6bsws6kv-source"}
```
# Description
This command downloads the source tree denoted by flake reference
*flake-url*. Note that this does not need to be a flake (i.e. it does
not have to contain a `flake.nix` file).
)""

View file

@ -960,6 +960,45 @@ struct CmdFlakeShow : FlakeCommand
}
};
struct CmdFlakePrefetch : FlakeCommand, MixJSON
{
CmdFlakePrefetch()
{
}
std::string description() override
{
return "download the source tree denoted by a flake reference into the Nix store";
}
std::string doc() override
{
return
#include "flake-prefetch.md"
;
}
void run(ref<Store> store) override
{
auto originalRef = getFlakeRef();
auto resolvedRef = originalRef.resolve(store);
auto [tree, lockedRef] = resolvedRef.fetchTree(store);
auto hash = store->queryPathInfo(tree.storePath)->narHash;
if (json) {
auto res = nlohmann::json::object();
res["storePath"] = store->printStorePath(tree.storePath);
res["hash"] = hash.to_string(SRI, true);
logger->cout(res.dump());
} else {
notice("Downloaded '%s' to '%s' (hash '%s').",
lockedRef.to_string(),
store->printStorePath(tree.storePath),
hash.to_string(SRI, true));
}
}
};
struct CmdFlake : NixMultiCommand
{
CmdFlake()
@ -973,6 +1012,7 @@ struct CmdFlake : NixMultiCommand
{"clone", []() { return make_ref<CmdFlakeClone>(); }},
{"archive", []() { return make_ref<CmdFlakeArchive>(); }},
{"show", []() { return make_ref<CmdFlakeShow>(); }},
{"prefetch", []() { return make_ref<CmdFlakePrefetch>(); }},
})
{
}

View file

@ -246,17 +246,15 @@ static int main_nix_prefetch_url(int argc, char * * argv)
static RegisterLegacyCommand r_nix_prefetch_url("nix-prefetch-url", main_nix_prefetch_url);
struct CmdStorePrefetch : StoreCommand, MixJSON
struct CmdStorePrefetchFile : StoreCommand, MixJSON
{
std::string url;
bool executable = false;
bool unpack;
std::optional<std::string> name;
HashType hashType = htSHA256;
std::optional<Hash> expectedHash;
CmdStorePrefetch(bool unpack)
: unpack(unpack)
CmdStorePrefetchFile()
{
addFlag({
.longName = "name",
@ -267,7 +265,7 @@ struct CmdStorePrefetch : StoreCommand, MixJSON
addFlag({
.longName = "expected-hash",
.description = unpack ? "expected NAR hash of the unpacked tarball" : "expected hash of the file",
.description = "expected hash of the file",
.labels = {"hash"},
.handler = {[&](std::string s) {
expectedHash = Hash::parseAny(s, hashType);
@ -276,14 +274,31 @@ struct CmdStorePrefetch : StoreCommand, MixJSON
addFlag(Flag::mkHashTypeFlag("hash-type", &hashType));
addFlag({
.longName = "executable",
.description = "make the resulting file executable",
.handler = {&executable, true},
});
expectArg("url", &url);
}
Category category() override { return catUtility; }
std::string description() override
{
return "download a file into the Nix store";
}
std::string doc() override
{
return
#include "store-prefetch-file.md"
;
}
void run(ref<Store> store) override
{
auto [storePath, hash] = prefetchFile(store, url, name, hashType, expectedHash, unpack, executable);
auto [storePath, hash] = prefetchFile(store, url, name, hashType, expectedHash, false, executable);
if (json) {
auto res = nlohmann::json::object();
@ -299,54 +314,4 @@ struct CmdStorePrefetch : StoreCommand, MixJSON
}
};
struct CmdStorePrefetchFile : CmdStorePrefetch
{
CmdStorePrefetchFile()
: CmdStorePrefetch(false)
{
name = "source";
addFlag({
.longName = "executable",
.description = "make the resulting file executable",
.handler = {&executable, true},
});
}
std::string description() override
{
return "download a file into the Nix store";
}
std::string doc() override
{
return
#include "store-prefetch-file.md"
;
}
};
static auto rCmdStorePrefetchFile = registerCommand2<CmdStorePrefetchFile>({"store", "prefetch-file"});
struct CmdStorePrefetchTarball : CmdStorePrefetch
{
CmdStorePrefetchTarball()
: CmdStorePrefetch(true)
{
name = "source";
}
std::string description() override
{
return "download and unpack a tarball into the Nix store";
}
std::string doc() override
{
return
#include "store-prefetch-tarball.md"
;
}
};
static auto rCmdStorePrefetchTarball = registerCommand2<CmdStorePrefetchTarball>({"store", "prefetch-tarball"});

View file

@ -1,31 +0,0 @@
R""(
# Examples
* Download a tarball and unpack it:
```console
# nix store prefetch-tarball https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.10.5.tar.xz
Downloaded 'https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.10.5.tar.xz'
to '/nix/store/sl5vvk8mb4ma1sjyy03kwpvkz50hd22d-source' (hash
'sha256-3XYHZANT6AFBV0BqegkAZHbba6oeDkIUCDwbATLMhAY=').
```
* Download a tarball and unpack it, unless it already exists in the
Nix store:
```console
# nix store prefetch-tarball https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.10.5.tar.xz \
--expected-hash sha256-3XYHZANT6AFBV0BqegkAZHbba6oeDkIUCDwbATLMhAY=
```
# Description
This command downloads a tarball or zip file from *url*, unpacks it,
and adds the unpacked tree to the Nix store. It prints out the
resulting store path and the NAR hash of that store path.
The name component of the store path defaults to `source`, but this
can be overriden using `--name`.
)""