Skip to content

Stored queries

A stored query names a question about the fleet. r is the eval result. The other formal args are its parameters: required, unless they have a default.

modules/queries.nix
# A stored query names a question. `r` is the eval result; the other args are its parameters.
{ yen, lib, ... }:
{
yen.queries = {
aspectsOn =
{ r, host }:
lib.sort (a: b: a < b) (
lib.filter (a: r.active.${a}."fleet:main/host:${host}" or false) (lib.attrNames r.active)
);
hostsWith =
{
r,
aspect,
kind ? "host",
}:
map (s: s.name) (lib.filter (s: s.kind == kind && r.active.${aspect}.${s.id}) r.scopes);
};
}

r.queries lists them with their parameters:

result
{
aspectsOn = {
optional = [ ];
required = [
"host"
];
};
hostsWith = {
optional = [
"kind"
];
required = [
"aspect"
];
};
}

r.ask name args answers:

result
[
"git"
"nginx"
]

A wrong call is an error record, never a throw: a missing parameter, an unknown one, or an unknown query with near.

result
{
class = "fixable";
code = "missing-param";
error = "query aspectsOn: missing host";
from = "ask";
got = [ ];
path = [ ];
want = [
"host"
];
}
Terminal window
nix eval --impure --json --expr '(import ./query.nix).ask "aspectsOn" { host = "web"; }'
Contribute Community Sponsor