Skip to content

Commit

Permalink
Add a new nix store add command
Browse files Browse the repository at this point in the history
Deprecate `nix store add-file` and `nix store add-path`, and replace
them with a single `nix store add` command.
  • Loading branch information
thufschmitt committed Nov 15, 2023
1 parent 2afe2e4 commit 0e47ea3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 45 deletions.
28 changes: 0 additions & 28 deletions src/nix/add-file.md

This file was deleted.

2 changes: 1 addition & 1 deletion src/nix/add-path.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Add a directory to the store:
# mkdir dir
# echo foo > dir/bar

# nix store add-path ./dir
# nix store add ./dir
/nix/store/6pmjx56pm94n66n4qw1nff0y1crm8nqg-dir

# cat /nix/store/6pmjx56pm94n66n4qw1nff0y1crm8nqg-dir/bar
Expand Down
58 changes: 42 additions & 16 deletions src/nix/add-to-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@

using namespace nix;

static FileIngestionMethod parseIngestionMethod(std::string_view input)
{
if (input == "flat") {
return FileIngestionMethod::Flat;
} else if (input == "nar") {
return FileIngestionMethod::Recursive;
} else {
throw UsageError("Unknown hash mode '%s', expect `flat` or `nar`");
}
}

struct CmdAddToStore : MixDryRun, StoreCommand
{
Path path;
std::optional<std::string> namePart;
FileIngestionMethod ingestionMethod;
FileIngestionMethod ingestionMethod = FileIngestionMethod::Recursive;

CmdAddToStore()
{
Expand All @@ -23,6 +34,23 @@ struct CmdAddToStore : MixDryRun, StoreCommand
.labels = {"name"},
.handler = {&namePart},
});

addFlag({
.longName = "mode",
.shortName = 'n',
.description = R"(
How to compute the hash of the input.
One of:
- `nar` (the default): Serialises the input as an archive (following the [_Nix Archive Format_](https://edolstra.github.io/pubs/phd-thesis.pdf#page=101)) and passes that to the hash function.
- `flat`: Assumes that the input is directly a single file and directly passes it to the hash function;
)",
.labels = {"hash-mode"},
.handler = {[this](std::string s) {
this->ingestionMethod = parseIngestionMethod(s);
}},
});
}

void run(ref<Store> store) override
Expand Down Expand Up @@ -62,45 +90,43 @@ struct CmdAddToStore : MixDryRun, StoreCommand
}
};

struct CmdAddFile : CmdAddToStore
struct CmdAdd : CmdAddToStore
{
CmdAddFile()
{
ingestionMethod = FileIngestionMethod::Flat;
}

std::string description() override
{
return "add a regular file to the Nix store";
return "Add a file or directory to the Nix store";
}

std::string doc() override
{
return
#include "add-file.md"
#include "add-path.md"
;
}
};

struct CmdAddPath : CmdAddToStore
struct CmdAddFile : CmdAddToStore
{
CmdAddPath()
CmdAddFile()
{
ingestionMethod = FileIngestionMethod::Recursive;
ingestionMethod = FileIngestionMethod::Flat;
}

std::string description() override
{
return "add a path to the Nix store";
return "Deprecated. Use [`nix store add --mode flat`](@docroot@/command-ref/new-cli/nix3-store-add.md) instead.";
}
};

std::string doc() override
struct CmdAddPath : CmdAddToStore
{
std::string description() override
{
return
#include "add-path.md"
;
return "Deprecated alias to [`nix store add`](@docroot@/command-ref/new-cli/nix3-store-add.md).";
}
};

static auto rCmdAddFile = registerCommand2<CmdAddFile>({"store", "add-file"});
static auto rCmdAddPath = registerCommand2<CmdAddPath>({"store", "add-path"});
static auto rCmdAdd = registerCommand2<CmdAdd>({"store", "add"});
17 changes: 17 additions & 0 deletions tests/functional/add.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,20 @@ hash2=$(nix-hash --type sha256 --base32 ./dummy)
echo $hash2

test "$hash1" = "sha256:$hash2"

#### New style commands

clearStore

(
path1=$(nix store add ./dummy)
path2=$(nix store add --mode nar ./dummy)
path3=$(nix store add-path ./dummy)
[[ "$path1" == "$path2" ]]
[[ "$path1" == "$path3" ]]
)
(
path1=$(nix store add --mode flat ./dummy)
path2=$(nix store add-file ./dummy)
[[ "$path1" == "$path2" ]]
)

0 comments on commit 0e47ea3

Please sign in to comment.