Skip to content

Commit

Permalink
Merge pull request #12443 from DeterminateSystems/prefetch-out-link
Browse files Browse the repository at this point in the history
nix flake prefetch: Add --out-link option
  • Loading branch information
roberth authored Feb 12, 2025
2 parents 0abc264 + 17d4604 commit eb91014
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/nix/flake-prefetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ R""(
* 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
# nix flake prefetch https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.10.5.tar.xz --out-link ./result
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=').

# cat ./result/README
Linux kernel
```

* Download the `dwarffs` flake (looked up in the flake registry):
Expand Down
18 changes: 18 additions & 0 deletions src/nix/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "markdown.hh"
#include "users.hh"
#include "fetch-to-store.hh"
#include "local-fs-store.hh"

#include <filesystem>
#include <nlohmann/json.hpp>
Expand Down Expand Up @@ -1430,8 +1431,18 @@ struct CmdFlakeShow : FlakeCommand, MixJSON

struct CmdFlakePrefetch : FlakeCommand, MixJSON
{
std::optional<std::filesystem::path> outLink;

CmdFlakePrefetch()
{
addFlag({
.longName = "out-link",
.shortName = 'o',
.description = "Create symlink named *path* to the resulting store path.",
.labels = {"path"},
.handler = {&outLink},
.completer = completePath
});
}

std::string description() override
Expand Down Expand Up @@ -1467,6 +1478,13 @@ struct CmdFlakePrefetch : FlakeCommand, MixJSON
store->printStorePath(storePath),
hash.to_string(HashFormat::SRI, true));
}

if (outLink) {
if (auto store2 = store.dynamic_pointer_cast<LocalFSStore>())
createOutLinks(*outLink, {BuiltPath::Opaque{storePath}}, *store2);
else
throw Error("'--out-link' is not supported for this Nix store");
}
}
};

Expand Down
14 changes: 7 additions & 7 deletions tests/functional/tarball.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ test_tarball .gz gzip
# All entries in tree.tar.gz refer to the same file, and all have the same inode when unpacked by GNU tar.
# We don't preserve the hard links, because that's an optimization we think is not worth the complexity,
# so we only make sure that the contents are copied correctly.
path="$(nix flake prefetch --json "tarball+file://$(pwd)/tree.tar.gz" | jq -r .storePath)"
[[ $(cat "$path/a/b/foo") = bar ]]
[[ $(cat "$path/a/b/xyzzy") = bar ]]
[[ $(cat "$path/a/yyy") = bar ]]
[[ $(cat "$path/a/zzz") = bar ]]
[[ $(cat "$path/c/aap") = bar ]]
[[ $(cat "$path/fnord") = bar ]]
nix flake prefetch --json "tarball+file://$(pwd)/tree.tar.gz" --out-link "$TEST_ROOT/result"
[[ $(cat "$TEST_ROOT/result/a/b/foo") = bar ]]
[[ $(cat "$TEST_ROOT/result/a/b/xyzzy") = bar ]]
[[ $(cat "$TEST_ROOT/result/a/yyy") = bar ]]
[[ $(cat "$TEST_ROOT/result/a/zzz") = bar ]]
[[ $(cat "$TEST_ROOT/result/c/aap") = bar ]]
[[ $(cat "$TEST_ROOT/result/fnord") = bar ]]

# Test a tarball that has multiple top-level directories.
rm -rf "$TEST_ROOT/tar_root"
Expand Down

0 comments on commit eb91014

Please sign in to comment.