den icon indicating copy to clipboard operation
den copied to clipboard

different behaviour of aspects with positional arguments vs named attrset arguments when included multiple times

Open serephus opened this issue 1 month ago • 3 comments

I don't know if this is intended or bug, but here is the gist:

aspects with positional argument and named attrset argument behave different when include multiple times, positional argument aspect don't get deduped, while aspects with a single attrset argument get deduped.

den.aspects.example-pos = arg: { ... };
den.aspects.example-attr = { arg ? "arg", ... }: { ... };

A real example: I created a den.aspects.disk to define different disko configs, and include multiple of them in one of my hosts, but only the main disk get mounted.

serephus avatar Oct 15 '26 02:10 serephus

Positional arguments carry no identifiers and as such aren't really supported within the pipeline.

sini avatar Oct 15 '26 03:10 sini

Thanks for the clear report, and sorry, my first answer was too terse. This is expected behaviour, but it isn't explained well, so here's what happens and how to structure disk.

Named args mean "parametric aspect"

When an aspect is a function with named arguments ({ device, name ? "main", ... }: …), den treats it as a parametric aspect. The named arguments are context it asks for (host, user, …), which the pipeline fills in. The aspect keeps its identity (disk) whatever values it receives, and den deduplicates includes by identity within a scope.

So den.aspects.disk { device = …; } does not create a new aspect each time. It's still the disk aspect, with those values bound as context. Including it twice on one host includes the same aspect twice, so the second is dropped. That's why only your main disk gets mounted: rootFileSystem and dataFileSystem both include den.aspects.disk { … } on the same host.

A positional function (cfg: { … }) isn't parametric. Each call is an ordinary function call that returns a fresh anonymous aspect, and anonymous aspects are never deduplicated. That's why that form behaved the way you wanted.

Rule of thumb: use named arguments when the aspect should receive context from den ({ host, ... }: …). Use a positional or plain function when you supply the arguments, as with a factory like mkDisk.

The pattern den's own batteries use

Several batteries are factories you call with your own value, often more than once on a host. Each takes its argument positionally and puts the argument in the aspect's name, so every instance has its own identity:

  • den.batteries.tty-autologin: __functor = _self: username: { name = "tty-autologin(${username})"; nixos = …; }. This is the closest match to disk.
  • den.batteries.unfree: the outer factory takes your package list. The aspect it returns is itself context-parametric ({ class, host ? null, ... }), so you can combine the two kinds.
  • den.batteries.user-shell: a plain positional function whose included aspects are named per user (user-shell/${user.userName}@${host.name}).

Applied to your disks

den.aspects.disk.__functor = _self: cfg: { name = "disk(${cfg.name or "main"})"; } // mkDisk cfg;

# rootFileSystem / dataFileSystem keep calling it the same way:
includes = [ (den.aspects.disk { name = "data"; device = …; … }) ];

Calls with different names are now distinct aspects, and an identical repeated call is still deduplicated. You can also drop one instance with excludes = [ (den.aspects.disk { name = "data"; … }) ]. If you'd rather not use __functor, the positional form behaves the same:

den.aspects.disk = cfg: { name = "disk(${cfg.name or "main"})"; } // mkDisk cfg;

Or keep mkDisk as a plain let-bound function and include (mkDisk { … }) directly, if you don't need it under den.aspects.

All of the above was checked against den main (f1a3f99): the collapse of named-argument calls, the positional and plain-function forms keeping both includes, the __functor factory (distinct per name, deduplicated when identical) and excludes on one instance.

sini avatar Oct 15 '26 03:10 sini

Thanks for the clear report, and sorry, my first answer was too terse. This is expected behaviour, but it isn't explained well, so here's what happens and how to structure disk.

Named args mean "parametric aspect"

When an aspect is a function with named arguments ({ device, name ? "main", ... }: …), den treats it as a parametric aspect. The named arguments are context it asks for (host, user, …), which the pipeline fills in. The aspect keeps its identity (disk) whatever values it receives, and den deduplicates includes by identity within a scope.

So den.aspects.disk { device = …; } does not create a new aspect each time. It's still the disk aspect, with those values bound as context. Including it twice on one host includes the same aspect twice, so the second is dropped. That's why only your main disk gets mounted: rootFileSystem and dataFileSystem both include den.aspects.disk { … } on the same host.

A positional function (cfg: { … }) isn't parametric. Each call is an ordinary function call that returns a fresh anonymous aspect, and anonymous aspects are never deduplicated. That's why that form behaved the way you wanted.

Rule of thumb: use named arguments when the aspect should receive context from den ({ host, ... }: …). Use a positional or plain function when you supply the arguments, as with a factory like mkDisk.

The pattern den's own batteries use

Several batteries are factories you call with your own value, often more than once on a host. Each takes its argument positionally and puts the argument in the aspect's name, so every instance has its own identity:

  • den.batteries.tty-autologin: __functor = _self: username: { name = "tty-autologin(${username})"; nixos = …; }. This is the closest match to disk.
  • den.batteries.unfree: the outer factory takes your package list. The aspect it returns is itself context-parametric ({ class, host ? null, ... }), so you can combine the two kinds.
  • den.batteries.user-shell: a plain positional function whose included aspects are named per user (user-shell/${user.userName}@${host.name}).

Applied to your disks

den.aspects.disk.__functor = _self: cfg: { name = "disk(${cfg.name or "main"})"; } // mkDisk cfg;

rootFileSystem / dataFileSystem keep calling it the same way:

includes = [ (den.aspects.disk { name = "data"; device = …; … }) ]; Calls with different names are now distinct aspects, and an identical repeated call is still deduplicated. You can also drop one instance with excludes = [ (den.aspects.disk { name = "data"; … }) ]. If you'd rather not use __functor, the positional form behaves the same:

den.aspects.disk = cfg: { name = "disk(${cfg.name or "main"})"; } // mkDisk cfg; Or keep mkDisk as a plain let-bound function and include (mkDisk { … }) directly, if you don't need it under den.aspects.

All of the above was checked against den main (f1a3f99): the collapse of named-argument calls, the positional and plain-function forms keeping both includes, the __functor factory (distinct per name, deduplicated when identical) and excludes on one instance.

ok, thanks

serephus avatar Oct 15 '26 03:10 serephus