Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/libexpr/eval-cache.cc
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -762,6 +763,14 @@ std::vector<Symbol> AttrCursor::getAttrs()
return attrs;
}

bool AttrCursor::isNull()
{
// <<< TODO: caching? >>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's surprising, but yeah, null isn't supported yet.

enum AttrType could be extended.
Not sure if that should be considered a breaking schema change, but it'd be a safe choice to then also update the version in the file name "/eval-cache-v5" to v6.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give that a shot! I see some type related checks in the other caching codepaths. Do I need to worry about the fact that the attribute I'm caching could change from a derivation to null and back?

auto & v = forceValue();

return v.type() == nNull;
}

bool AttrCursor::isDerivation()
{
auto aType = maybeGetAttr("type");
Expand Down
2 changes: 2 additions & 0 deletions src/libexpr/include/nix/expr/eval-cache.hh
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public:

bool isDerivation();

bool isNull();

Value & forceValue();

/**
Expand Down
12 changes: 8 additions & 4 deletions src/nix/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions tests/functional/flakes/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/flakes/show.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ cat >flake.nix <<EOF
packages.$system = { };
packages.someOtherSystem = { };

formatter = { };
formatter.x86_64-linux = null;
nixosConfigurations = { };
nixosModules = { };
};
Expand All @@ -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
'

Expand Down
37 changes: 37 additions & 0 deletions tests/functional/formatter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading