From 254b7afe66460908d59a894cf5e941e515a2b044 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 10 Feb 2025 17:52:26 +0100 Subject: [PATCH 1/2] nix flake prefetch: Add --out-link option This makes `nix flake prefetch` more useful for scripting and prevents the result from being GC'ed prematurely. --- src/nix/flake-prefetch.md | 6 +++++- src/nix/flake.cc | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/nix/flake-prefetch.md b/src/nix/flake-prefetch.md index a1cf0289ae9..4666aadc4df 100644 --- a/src/nix/flake-prefetch.md +++ b/src/nix/flake-prefetch.md @@ -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): diff --git a/src/nix/flake.cc b/src/nix/flake.cc index 6f220b495fe..4b32020af9b 100644 --- a/src/nix/flake.cc +++ b/src/nix/flake.cc @@ -18,6 +18,7 @@ #include "markdown.hh" #include "users.hh" #include "fetch-to-store.hh" +#include "local-fs-store.hh" #include #include @@ -1430,8 +1431,18 @@ struct CmdFlakeShow : FlakeCommand, MixJSON struct CmdFlakePrefetch : FlakeCommand, MixJSON { + std::optional 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 @@ -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()) + createOutLinks(*outLink, {BuiltPath::Opaque{storePath}}, *store2); + else + throw Error("'--out-link' is not supported for this Nix store"); + } } }; From 17d46041b8c68c66402296d23c3bccfe1731b17e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Wed, 12 Feb 2025 22:05:38 +0100 Subject: [PATCH 2/2] Add test --- tests/functional/tarball.sh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/functional/tarball.sh b/tests/functional/tarball.sh index 720b3688f4e..53807603cf3 100755 --- a/tests/functional/tarball.sh +++ b/tests/functional/tarball.sh @@ -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"