mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-10 00:08:07 +02:00
Move 'nix hash-*' and 'nix to-*' to 'nix hash'
From the 'nix' UX review.
This commit is contained in:
parent
8ad2c9c4b9
commit
b2d6c6161e
6 changed files with 51 additions and 27 deletions
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
using namespace nix;
|
using namespace nix;
|
||||||
|
|
||||||
struct CmdHash : Command
|
struct CmdHashBase : Command
|
||||||
{
|
{
|
||||||
FileIngestionMethod mode;
|
FileIngestionMethod mode;
|
||||||
Base base = SRI;
|
Base base = SRI;
|
||||||
|
@ -17,7 +17,7 @@ struct CmdHash : Command
|
||||||
std::vector<std::string> paths;
|
std::vector<std::string> paths;
|
||||||
std::optional<std::string> modulus;
|
std::optional<std::string> modulus;
|
||||||
|
|
||||||
CmdHash(FileIngestionMethod mode) : mode(mode)
|
CmdHashBase(FileIngestionMethod mode) : mode(mode)
|
||||||
{
|
{
|
||||||
mkFlag(0, "sri", "print hash in SRI format", &base, SRI);
|
mkFlag(0, "sri", "print hash in SRI format", &base, SRI);
|
||||||
mkFlag(0, "base64", "print hash in base-64", &base, Base64);
|
mkFlag(0, "base64", "print hash in base-64", &base, Base64);
|
||||||
|
@ -51,8 +51,6 @@ struct CmdHash : Command
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
Category category() override { return catUtility; }
|
|
||||||
|
|
||||||
void run() override
|
void run() override
|
||||||
{
|
{
|
||||||
for (auto path : paths) {
|
for (auto path : paths) {
|
||||||
|
@ -79,9 +77,6 @@ struct CmdHash : Command
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static RegisterCommand rCmdHashFile("hash-file", [](){ return make_ref<CmdHash>(FileIngestionMethod::Flat); });
|
|
||||||
static RegisterCommand rCmdHashPath("hash-path", [](){ return make_ref<CmdHash>(FileIngestionMethod::Recursive); });
|
|
||||||
|
|
||||||
struct CmdToBase : Command
|
struct CmdToBase : Command
|
||||||
{
|
{
|
||||||
Base base;
|
Base base;
|
||||||
|
@ -103,8 +98,6 @@ struct CmdToBase : Command
|
||||||
"SRI");
|
"SRI");
|
||||||
}
|
}
|
||||||
|
|
||||||
Category category() override { return catUtility; }
|
|
||||||
|
|
||||||
void run() override
|
void run() override
|
||||||
{
|
{
|
||||||
for (auto s : args)
|
for (auto s : args)
|
||||||
|
@ -112,10 +105,41 @@ struct CmdToBase : Command
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static RegisterCommand rCmdToBase16("to-base16", [](){ return make_ref<CmdToBase>(Base16); });
|
struct CmdHash : NixMultiCommand
|
||||||
static RegisterCommand rCmdToBase32("to-base32", [](){ return make_ref<CmdToBase>(Base32); });
|
{
|
||||||
static RegisterCommand rCmdToBase64("to-base64", [](){ return make_ref<CmdToBase>(Base64); });
|
CmdHash()
|
||||||
static RegisterCommand rCmdToSRI("to-sri", [](){ return make_ref<CmdToBase>(SRI); });
|
: MultiCommand({
|
||||||
|
{"file", []() { return make_ref<CmdHashBase>(FileIngestionMethod::Flat);; }},
|
||||||
|
{"path", []() { return make_ref<CmdHashBase>(FileIngestionMethod::Recursive); }},
|
||||||
|
{"to-base16", []() { return make_ref<CmdToBase>(Base16); }},
|
||||||
|
{"to-base32", []() { return make_ref<CmdToBase>(Base32); }},
|
||||||
|
{"to-base64", []() { return make_ref<CmdToBase>(Base64); }},
|
||||||
|
{"to-sri", []() { return make_ref<CmdToBase>(SRI); }},
|
||||||
|
})
|
||||||
|
{ }
|
||||||
|
|
||||||
|
std::string description() override
|
||||||
|
{
|
||||||
|
return "compute and convert cryptographic hashes";
|
||||||
|
}
|
||||||
|
|
||||||
|
Category category() override { return catUtility; }
|
||||||
|
|
||||||
|
void run() override
|
||||||
|
{
|
||||||
|
if (!command)
|
||||||
|
throw UsageError("'nix hash' 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 rCmdHash = registerCommand<CmdHash>("hash");
|
||||||
|
|
||||||
/* Legacy nix-hash command. */
|
/* Legacy nix-hash command. */
|
||||||
static int compatNixHash(int argc, char * * argv)
|
static int compatNixHash(int argc, char * * argv)
|
||||||
|
@ -149,7 +173,7 @@ static int compatNixHash(int argc, char * * argv)
|
||||||
});
|
});
|
||||||
|
|
||||||
if (op == opHash) {
|
if (op == opHash) {
|
||||||
CmdHash cmd(flat ? FileIngestionMethod::Flat : FileIngestionMethod::Recursive);
|
CmdHashBase cmd(flat ? FileIngestionMethod::Flat : FileIngestionMethod::Recursive);
|
||||||
cmd.ht = ht;
|
cmd.ht = ht;
|
||||||
cmd.base = base32 ? Base32 : Base16;
|
cmd.base = base32 ? Base32 : Base16;
|
||||||
cmd.truncate = truncate;
|
cmd.truncate = truncate;
|
||||||
|
|
|
@ -9,13 +9,13 @@ outPath=$(nix-build dependencies.nix --no-out-link)
|
||||||
|
|
||||||
nix copy --to $cacheURI $outPath
|
nix copy --to $cacheURI $outPath
|
||||||
|
|
||||||
HASH=$(nix hash-path $outPath)
|
HASH=$(nix hash path $outPath)
|
||||||
|
|
||||||
clearStore
|
clearStore
|
||||||
clearCacheCache
|
clearCacheCache
|
||||||
|
|
||||||
nix copy --from $cacheURI $outPath --no-check-sigs
|
nix copy --from $cacheURI $outPath --no-check-sigs
|
||||||
|
|
||||||
HASH2=$(nix hash-path $outPath)
|
HASH2=$(nix hash path $outPath)
|
||||||
|
|
||||||
[[ $HASH = $HASH2 ]]
|
[[ $HASH = $HASH2 ]]
|
||||||
|
|
|
@ -12,7 +12,7 @@ cmp $outPath fetchurl.sh
|
||||||
# Now using a base-64 hash.
|
# Now using a base-64 hash.
|
||||||
clearStore
|
clearStore
|
||||||
|
|
||||||
hash=$(nix hash-file --type sha512 --base64 ./fetchurl.sh)
|
hash=$(nix hash file --type sha512 --base64 ./fetchurl.sh)
|
||||||
|
|
||||||
outPath=$(nix-build '<nix/fetchurl.nix>' --argstr url file://$(pwd)/fetchurl.sh --argstr sha512 $hash --no-out-link)
|
outPath=$(nix-build '<nix/fetchurl.nix>' --argstr url file://$(pwd)/fetchurl.sh --argstr sha512 $hash --no-out-link)
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ cmp $outPath fetchurl.sh
|
||||||
# Now using an SRI hash.
|
# Now using an SRI hash.
|
||||||
clearStore
|
clearStore
|
||||||
|
|
||||||
hash=$(nix hash-file ./fetchurl.sh)
|
hash=$(nix hash file ./fetchurl.sh)
|
||||||
|
|
||||||
[[ $hash =~ ^sha256- ]]
|
[[ $hash =~ ^sha256- ]]
|
||||||
|
|
||||||
|
@ -34,14 +34,14 @@ clearStore
|
||||||
|
|
||||||
other_store=file://$TEST_ROOT/other_store?store=/fnord/store
|
other_store=file://$TEST_ROOT/other_store?store=/fnord/store
|
||||||
|
|
||||||
hash=$(nix hash-file --type sha256 --base16 ./fetchurl.sh)
|
hash=$(nix hash file --type sha256 --base16 ./fetchurl.sh)
|
||||||
|
|
||||||
storePath=$(nix --store $other_store add-to-store --flat ./fetchurl.sh)
|
storePath=$(nix --store $other_store add-to-store --flat ./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)
|
outPath=$(nix-build '<nix/fetchurl.nix>' --argstr url file:///no-such-dir/fetchurl.sh --argstr sha256 $hash --no-out-link --substituters $other_store)
|
||||||
|
|
||||||
# Test hashed mirrors with an SRI hash.
|
# Test hashed mirrors with an SRI hash.
|
||||||
nix-build '<nix/fetchurl.nix>' --argstr url file:///no-such-dir/fetchurl.sh --argstr hash $(nix to-sri --type sha256 $hash) \
|
nix-build '<nix/fetchurl.nix>' --argstr url file:///no-such-dir/fetchurl.sh --argstr hash $(nix hash to-sri --type sha256 $hash) \
|
||||||
--no-out-link --substituters $other_store
|
--no-out-link --substituters $other_store
|
||||||
|
|
||||||
# Test unpacking a NAR.
|
# Test unpacking a NAR.
|
||||||
|
|
|
@ -2,7 +2,7 @@ source common.sh
|
||||||
|
|
||||||
try () {
|
try () {
|
||||||
printf "%s" "$2" > $TEST_ROOT/vector
|
printf "%s" "$2" > $TEST_ROOT/vector
|
||||||
hash=$(nix hash-file --base16 $EXTRA --type "$1" $TEST_ROOT/vector)
|
hash=$(nix hash file --base16 $EXTRA --type "$1" $TEST_ROOT/vector)
|
||||||
if test "$hash" != "$3"; then
|
if test "$hash" != "$3"; then
|
||||||
echo "hash $1, expected $3, got $hash"
|
echo "hash $1, expected $3, got $hash"
|
||||||
exit 1
|
exit 1
|
||||||
|
@ -69,17 +69,17 @@ try2 md5 "f78b733a68f5edbdf9413899339eaa4a"
|
||||||
|
|
||||||
# Conversion.
|
# Conversion.
|
||||||
try3() {
|
try3() {
|
||||||
h64=$(nix to-base64 --type "$1" "$2")
|
h64=$(nix hash to-base64 --type "$1" "$2")
|
||||||
[ "$h64" = "$4" ]
|
[ "$h64" = "$4" ]
|
||||||
sri=$(nix to-sri --type "$1" "$2")
|
sri=$(nix hash to-sri --type "$1" "$2")
|
||||||
[ "$sri" = "$1-$4" ]
|
[ "$sri" = "$1-$4" ]
|
||||||
h32=$(nix-hash --type "$1" --to-base32 "$2")
|
h32=$(nix-hash --type "$1" --to-base32 "$2")
|
||||||
[ "$h32" = "$3" ]
|
[ "$h32" = "$3" ]
|
||||||
h16=$(nix-hash --type "$1" --to-base16 "$h32")
|
h16=$(nix-hash --type "$1" --to-base16 "$h32")
|
||||||
[ "$h16" = "$2" ]
|
[ "$h16" = "$2" ]
|
||||||
h16=$(nix to-base16 --type "$1" "$h64")
|
h16=$(nix hash to-base16 --type "$1" "$h64")
|
||||||
[ "$h16" = "$2" ]
|
[ "$h16" = "$2" ]
|
||||||
h16=$(nix to-base16 "$sri")
|
h16=$(nix hash to-base16 "$sri")
|
||||||
[ "$h16" = "$2" ]
|
[ "$h16" = "$2" ]
|
||||||
}
|
}
|
||||||
try3 sha1 "800d59cfcd3c05e900cb4e214be48f6b886a08df" "vw46m23bizj4n8afrc0fj19wrp7mj3c0" "gA1Zz808BekAy04hS+SPa4hqCN8="
|
try3 sha1 "800d59cfcd3c05e900cb4e214be48f6b886a08df" "vw46m23bizj4n8afrc0fj19wrp7mj3c0" "gA1Zz808BekAy04hS+SPa4hqCN8="
|
||||||
|
|
|
@ -15,7 +15,7 @@ nix eval --expr 'assert 1 + 2 == 3; true'
|
||||||
|
|
||||||
[[ $(nix eval --impure --expr "(import (builtins.fetchurl { url = file://$(pwd)/pure-eval.nix; })).x") == 123 ]]
|
[[ $(nix eval --impure --expr "(import (builtins.fetchurl { url = file://$(pwd)/pure-eval.nix; })).x") == 123 ]]
|
||||||
(! nix eval --expr "(import (builtins.fetchurl { url = file://$(pwd)/pure-eval.nix; })).x")
|
(! nix eval --expr "(import (builtins.fetchurl { url = file://$(pwd)/pure-eval.nix; })).x")
|
||||||
nix eval --expr "(import (builtins.fetchurl { url = file://$(pwd)/pure-eval.nix; sha256 = \"$(nix hash-file pure-eval.nix --type sha256)\"; })).x"
|
nix eval --expr "(import (builtins.fetchurl { url = file://$(pwd)/pure-eval.nix; sha256 = \"$(nix hash file pure-eval.nix --type sha256)\"; })).x"
|
||||||
|
|
||||||
rm -rf $TEST_ROOT/eval-out
|
rm -rf $TEST_ROOT/eval-out
|
||||||
nix eval --store dummy:// --write-to $TEST_ROOT/eval-out --expr '{ x = "foo" + "bar"; y = { z = "bla"; }; }'
|
nix eval --store dummy:// --write-to $TEST_ROOT/eval-out --expr '{ x = "foo" + "bar"; y = { z = "bla"; }; }'
|
||||||
|
|
|
@ -10,7 +10,7 @@ mkdir -p $tarroot
|
||||||
cp dependencies.nix $tarroot/default.nix
|
cp dependencies.nix $tarroot/default.nix
|
||||||
cp config.nix dependencies.builder*.sh $tarroot/
|
cp config.nix dependencies.builder*.sh $tarroot/
|
||||||
|
|
||||||
hash=$(nix hash-path $tarroot)
|
hash=$(nix hash path $tarroot)
|
||||||
|
|
||||||
test_tarball() {
|
test_tarball() {
|
||||||
local ext="$1"
|
local ext="$1"
|
||||||
|
|
Loading…
Reference in a new issue