Split 'nix store add-to-store' into 'add-path' and 'add-file'

This makes it consistent with 'nix hash <path|file>'.
This commit is contained in:
Eelco Dolstra 2020-12-04 00:58:09 +01:00
parent 8df58eae4c
commit f337aa7099
4 changed files with 77 additions and 25 deletions

View file

@ -2,8 +2,8 @@ R""(
# Description
Copy the file or directory *path* to the Nix store, and
print the resulting store path on standard output.
Copy the regular file *path* to the Nix store, and print the resulting
store path on standard output.
> **Warning**
>
@ -18,7 +18,7 @@ Add a regular file to the store:
```console
# echo foo > bar
# nix add-to-store --flat ./bar
# nix store add-file ./bar
/nix/store/cbv2s4bsvzjri77s2gb8g8bpcb6dpa8w-bar
# cat /nix/store/cbv2s4bsvzjri77s2gb8g8bpcb6dpa8w-bar

29
src/nix/add-path.md Normal file
View file

@ -0,0 +1,29 @@
R""(
# Description
Copy *path* to the Nix store, and print the resulting store path on
standard output.
> **Warning**
>
> The resulting store path is not registered as a garbage
> collector root, so it could be deleted before you have a
> chance to register it.
# Examples
Add a directory to the store:
```console
# mkdir dir
# echo foo > dir/bar
# nix store add-path ./dir
/nix/store/6pmjx56pm94n66n4qw1nff0y1crm8nqg-dir
# cat /nix/store/6pmjx56pm94n66n4qw1nff0y1crm8nqg-dir/bar
foo
```
)""

View file

@ -9,10 +9,11 @@ struct CmdAddToStore : MixDryRun, StoreCommand
{
Path path;
std::optional<std::string> namePart;
FileIngestionMethod ingestionMethod = FileIngestionMethod::Recursive;
FileIngestionMethod ingestionMethod;
CmdAddToStore()
{
// FIXME: completion
expectArg("path", &path);
addFlag({
@ -22,25 +23,6 @@ struct CmdAddToStore : MixDryRun, StoreCommand
.labels = {"name"},
.handler = {&namePart},
});
addFlag({
.longName = "flat",
.shortName = 0,
.description = "add flat file to the Nix store",
.handler = {&ingestionMethod, FileIngestionMethod::Flat},
});
}
std::string description() override
{
return "add a path to the Nix store";
}
std::string doc() override
{
return
#include "add-to-store.md"
;
}
void run(ref<Store> store) override
@ -78,4 +60,45 @@ struct CmdAddToStore : MixDryRun, StoreCommand
}
};
static auto rCmdAddToStore = registerCommand2<CmdAddToStore>({"store", "add-path"});
struct CmdAddFile : CmdAddToStore
{
CmdAddFile()
{
ingestionMethod = FileIngestionMethod::Flat;
}
std::string description() override
{
return "add a regular file to the Nix store";
}
std::string doc() override
{
return
#include "add-file.md"
;
}
};
struct CmdAddPath : CmdAddToStore
{
CmdAddPath()
{
ingestionMethod = FileIngestionMethod::Recursive;
}
std::string description() override
{
return "add a path to the Nix store";
}
std::string doc() override
{
return
#include "add-path.md"
;
}
};
static auto rCmdAddFile = registerCommand2<CmdAddFile>({"store", "add-file"});
static auto rCmdAddPath = registerCommand2<CmdAddPath>({"store", "add-path"});

View file

@ -36,7 +36,7 @@ other_store=file://$TEST_ROOT/other_store?store=/fnord/store
hash=$(nix hash file --type sha256 --base16 ./fetchurl.sh)
storePath=$(nix --store $other_store store add-path --flat ./fetchurl.sh)
storePath=$(nix --store $other_store store add-file ./fetchurl.sh)
outPath=$(nix-build '<nix/fetchurl.nix>' --argstr url file:///no-such-dir/fetchurl.sh --argstr sha256 $hash --no-out-link --substituters $other_store)