Skip to content

Comments

lib.attrsets.recurse: init#387760

Draft
tie wants to merge 4 commits intoNixOS:masterfrom
tie:attrs-recursive-cond
Draft

lib.attrsets.recurse: init#387760
tie wants to merge 4 commits intoNixOS:masterfrom
tie:attrs-recursive-cond

Conversation

@tie
Copy link
Member

@tie tie commented Mar 6, 2025

This PR adds lib.attrsets.recurse function (based on mapAttrsRecursiveCond) that performs graph traversal on nested attribute sets and allows converting other functions like mapAttrs, concatMapAttrs, mapAttrsToList, etc, to recursive versions.

It also updates mapAttrsRecursiveCond and filterAttrsRecursive implementations to use recurse. In particular, this allows reusing filterAttrs for recursive implementation and should benefit from #345547.

See also #237776

Supersedes #221608 (lib.{collect',flattenAttrs} can be implemented using attrsets.recurse). E.g.

lib.attrsets.recurse lib.concatMapAttrs
  (_: v: v != { })
  (p: v: { ${lib.concatStringsSep "/" p} = v; })
  { a = 1; b.c.d = 2; x.y = { }; }{ a = 1; "b/c/d" = 2; "x/y" = { }; }
concatMapAttrsToList = f: attrs: builtins.concatMap (name: f name attrs.${name}) (builtins.attrNames attrs)
lib.attrsets.recurse concatMapAttrsToList
  (_: v: v != { })
  (path: value: [{ inherit path value; }])
  { a = 1; b.c.d = 2; x.y = { }; }[
    { path = [ "a" ]; value = 1; }
    { path = [ "b" "c" "d" ]; value = 2; }
    { path = [ "x" "y" ]; value = { }; }
  ]

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 25.05 Release Notes (or backporting 24.11 and 25.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@tie tie requested review from h7x4, hsjobeki, infinisil and roberth March 6, 2025 23:45
@github-actions github-actions bot added 6.topic: lib The Nixpkgs function library 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Mar 6, 2025
@tie
Copy link
Member Author

tie commented Mar 7, 2025

I couldn’t think of a better name than recurse, but I don’t think it’s ideal.

Also, this can probably be extended to support any type that fAttrs can accept if we don’t hardcode isAttrs condition for isBranch. Perhaps as a lib.graphs.recurse? E.g.

lib.graphs.recurse
  (f: v: if isAttrs v then mapAttrs f v else if isList v then imap0 f v else)
  (_: v: isList v && v != [ ] || isAttrs v && v != { } ||)
  (_: v: v)
  [{ foo = [ "bar" ]; baz = [ ]; qux = { }; }]

There is also Nix code that uses builtins.tryEval before isAttrs for isBranch function,

safeMapAttrsRecursiveCond =
cond: f: set:
let
recurse =
path:
lib.mapAttrs (
name: value:
let
e = builtins.tryEval value;
path' = path ++ [ name ];
in
if e.success && lib.isAttrs value && cond value then recurse path' value else f path' value
);
in
recurse [ ] set;

@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Apr 2, 2025
@nixpkgs-ci nixpkgs-ci bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Sep 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: merge conflict This PR has merge conflicts with the target branch 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: lib The Nixpkgs function library 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants