Skip to content
Merged
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
39 changes: 34 additions & 5 deletions src/nix/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ struct CmdFlakeCheck : FlakeCommand
throw;
} catch (Error & e) {
if (settings.keepGoing) {
ignoreExceptionExceptInterrupt();
logError(e.info());
hasErrors = true;
} else
throw;
Expand Down Expand Up @@ -423,7 +423,7 @@ struct CmdFlakeCheck : FlakeCommand
return std::nullopt;
};

std::vector<DerivedPath> drvPaths;
std::map<DerivedPath, std::vector<AttrPath>> attrPathsByDrv;

auto checkApp = [&](const std::string & attrPath, Value & v, const PosIdx pos) {
try {
Expand Down Expand Up @@ -621,7 +621,13 @@ struct CmdFlakeCheck : FlakeCommand
.drvPath = makeConstantStorePathRef(*drvPath),
.outputs = OutputsSpec::All{},
};
drvPaths.push_back(std::move(path));

// Build and store the attribute path for error reporting
AttrPath attrPath;
attrPath.push_back(AttrName(state->symbols.create(name)));
attrPath.push_back(AttrName(attr.name));
attrPath.push_back(AttrName(attr2.name));
attrPathsByDrv[path].push_back(std::move(attrPath));
}
}
}
Expand Down Expand Up @@ -785,7 +791,9 @@ struct CmdFlakeCheck : FlakeCommand
});
}

if (build && !drvPaths.empty()) {
if (build && !attrPathsByDrv.empty()) {
auto keys = std::views::keys(attrPathsByDrv);
std::vector<DerivedPath> drvPaths(keys.begin(), keys.end());
// TODO: This filtering of substitutable paths is a temporary workaround until
// https://github.com/NixOS/nix/issues/5025 (union stores) is implemented.
//
Expand All @@ -811,7 +819,28 @@ struct CmdFlakeCheck : FlakeCommand
}

Activity act(*logger, lvlInfo, actUnknown, fmt("running %d flake checks", toBuild.size()));
store->buildPaths(toBuild);
auto results = store->buildPathsWithResults(toBuild);

// Report build failures with attribute paths
for (auto & result : results) {
if (auto * failure = result.tryGetFailure()) {
auto it = attrPathsByDrv.find(result.path);
if (it != attrPathsByDrv.end() && !it->second.empty()) {
for (auto & attrPath : it->second) {
auto attrPathStr = showAttrPath(state->symbols, attrPath);
reportError(Error(
"failed to build attribute '%s', build of '%s' failed: %s",
attrPathStr,
result.path.to_string(*store),
failure->errorMsg));
Comment on lines +831 to +835
Copy link
Member Author

@roberth roberth Oct 22, 2025

Choose a reason for hiding this comment

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

Not a blocker, idea:
Suggest nix check <flakeref>.<attrPathStr>

}
} else {
// Derivation has no attribute path (e.g., a build dependency)
reportError(
Error("build of '%s' failed: %s", result.path.to_string(*store), failure->errorMsg));
}
}
}
}
if (hasErrors)
throw Error("some errors were encountered during the evaluation");
Expand Down
21 changes: 21 additions & 0 deletions tests/functional/flakes/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,24 @@ EOF
# shellcheck disable=SC2015
checkRes=$(nix flake check "$flakeDir" 2>&1 && fail "nix flake check should have failed" || true)
echo "$checkRes" | grepQuiet -E "builder( for .*)? failed with exit code 1"

# Test that attribute paths are shown in error messages
cat > "$flakeDir"/flake.nix <<EOF
{
outputs = { self }: with import ./config.nix; {
checks.${system}.failingCheck = mkDerivation {
name = "failing-check";
buildCommand = "echo 'This check fails'; exit 1";
};
checks.${system}.anotherFailingCheck = mkDerivation {
name = "another-failing-check";
buildCommand = "echo 'This also fails'; exit 1";
};
};
}
EOF

# shellcheck disable=SC2015
checkRes=$(nix flake check --keep-going "$flakeDir" 2>&1 && fail "nix flake check should have failed" || true)
echo "$checkRes" | grepQuiet "checks.${system}.failingCheck"
echo "$checkRes" | grepQuiet "checks.${system}.anotherFailingCheck"
Loading