den icon indicating copy to clipboard operation
den copied to clipboard

BUG: <entity>.hasAspect doesn't work when it's included at the first place

Open fmway opened this issue 6 months ago • 13 comments

Related to https://github.com/denful/den/issues/525 https://github.com/denful/den/pull/530

Guards are walk-order independent

Hi. Sorry, i just found a bug related to the PR. It's not work when the conditional is in the first place. It's a minor problem, but I'll assume this is a bug because it doesn't match what's described in the PR.

Here is the reproduction code (ref: https://github.com/fmway/den-policy-conditional-bogus)


  test-conditional-in-the-first-place = denTest (
      { den, igloo, ... }:
      {
        den.hosts.x86_64-linux.igloo.users.tux = { };

        den.aspects.igloo.includes = [
          den.aspects.conditional
          den.aspects.base
        ];

        den.aspects.base.includes = [
          den.aspects.git
        ];

        den.aspects.conditional = {
          includes = [
            (den.lib.policy.when ({ host, ... }: host.hasAspect den.aspects.git) {
              nixos.environment.variables.GIT_ENABLED = "true";
            })
          ];
        };
        den.aspects.git.nixos.programs.git.enable = true;

        expr = igloo.environment.variables ? GIT_ENABLED;
        expected = true;
      }
    );

even included in den.schema it still fails when the the conditional and the entity (or parent) are at the same level.

e.g:

den.schema.host.includes = [
 den.aspects.conditional 
 den.aspects.base
];

fmway avatar May 14 '26 08:05 fmway

Thanks for the bug report! Just to clarify, you're saying that if you write

den.aspects.igloo.includes = [
  den.aspects.base # switched to first position
  den.aspects.conditional
];

That it works properly?

theutz avatar May 14 '26 08:05 theutz

That it works properly?

Yeah, That will work well when the condition is used in the last or next entity.

fmway avatar May 14 '26 08:05 fmway

Ok, perfect. Thanks for confirming the bug and the workaround!

theutz avatar May 14 '26 08:05 theutz

I have an inkling of what the root cause is based on the solution I delivered. But hey, no more infinite recursion right? I'll see if I can close this gap.

sini avatar May 14 '26 09:05 sini

@fmway Can you test the branch in #541 and let me know if you're still able to produce a failure? I'm off to bed, but if it all looks good will merge in the morning. :)

sini avatar May 14 '26 09:05 sini

Wait

fmway avatar May 14 '26 09:05 fmway

OK

sini avatar May 14 '26 09:05 sini

On iteration 3 of the fix, probably the final one -- it defers all guards and only clears them if the remaining guards are still unresolved on a second loop through.

sini avatar May 14 '26 09:05 sini

@fmway I'm off to bed; let me know if you have any followup otherwise I'll merge in the morning.

sini avatar May 14 '26 09:05 sini

Go ahead 😅. I have another test case but I haven't created it yet. I'll mention you when it's done.

fmway avatar May 14 '26 09:05 fmway

Here, my new test @sini

(ref: https://github.com/fmway/den-policy-conditional-bogus)

  test-conditional-in-mutual-provider = denTest (
      { den, igloo, lib, ... }: let
        inherit (den.lib) policy;
      in
      {
        den.hosts.x86_64-linux.igloo.users = {
          tux = { };
          pingu = { };
        };

        den.aspects.features = {
          includes = with den.aspects.features; [
            starship
            jujutsu
          ];
          _.starship = {
            homeManager.programs.starship.enable = true;
          };

          _.jujutsu = {
            homeManager.programs.jujutsu.enable = true;

            includes = [
              (policy.when ({ user, ... }: user.hasAspect den.aspects.features.starship)
                (policy.include den.aspects.features.jujutsu.starship)
              )
            ];
            _.starship = {
              homeManager = { pkgs, ... }:
              {
                home.packages = [ pkgs.starship-jj ];
                programs.starship.settings.custom.jj = {
                  command = "prompt";
                  format = "$output";
                  ignore_timeout = true;
                  shell = ["starship-jj" "--ignore-working-copy" "starship"];
                  use_stdin = false;
                  when = true;
                };
              };
            };
          };
        };

        den.aspects.igloo = {
          includes = [ den.aspects.igloo.policies.to-users ];
          policies.to-users = { user, ... }: [
            (policy.include den.aspects.features)
          ] ++ lib.optional (user.userName == "tux")
            (policy.exclude den.aspects.features.starship);
        };

        expr.tux-starship-enabled = igloo.home-manager.users.tux.programs.starship.enable;
        expr.pingu-starship-enabled = igloo.home-manager.users.pingu.programs.starship.enable;
        expr.tux-starship-include-jj = igloo.home-manager.users.tux.programs.starship.settings ? custom.jj;
        expr.pingu-starship-include-jj = igloo.home-manager.users.pingu.programs.starship.settings ? custom.jj;

        expected.tux-starship-enabled = false;
        expected.pingu-starship-enabled = true;
        expected.tux-starship-include-jj = false;
        expected.pingu-starship-include-jj = true;
      }
    );

The result:

❌ test-conditional-in-mutual-provider
/tmp/nix-761404-1461110346/expected.nix --- Nix
1 { pingu-starship-enabled = true; pingu-starship-include-jj = true; tux-starship-ena  1 { pingu-starship-enabled = true; pingu-starship-include-jj = true; tux-starship-ena
. bled = false; tux-starship-include-jj = true; }                                      . bled = false; tux-starship-include-jj = false; }

features.starship is already excluded for user tux, but it still includes features.jujutsu.starship

fmway avatar May 14 '26 12:05 fmway

Same test, without sub-aspect/provides. Same result

  test-conditional-in-mutual-provider = denTest (
      { den, igloo, lib, ... }: let
        inherit (den.lib) policy;
      in
      {
        den.hosts.x86_64-linux.igloo.users = {
          tux = { };
          pingu = { };
        };

        den.aspects.starship.homeManager.programs.starship.enable = true;
        den.aspects.jujutsu = {
          homeManager.programs.jujutsu.enable = true;
          includes = [
            (policy.when ({ user, ... }: user.hasAspect den.aspects.starship)
              (policy.include {
                homeManager = { pkgs, ... }: {
                  home.packages = [ pkgs.starship-jj ];
                  programs.starship.settings.custom.jj = {
                    command = "prompt";
                    format = "$output";
                    ignore_timeout = true;
                    shell = ["starship-jj" "--ignore-working-copy" "starship"];
                    use_stdin = false;
                    when = true;
                  };
                };
              }))
          ];
        };

        den.aspects.features.includes = with den.aspects; [ starship jujutsu ];

        den.aspects.igloo = {
          includes = [ den.aspects.igloo.policies.to-users ];
          policies.to-users = { user, ... }: [
            (policy.include den.aspects.features)
          ] ++ lib.optional (user.userName == "tux")
            (policy.exclude den.aspects.starship);
        };

        expr.tux-starship-enabled = igloo.home-manager.users.tux.programs.starship.enable;
        expr.pingu-starship-enabled = igloo.home-manager.users.pingu.programs.starship.enable;
        expr.tux-starship-include-jj = igloo.home-manager.users.tux.programs.starship.settings ? custom.jj;
        expr.pingu-starship-include-jj = igloo.home-manager.users.pingu.programs.starship.settings ? custom.jj;

        expected.tux-starship-enabled = false;
        expected.pingu-starship-enabled = true;
        expected.tux-starship-include-jj = false;
        expected.pingu-starship-include-jj = true;
      }
    );

fmway avatar May 14 '26 12:05 fmway

@fmway I woke up this morning and the first thing I thought upon reading your test was ... dang they're 100% right. Thank you so much for probing the edge case, it shows you do deeply understand the intent and extent of the policy system as designed and that makes me very happy.

This should be handled now. Thank you. :)

sini avatar May 14 '26 17:05 sini