Add support for recurseForDerivations

This will respect `recurseForDerivations` when iterating over attrsets.

Example expression:
``` nix
{ system ? builtins.currentSystem }:
{
  recurseForDerivations = true;

  # This should build as it's in the top-level attrset
  drvA = derivation {
    inherit system;
    name = "drvA";
    builder = ":";
  };

  dontRecurse = {
    # This shouldn't build as `recurseForDerivations = true;` is not set
    # recurseForDerivations = true;

    # This should not build
    drvB = derivation {
      inherit system;
      name = "drvA";
      builder = ":";
    };
  };

  recurse = {
    # This should build
    recurseForDerivations = true;

    # This should not build
    drvC = derivation {
      inherit system;
      name = "drvC";
      builder = ":";
    };
  };

}
```
This commit is contained in:
adisbladis 2022-04-25 21:23:06 +12:00
parent e303b2e43f
commit c1bbb11c5d
3 changed files with 47 additions and 10 deletions

View file

@ -281,7 +281,8 @@ static void worker(
else if (v->type() == nAttrs) else if (v->type() == nAttrs)
{ {
auto attrs = nlohmann::json::array(); auto attrs = nlohmann::json::array();
StringSet ss; bool recurse = attrPath == ""; // Dont require `recurseForDerivations = true;` for top-level attrset
for (auto & i : v->attrs->lexicographicOrder()) { for (auto & i : v->attrs->lexicographicOrder()) {
std::string name(i->name); std::string name(i->name);
if (name.find('.') != std::string::npos || name.find(' ') != std::string::npos) { if (name.find('.') != std::string::npos || name.find(' ') != std::string::npos) {
@ -289,8 +290,16 @@ static void worker(
continue; continue;
} }
attrs.push_back(name); attrs.push_back(name);
if (name == "recurseForDerivations") {
auto attrv = v->attrs->get(state.sRecurseForDerivations);
recurse = state.forceBool(*attrv->value, *attrv->pos);
}
} }
reply["attrs"] = std::move(attrs); if (recurse)
reply["attrs"] = std::move(attrs);
else
reply["attrs"] = nlohmann::json::array();
} }
else if (v->type() == nNull) else if (v->type() == nNull)

View file

@ -1,9 +1,34 @@
{ pkgs ? import (builtins.getFlake (toString ./.)).inputs.nixpkgs { } }: {
pkgs ? import (builtins.getFlake (toString ./.)).inputs.nixpkgs { }
, system ? pkgs.system
}:
{ {
builtJob = pkgs.writeText "job1" "job1"; builtJob = pkgs.writeText "job1" "job1";
substitutedJob = pkgs.hello; substitutedJob = pkgs.hello;
nested = {
job = pkgs.hello; dontRecurse = {
# This shouldn't build as `recurseForDerivations = true;` is not set
# recurseForDerivations = true;
# This should not build
drvB = derivation {
inherit system;
name = "drvA";
builder = ":";
};
}; };
recurse = {
# This should build
recurseForDerivations = true;
# This should not build
drvB = derivation {
inherit system;
name = "drvB";
builder = ":";
};
};
} }

View file

@ -23,7 +23,7 @@ def common_test(extra_args: List[str]) -> None:
) )
results = [json.loads(r) for r in res.stdout.split("\n") if r] results = [json.loads(r) for r in res.stdout.split("\n") if r]
assert len(results) == 3 assert len(results) == 4
built_job = results[0] built_job = results[0]
assert built_job["attr"] == "builtJob" assert built_job["attr"] == "builtJob"
@ -32,11 +32,14 @@ def common_test(extra_args: List[str]) -> None:
assert built_job["drvPath"].endswith(".drv") assert built_job["drvPath"].endswith(".drv")
assert built_job["meta"]['broken'] is False assert built_job["meta"]['broken'] is False
nested_job = results[1] recurse_drv = results[1]
assert nested_job["attr"] == "nested.job" assert recurse_drv["attr"] == "recurse.drvB"
assert nested_job["name"].startswith("hello-") assert recurse_drv["name"] == "drvB"
substituted_job = results[2] recurse_recurse_bool = results[2]
assert "error" in recurse_recurse_bool
substituted_job = results[3]
assert substituted_job["attr"] == "substitutedJob" assert substituted_job["attr"] == "substitutedJob"
assert substituted_job["name"].startswith("hello-") assert substituted_job["name"].startswith("hello-")
assert substituted_job["meta"]['broken'] is False assert substituted_job["meta"]['broken'] is False