aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nix/flake-prefetch.md28
-rw-r--r--src/nix/flake.cc40
-rw-r--r--src/nix/prefetch.cc77
-rw-r--r--src/nix/store-prefetch-tarball.md31
4 files changed, 89 insertions, 87 deletions
diff --git a/src/nix/flake-prefetch.md b/src/nix/flake-prefetch.md
new file mode 100644
index 000000000..a1cf0289a
--- /dev/null
+++ b/src/nix/flake-prefetch.md
@@ -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).
+
+)""
diff --git a/src/nix/flake.cc b/src/nix/flake.cc
index 2b91faa64..b73b9cf4e 100644
--- a/src/nix/flake.cc
+++ b/src/nix/flake.cc
@@ -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>(); }},
})
{
}
diff --git a/src/nix/prefetch.cc b/src/nix/prefetch.cc
index 969299489..ce8c85ecf 100644
--- a/src/nix/prefetch.cc
+++ b/src/nix/prefetch.cc
@@ -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,43 +274,17 @@ struct CmdStorePrefetch : StoreCommand, MixJSON
addFlag(Flag::mkHashTypeFlag("hash-type", &hashType));
- expectArg("url", &url);
- }
-
- Category category() override { return catUtility; }
-
- void run(ref<Store> store) override
- {
- auto [storePath, hash] = prefetchFile(store, url, name, hashType, expectedHash, unpack, executable);
-
- if (json) {
- auto res = nlohmann::json::object();
- res["storePath"] = store->printStorePath(storePath);
- res["hash"] = hash.to_string(SRI, true);
- logger->cout(res.dump());
- } else {
- notice("Downloaded '%s' to '%s' (hash '%s').",
- url,
- store->printStorePath(storePath),
- hash.to_string(SRI, true));
- }
- }
-};
-
-struct CmdStorePrefetchFile : CmdStorePrefetch
-{
- CmdStorePrefetchFile()
- : CmdStorePrefetch(false)
- {
- name = "source";
-
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";
@@ -324,29 +296,22 @@ struct CmdStorePrefetchFile : CmdStorePrefetch
#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
+ void run(ref<Store> store) override
{
- return "download and unpack a tarball into the Nix store";
- }
+ auto [storePath, hash] = prefetchFile(store, url, name, hashType, expectedHash, false, executable);
- std::string doc() override
- {
- return
- #include "store-prefetch-tarball.md"
- ;
+ if (json) {
+ auto res = nlohmann::json::object();
+ res["storePath"] = store->printStorePath(storePath);
+ res["hash"] = hash.to_string(SRI, true);
+ logger->cout(res.dump());
+ } else {
+ notice("Downloaded '%s' to '%s' (hash '%s').",
+ url,
+ store->printStorePath(storePath),
+ hash.to_string(SRI, true));
+ }
}
};
-static auto rCmdStorePrefetchTarball = registerCommand2<CmdStorePrefetchTarball>({"store", "prefetch-tarball"});
+static auto rCmdStorePrefetchFile = registerCommand2<CmdStorePrefetchFile>({"store", "prefetch-file"});
diff --git a/src/nix/store-prefetch-tarball.md b/src/nix/store-prefetch-tarball.md
deleted file mode 100644
index 535d7e022..000000000
--- a/src/nix/store-prefetch-tarball.md
+++ /dev/null
@@ -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`.
-
-)""