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
53 changes: 42 additions & 11 deletions src/nix/flake.cc
Original file line number Diff line number Diff line change
Expand Up @@ -437,14 +437,39 @@ struct CmdFlakeCheck : FlakeCommand

auto checkApp = [&](const std::string & attrPath, Value & v, const PosIdx pos) {
try {
#if 0
// FIXME
auto app = App(*state, v);
for (auto & i : app.context) {
auto [drvPathS, outputName] = NixStringContextElem::parse(i);
store->parseStorePath(drvPathS);
Activity act(*logger, lvlInfo, actUnknown, fmt("checking app '%s'", attrPath));
state->forceAttrs(v, pos, "");
if (auto attr = v.attrs()->get(state->symbols.create("type")))
state->forceStringNoCtx(*attr->value, attr->pos, "");
else
throw Error("app '%s' lacks attribute 'type'", attrPath);

if (auto attr = v.attrs()->get(state->symbols.create("program"))) {
if (attr->name == state->symbols.create("program")) {
NixStringContext context;
state->forceString(*attr->value, context, attr->pos, "");
}
} else
throw Error("app '%s' lacks attribute 'program'", attrPath);

if (auto attr = v.attrs()->get(state->symbols.create("meta"))) {
state->forceAttrs(*attr->value, attr->pos, "");
if (auto dAttr = attr->value->attrs()->get(state->symbols.create("description")))
state->forceStringNoCtx(*dAttr->value, dAttr->pos, "");
else
logWarning({
.msg = HintFmt("app '%s' lacks attribute 'meta.description'", attrPath),
});
} else
logWarning({
.msg = HintFmt("app '%s' lacks attribute 'meta'", attrPath),
});

for (auto & attr : *v.attrs()) {
std::string_view name(state->symbols[attr.name]);
if (name != "type" && name != "program" && name != "meta")
throw Error("app '%s' has unsupported attribute '%s'", attrPath, name);
}
#endif
} catch (Error & e) {
e.addTrace(resolve(pos), HintFmt("while checking the app definition '%s'", attrPath));
reportError(e);
Expand Down Expand Up @@ -629,7 +654,7 @@ struct CmdFlakeCheck : FlakeCommand
const auto & attr_name = state->symbols[attr.name];
checkSystemName(attr_name, attr.pos);
if (checkSystemType(attr_name, attr.pos)) {
checkApp(
checkDerivation(
fmt("%s.%s", name, attr_name),
*attr.value, attr.pos);
};
Expand Down Expand Up @@ -1251,8 +1276,7 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
}
j.emplace("type", "derivation");
j.emplace("name", name);
if (description)
j.emplace("description", *description);
j.emplace("description", description ? *description : "");
} else {
logger->cout("%s: %s '%s'",
headerPrefix,
Expand Down Expand Up @@ -1340,12 +1364,19 @@ struct CmdFlakeShow : FlakeCommand, MixJSON
(attrPath.size() == 3 && attrPathS[0] == "apps"))
{
auto aType = visitor.maybeGetAttr("type");
std::optional<std::string> description;
if (auto aMeta = visitor.maybeGetAttr(state->sMeta)) {
if (auto aDescription = aMeta->maybeGetAttr(state->sDescription))
description = aDescription->getString();
}
if (!aType || aType->getString() != "app")
state->error<EvalError>("not an app definition").debugThrow();
if (json) {
j.emplace("type", "app");
if (description)
j.emplace("description", *description);
} else {
logger->cout("%s: app", headerPrefix);
logger->cout("%s: app: " ANSI_BOLD "%s" ANSI_NORMAL, headerPrefix, description ? *description : "no description");
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/nix/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ An app is specified by a flake output attribute named
apps.x86_64-linux.blender_2_79 = {
type = "app";
program = "${self.packages.x86_64-linux.blender_2_79}/bin/blender";
meta.description = "Run Blender, a free and open-source 3D creation suite.";
};
```

Expand All @@ -90,4 +91,6 @@ The only supported attributes are:
* `program` (required): The full path of the executable to run. It
must reside in the Nix store.

* `meta.description` (optional): A description of the app.

)""
44 changes: 44 additions & 0 deletions tests/functional/flakes/check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,47 @@ nix flake check $flakeDir
checkRes=$(nix flake check --all-systems --keep-going $flakeDir 2>&1 && fail "nix flake check --all-systems should have failed" || true)
echo "$checkRes" | grepQuiet "packages.system-1.default"
echo "$checkRes" | grepQuiet "packages.system-2.default"

cat > $flakeDir/flake.nix <<EOF
{
outputs = { self }: {
apps.system-1.default = {
type = "app";
program = "foo";
};
apps.system-2.default = {
type = "app";
program = "bar";
meta.description = "baz";
};
};
}
EOF

nix flake check --all-systems $flakeDir

cat > $flakeDir/flake.nix <<EOF
{
outputs = { self }: {
apps.system-1.default = {
type = "app";
program = "foo";
unknown-attr = "bar";
};
};
}
EOF

checkRes=$(nix flake check --all-systems $flakeDir 2>&1 && fail "nix flake check --all-systems should have failed" || true)
echo "$checkRes" | grepQuiet "unknown-attr"

cat > $flakeDir/flake.nix <<EOF
{
outputs = { self }: {
formatter.system-1 = "foo";
};
}
EOF

checkRes=$(nix flake check --all-systems $flakeDir 2>&1 && fail "nix flake check --all-systems should have failed" || true)
echo "$checkRes" | grepQuiet "formatter.system-1"