Skip to content

lib/generators: withRecursion: don't break attr-sets with special attrs#168374

Merged
infinisil merged 1 commit intoNixOS:masterfrom
Ma27:special-attrs-in-with-recursion
May 23, 2022
Merged

lib/generators: withRecursion: don't break attr-sets with special attrs#168374
infinisil merged 1 commit intoNixOS:masterfrom
Ma27:special-attrs-in-with-recursion

Conversation

@Ma27
Copy link
Member

@Ma27 Ma27 commented Apr 12, 2022

Description of changes

Note: This is an alternative implementation for #168327. Please also read the discussion about the pros and cons of this vs the other PRs in #168327 before deciding which patch is preferable.

cc @Profpatsch @K900 @infinisil @lheckemann


Closes #168327

The issue reported there can be demonstrated with the following
expression:

→ nix-instantiate --eval -E "with import ./. {}; pkgs.lib.options.showDefs [ { file = \"foo\"; value = pkgs.rust.packages.stable.buildRustPackages; } ]"
error: attempt to call something which is not a function but a string

       at /home/ma27/Projects/nixpkgs/lib/trivial.nix:442:35:

          441|   isFunction = f: builtins.isFunction f ||
          442|     (f ? __functor && isFunction (f.__functor f));
             |                                   ^
          443|

Basically, if a __functor is in an attribute-set at depth-limit,
__functor will be set to "<unevaluated>". This however breaks
lib.isFunction which checks for a __functor by invoking __functor
with f itself.

The same issue - "magic" attributes being shadowed by withRecursion -
also applies to others such as
__pretty/__functionArgs/__toString.

Since these attributes have a low-risk of causing a stack overflow
(because these are flat attr-sets or even functions), ignoring them in
withRecursion seems like a valid solution.

Things done
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • 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/)
  • 22.05 Release Notes (or backporting 21.11 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
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

Closes NixOS#168327

The issue reported there can be demonstrated with the following
expression:

    → nix-instantiate --eval -E "with import ./. {}; pkgs.lib.options.showDefs [ { file = \"foo\"; value = pkgs.rust.packages.stable.buildRustPackages; } ]"
    error: attempt to call something which is not a function but a string

           at /home/ma27/Projects/nixpkgs/lib/trivial.nix:442:35:

              441|   isFunction = f: builtins.isFunction f ||
              442|     (f ? __functor && isFunction (f.__functor f));
                 |                                   ^
              443|

Basically, if a `__functor` is in an attribute-set at depth-limit,
`__functor` will be set to `"<unevaluated>"`. This however breaks
`lib.isFunction` which checks for a `__functor` by invoking `__functor`
with `f` itself.

The same issue - "magic" attributes being shadowed by `withRecursion` -
also applies to others such as
`__pretty`/`__functionArgs`/`__toString`.

Since these attributes have a low-risk of causing a stack overflow
(because these are flat attr-sets or even functions), ignoring them in
`withRecursion` seems like a valid solution.
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Apr 12, 2022
@infinisil infinisil merged commit 4b2827e into NixOS:master May 23, 2022
@Ma27 Ma27 deleted the special-attrs-in-with-recursion branch May 23, 2022 16:10
@Profpatsch
Copy link
Member

For the record, I’m not happy with the direction the generators are taking.

Stuff like limiting recursion depth just does not belong into these functions, it should be a separate mechanism. I don’t have enough time to prevent this stuff from happening, but I’m kinda sad about it.

@Profpatsch
Copy link
Member

Like, these functions do not do anything complicated, yet they are getting absurdly complex.

@Profpatsch
Copy link
Member

Profpatsch commented Jun 20, 2022

I might have misunderstood, withRecursion is outside of toYAML, a new and it looks like undocumented function which I don’t know what it’s for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

10.rebuild-darwin: 0 This PR does not cause any packages 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.

4 participants