1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-24 22:11:15 +02:00
This commit is contained in:
Jeremy Fleischman 2025-06-21 02:14:07 +01:00 committed by GitHub
commit 4bd0beb001
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 59 additions and 6 deletions

View file

@ -1,3 +1,4 @@
#include "nix/expr/value.hh"
#include "nix/util/users.hh"
#include "nix/expr/eval-cache.hh"
#include "nix/store/sqlite.hh"
@ -762,6 +763,14 @@ std::vector<Symbol> AttrCursor::getAttrs()
return attrs;
}
bool AttrCursor::isNull()
{
// <<< TODO: caching? >>>
auto & v = forceValue();
return v.type() == nNull;
}
bool AttrCursor::isDerivation()
{
auto aType = maybeGetAttr("type");

View file

@ -153,6 +153,8 @@ public:
bool isDerivation();
bool isNull();
Value & forceValue();
/**

View file

@ -1340,10 +1340,14 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
}
} else {
try {
if (visitor.isDerivation())
showDerivation();
else
throw Error("expected a derivation");
if (visitor.isNull()) {
j = nullptr;
} else{
if (visitor.isDerivation())
showDerivation();
else
throw Error("expected a derivation");
}
} catch (IFDError & e) {
if (!json) {
logger->cout(fmt("%s " ANSI_WARNING "omitted due to use of import from derivation" ANSI_NORMAL, headerPrefix));

View file

@ -16,6 +16,7 @@ writeSimpleFlake() {
foo = import ./simple.nix;
fooScript = (import ./shell.nix {}).foo;
default = foo;
noPackage = null;
};
packages.someOtherSystem = rec {
foo = import ./simple.nix;

View file

@ -53,7 +53,7 @@ cat >flake.nix <<EOF
packages.$system = { };
packages.someOtherSystem = { };
formatter = { };
formatter.x86_64-linux = null;
nixosConfigurations = { };
nixosModules = { };
};
@ -63,7 +63,7 @@ nix flake show --json --all-systems > show-output.json
nix eval --impure --expr '
let show_output = builtins.fromJSON (builtins.readFile ./show-output.json);
in
assert show_output == { };
assert show_output == { formatter = { x86_64-linux = null; }; };
true
'

View file

@ -85,3 +85,40 @@ rm ./my-result
# Flake outputs check.
nix flake check
nix flake show | grep -P "package 'formatter'"
function expectFailWithOutputMatching() {
outputMustMatch=$1
if output=$(nix fmt 2>&1); then
echo >&2 "nix fmt unexpectedly succeeded"
exit 1
fi
if ! echo "$output" | grep "$outputMustMatch"; then
echo >&2 "Expected nix fmt output to match:"
echo >&2 "$outputMustMatch"
echo >&2 "However, the actual output was:"
echo >&2 "$output"
exit 1
fi
}
# Try a flake with no formatter.
cat << EOF > flake.nix
{
outputs = _: {};
}
EOF
expectFailWithOutputMatching "does not provide attribute 'formatter.$system'"
# Confirm that a null formatter is treated as if there is no formatter.
cat << EOF > flake.nix
{
outputs = _: {
formatter.$system = null;
};
}
EOF
if nix fmt | grep "does not provide attribute 'formatter.$system'"; then
echo >&2 "nix fmt unexpectedly succeeded"
exit 1
fi