From 356352c3709f69b6d11ed7f14ffa586219170908 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 19 Jan 2024 01:07:26 -0500 Subject: [PATCH] Add missing `--hash-algo` flag to `nix store add` --- doc/manual/rl-next/nix-store-add.md | 7 +++++++ src/nix/add-to-store.cc | 7 +++++-- tests/functional/add.sh | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 doc/manual/rl-next/nix-store-add.md diff --git a/doc/manual/rl-next/nix-store-add.md b/doc/manual/rl-next/nix-store-add.md new file mode 100644 index 000000000..d55711569 --- /dev/null +++ b/doc/manual/rl-next/nix-store-add.md @@ -0,0 +1,7 @@ +--- +synopsis: Give `nix store add` a `--hash-algo` flag +prs: 9809 +--- + +Adds a missing feature that was present in the old CLI, and matches our +plans to have similar flags for `nix hash convert` and `hash hash path`. diff --git a/src/nix/add-to-store.cc b/src/nix/add-to-store.cc index 171848002..f2dbe8a2c 100644 --- a/src/nix/add-to-store.cc +++ b/src/nix/add-to-store.cc @@ -22,6 +22,7 @@ struct CmdAddToStore : MixDryRun, StoreCommand Path path; std::optional namePart; ContentAddressMethod caMethod = FileIngestionMethod::Recursive; + HashAlgorithm hashAlgo = HashAlgorithm::SHA256; CmdAddToStore() { @@ -51,6 +52,8 @@ struct CmdAddToStore : MixDryRun, StoreCommand this->caMethod = parseIngestionMethod(s); }}, }); + + addFlag(Flag::mkHashAlgoFlag("hash-algo", &hashAlgo)); } void run(ref store) override @@ -63,9 +66,9 @@ struct CmdAddToStore : MixDryRun, StoreCommand auto storePath = dryRun ? store->computeStorePath( - *namePart, accessor, path2, caMethod, HashAlgorithm::SHA256, {}).first + *namePart, accessor, path2, caMethod, hashAlgo, {}).first : store->addToStoreSlow( - *namePart, accessor, path2, caMethod, HashAlgorithm::SHA256, {}).path; + *namePart, accessor, path2, caMethod, hashAlgo, {}).path; logger->cout("%s", store->printStorePath(storePath)); } diff --git a/tests/functional/add.sh b/tests/functional/add.sh index d0fedcb25..762e01dbe 100644 --- a/tests/functional/add.sh +++ b/tests/functional/add.sh @@ -37,9 +37,11 @@ clearStore path3=$(nix store add-path ./dummy) [[ "$path1" == "$path2" ]] [[ "$path1" == "$path3" ]] + path4=$(nix store add --mode nar --hash-algo sha1 ./dummy) ) ( path1=$(nix store add --mode flat ./dummy) path2=$(nix store add-file ./dummy) [[ "$path1" == "$path2" ]] + path4=$(nix store add --mode flat --hash-algo sha1 ./dummy) )