Move NAR-related commands to 'nix nar'

This commit is contained in:
Eelco Dolstra 2020-07-24 20:42:24 +02:00
parent 79c1967ded
commit ef583303f0
6 changed files with 54 additions and 27 deletions

View file

@ -64,13 +64,11 @@ struct CmdCatNar : StoreCommand, MixCat
return "print the contents of a file inside a NAR file on stdout"; return "print the contents of a file inside a NAR file on stdout";
} }
Category category() override { return catUtility; }
void run(ref<Store> store) override void run(ref<Store> store) override
{ {
cat(makeNarAccessor(make_ref<std::string>(readFile(narPath)))); cat(makeNarAccessor(make_ref<std::string>(readFile(narPath))));
} }
}; };
static auto rCmdCatStore = registerCommand<CmdCatStore>("cat-store"); static auto rCmdCatStore = registerCommand2<CmdCatStore>({"store", "cat"});
static auto rCmdCatNar = registerCommand<CmdCatNar>("cat-nar"); static auto rCmdCatNar = registerCommand2<CmdCatNar>({"nar", "cat"});

View file

@ -134,7 +134,7 @@ struct CmdLsNar : Command, MixLs
return { return {
Example{ Example{
"To list a specific file in a NAR:", "To list a specific file in a NAR:",
"nix ls-nar -l hello.nar /bin/hello" "nix nar ls -l hello.nar /bin/hello"
}, },
}; };
} }
@ -144,13 +144,11 @@ struct CmdLsNar : Command, MixLs
return "show information about a path inside a NAR file"; return "show information about a path inside a NAR file";
} }
Category category() override { return catUtility; }
void run() override void run() override
{ {
list(makeNarAccessor(make_ref<std::string>(readFile(narPath)))); list(makeNarAccessor(make_ref<std::string>(readFile(narPath))));
} }
}; };
static auto rCmdLsStore = registerCommand<CmdLsStore>("ls-store"); static auto rCmdLsStore = registerCommand2<CmdLsStore>({"store", "ls"});
static auto rCmdLsNar = registerCommand<CmdLsNar>("ls-nar"); static auto rCmdLsNar = registerCommand2<CmdLsNar>({"nar", "ls"});

31
src/nix/nar.cc Normal file
View file

@ -0,0 +1,31 @@
#include "command.hh"
using namespace nix;
struct CmdNar : NixMultiCommand
{
CmdNar() : MultiCommand(RegisterCommand::getCommandsFor({"nar"}))
{ }
std::string description() override
{
return "query the contents of NAR files";
}
Category category() override { return catUtility; }
void run() override
{
if (!command)
throw UsageError("'nix nar' requires a sub-command.");
command->second->prepare();
command->second->run();
}
void printHelp(const string & programName, std::ostream & out) override
{
MultiCommand::printHelp(programName, out);
}
};
static auto rCmdNar = registerCommand<CmdNar>("nar");

View file

@ -196,13 +196,13 @@ narCache=$TEST_ROOT/nar-cache
rm -rf $narCache rm -rf $narCache
mkdir $narCache mkdir $narCache
[[ $(nix cat-store --store "file://$cacheDir?local-nar-cache=$narCache" $outPath/foobar) = FOOBAR ]] [[ $(nix store cat --store "file://$cacheDir?local-nar-cache=$narCache" $outPath/foobar) = FOOBAR ]]
rm -rfv "$cacheDir/nar" rm -rfv "$cacheDir/nar"
[[ $(nix cat-store --store "file://$cacheDir?local-nar-cache=$narCache" $outPath/foobar) = FOOBAR ]] [[ $(nix store cat --store "file://$cacheDir?local-nar-cache=$narCache" $outPath/foobar) = FOOBAR ]]
(! nix cat-store --store file://$cacheDir $outPath/foobar) (! nix store cat --store file://$cacheDir $outPath/foobar)
# Test NAR listing generation. # Test NAR listing generation.

View file

@ -22,9 +22,9 @@ outPath=$(nix-build dependencies.nix --no-out-link --sandbox-paths /nix/store)
nix path-info -r $outPath | grep input-2 nix path-info -r $outPath | grep input-2
nix ls-store -R -l $outPath | grep foobar nix store ls -R -l $outPath | grep foobar
nix cat-store $outPath/foobar | grep FOOBAR nix store cat $outPath/foobar | grep FOOBAR
# Test --check without hash rewriting. # Test --check without hash rewriting.
nix-build dependencies.nix --no-out-link --check --sandbox-paths /nix/store nix-build dependencies.nix --no-out-link --check --sandbox-paths /nix/store

View file

@ -9,45 +9,45 @@ cd "$TEST_ROOT"
narFile="$TEST_ROOT/path.nar" narFile="$TEST_ROOT/path.nar"
nix-store --dump $storePath > $narFile nix-store --dump $storePath > $narFile
# Check that find and ls-nar match. # Check that find and nar ls match.
( cd $storePath; find . | sort ) > files.find ( cd $storePath; find . | sort ) > files.find
nix ls-nar -R -d $narFile "" | sort > files.ls-nar nix nar ls -R -d $narFile "" | sort > files.ls-nar
diff -u files.find files.ls-nar diff -u files.find files.ls-nar
# Check that file contents of data match. # Check that file contents of data match.
nix cat-nar $narFile /foo/data > data.cat-nar nix nar cat $narFile /foo/data > data.cat-nar
diff -u data.cat-nar $storePath/foo/data diff -u data.cat-nar $storePath/foo/data
# Check that file contents of baz match. # Check that file contents of baz match.
nix cat-nar $narFile /foo/baz > baz.cat-nar nix nar cat $narFile /foo/baz > baz.cat-nar
diff -u baz.cat-nar $storePath/foo/baz diff -u baz.cat-nar $storePath/foo/baz
nix cat-store $storePath/foo/baz > baz.cat-nar nix store cat $storePath/foo/baz > baz.cat-nar
diff -u baz.cat-nar $storePath/foo/baz diff -u baz.cat-nar $storePath/foo/baz
# Test --json. # Test --json.
diff -u \ diff -u \
<(nix ls-nar --json $narFile / | jq -S) \ <(nix nar ls --json $narFile / | jq -S) \
<(echo '{"type":"directory","entries":{"foo":{},"foo-x":{},"qux":{},"zyx":{}}}' | jq -S) <(echo '{"type":"directory","entries":{"foo":{},"foo-x":{},"qux":{},"zyx":{}}}' | jq -S)
diff -u \ diff -u \
<(nix ls-nar --json -R $narFile /foo | jq -S) \ <(nix nar ls --json -R $narFile /foo | jq -S) \
<(echo '{"type":"directory","entries":{"bar":{"type":"regular","size":0,"narOffset":368},"baz":{"type":"regular","size":0,"narOffset":552},"data":{"type":"regular","size":58,"narOffset":736}}}' | jq -S) <(echo '{"type":"directory","entries":{"bar":{"type":"regular","size":0,"narOffset":368},"baz":{"type":"regular","size":0,"narOffset":552},"data":{"type":"regular","size":58,"narOffset":736}}}' | jq -S)
diff -u \ diff -u \
<(nix ls-nar --json -R $narFile /foo/bar | jq -S) \ <(nix nar ls --json -R $narFile /foo/bar | jq -S) \
<(echo '{"type":"regular","size":0,"narOffset":368}' | jq -S) <(echo '{"type":"regular","size":0,"narOffset":368}' | jq -S)
diff -u \ diff -u \
<(nix ls-store --json $storePath | jq -S) \ <(nix store ls --json $storePath | jq -S) \
<(echo '{"type":"directory","entries":{"foo":{},"foo-x":{},"qux":{},"zyx":{}}}' | jq -S) <(echo '{"type":"directory","entries":{"foo":{},"foo-x":{},"qux":{},"zyx":{}}}' | jq -S)
diff -u \ diff -u \
<(nix ls-store --json -R $storePath/foo | jq -S) \ <(nix store ls --json -R $storePath/foo | jq -S) \
<(echo '{"type":"directory","entries":{"bar":{"type":"regular","size":0},"baz":{"type":"regular","size":0},"data":{"type":"regular","size":58}}}' | jq -S) <(echo '{"type":"directory","entries":{"bar":{"type":"regular","size":0},"baz":{"type":"regular","size":0},"data":{"type":"regular","size":58}}}' | jq -S)
diff -u \ diff -u \
<(nix ls-store --json -R $storePath/foo/bar| jq -S) \ <(nix store ls --json -R $storePath/foo/bar| jq -S) \
<(echo '{"type":"regular","size":0}' | jq -S) <(echo '{"type":"regular","size":0}' | jq -S)
# Test missing files. # Test missing files.
nix ls-store --json -R $storePath/xyzzy 2>&1 | grep 'does not exist in NAR' nix store ls --json -R $storePath/xyzzy 2>&1 | grep 'does not exist in NAR'
nix ls-store $storePath/xyzzy 2>&1 | grep 'does not exist' nix store ls $storePath/xyzzy 2>&1 | grep 'does not exist'
# Test failure to dump. # Test failure to dump.
if nix-store --dump $storePath >/dev/full ; then if nix-store --dump $storePath >/dev/full ; then