hlissner / dotfiles

And I say hey, what's going on?
https://youtu.be/ZZ5LpwO-An4
MIT License
1.62k stars 98 forks source link

`countAttrs` doesn't work as expected #34

Closed archer-65 closed 2 months ago

archer-65 commented 1 year ago

What did you expect to happen?

Here we are. My nix-fu isn't great, but maybe I found an issue with this function. I'm trying to use countAttrs as done in this assertion.

I have a similar setup for my WMs, like that:

  cfgXorg = config.user-modules.desktop.xorg;
  cfgWayland = config.user-modules.desktop.wayland;

  ...

  {
    assertion = (countAttrs (n: v: n == "enable" && v) cfgXorg) < 2;
    message = "[Xorg] Can't have more than one WM/DE enable at a time.";
  }
  {
    assertion = (countAttrs (n: v: n == "enable" && v) cfgWayland) < 2;
    message = "[Wayland] Can't have more than one WM/DE enable at a time.";
  }

Something like this should print a backtrace!

 user-modules.desktop.wayland = {
      enable = true;
      sway.enable = true;
      hyprland.enable = true;
 };

What actually happened?

The evaluation continues, instead of printing the backtrace.

Describe your attempts to resolve the issue

No response

Steps to reproduce

Enable at least two WMs (I'm using your functions for the same purpose)

Package commit

089f1a9da9018df9e5fc200c2d7bef70f4546026

System Information

No response

hlissner commented 2 months ago

Sorry for only noticing this issue now, and you've probably already figured this out by now, but the problem is with the predicate function. Yours is only searching for a top-level enabled property, and not nested ones. They'd need to be rewritten to:

  {
    assertion = (countAttrs (_: v: v.enable or false) cfgXorg) < 2;
    message = "[Xorg] Can't have more than one WM/DE enable at a time.";
  }
  {
    assertion = (countAttrs (_: v: v.enable or false) cfgWayland) < 2;
    message = "[Wayland] Can't have more than one WM/DE enable at a time.";
  }

Hope that helps someone!