den icon indicating copy to clipboard operation
den copied to clipboard

The `host` context argument does not inherit from `host` or `home` schemas in standalone home contexts

Open Smona opened this issue 1 week ago • 7 comments

My use case is that I want to configure whether a system isGraphical via the host/home schemas, and then use that value in parametric aspects included by my user aspect, to conditionally import aspects for GUI applications. This works great for NixOS/Darwin hosts, but for standalone homes, the host argument is only { inherit name system; }, so I can't simply access those host fields in my aspects. The same seems to be true of freeform fields on homes.

If I declare a host with the same name I can work around it, but unless I put fake config in there it fails nix flake check.

I searched around the docs for a way to support this use case but couldn't find any. I know standalone homes are newer so I'm not sure if this is a bug or if I'm just missing how to do this.

Smona avatar Nov 23 '26 08:11 Smona

Sorry, the docs don't make this clear at all. It's working as intended, and there's a way to do what you want today.

On a standalone home, host is deliberately minimal. A plain tux home doesn't bind host at all. A tux@astra home whose host isn't in den.hosts only gets { name; system; }, and that host has no class on purpose, so OS-level batteries stay off for machines den doesn't manage. The home's own fields aren't copied onto host; the home entity itself is bound as home, and that's where your schema and freeform fields live.

So declare the option once on den.schema.conf, which hosts, users and homes all inherit, and read it from whichever entity is in scope:

den.schema.conf.options.isGraphical = lib.mkOption {
  type = lib.types.bool;
  default = false;
};

den.hosts.x86_64-linux.igloo = { isGraphical = true; users.tux = { }; };
den.homes.x86_64-linux."tux@work".isGraphical = true;

den.aspects.tux.includes = [
  # host users
  ({ host, user, ... }: lib.optionalAttrs (host.isGraphical or false) { includes = [ den.aspects.gui ]; })
  # standalone homes
  ({ home, ... }: lib.optionalAttrs home.isGraphical { includes = [ den.aspects.gui ]; })
];

The or false is there because a user@host home whose host isn't declared gets that minimal host, which has no isGraphical. If you'd rather keep the field off hosts, den.schema.home.options.isGraphical works the same way for the home side.

You don't need the fake host either. Declaring one makes den build a real nixosConfigurations.<name>, which is why nix flake check wanted real config for it.

I'll open a PR adding this to the Standalone Home Manager guide and link it here once it's up.

sini avatar Nov 23 '26 19:11 sini

I see, that does make sense! I suppose I was hoping to not have to duplicate the conditionals and the includes for what really is a single concept: whether the host the user aspect is installed into has a graphical environment.

Considering that not having to duplicate those included aspects would clean up user code a fair bit, do you see it as undesirable to support a single context argument that can be used for home/host schema fields? Since there is already a host virtual argument for standalone homes, I don't see what architectural downside that would have that would outweigh the convenience it would add. The class could still be omitted, which I agree makes sense. But I will admit I'm swimming beyond my depth when it comes to the architecture of den 🙂

If that goes against your vision then I can definitely work with your suggestion, maybe write a wrapper function that encapsulates the host/home branching. This was just the one area I found where my configuration wasn't dead simple after migrating to your framework, which is saying a lot!! you've really done some incredible work here ❤️

Smona avatar Nov 23 '26 20:11 Smona

It's a fair ask, and you're right that it's one concept: the machine the user aspect lands on. The user design predates my time as a core maintainer, and I'd been deferring a proper user registry until I finished work on the new gen kernel components. This issue has prompted me to reschedule that. I'll have a specification gist and a new issue linked here shortly, covering standalone homes, hosts and user assignment.

In the meantime, you can get a single include today by declaring the machine as a host that den doesn't build. With intoAttr = [ ], den produces no nixosConfigurations.work, so nix flake check has nothing to check, and the tux@work home binds it as its real host:

den.schema.host.options.isGraphical = lib.mkOption {
  type = lib.types.bool;
  default = false;
};

den.hosts.x86_64-linux.igloo = { isGraphical = true; users.tux = { }; };
den.hosts.x86_64-linux.work = { isGraphical = true; intoAttr = [ ]; }; # described, not built
den.homes.x86_64-linux."tux@work" = { };

den.aspects.tux.includes = [
  ({ host, user, ... }: lib.optionalAttrs (host.isGraphical or false) { includes = [ den.aspects.gui ]; })
];

That one include covers tux on igloo and the tux@work home. Two caveats: osConfig is null in tux@work, since there's no system config behind it, and a plain tux home (no @host) still has no host to read. The or false keeps it safe for any user@host home whose host you haven't declared.

And thank you, that's really kind to hear ❤️

sini avatar Nov 23 '26 21:11 sini

The docs PR is up: #780. It adds a "Your own fields on a home" section to the Standalone Home Manager guide, covering reading your own fields through home and the den.schema.conf pattern. Each example is backed by a test.

sini avatar Nov 23 '26 21:11 sini

As promised: the specification is at https://gist.github.com/sini/26625fc7b8d19fd8cec1f0fe42810d28 and tracked in #781. Standalone homes would bind the registry user the same way host users do, so a single include reading user covers both.

sini avatar Nov 23 '26 22:11 sini

That looks great!! It seems like with that change, I could define my custom schema fields on my user entity, and read them off the user in both host and home contexts.

To get a little bit pedantic, I'm still not sure that's the right configuration level. For instance, if I had multiple users on a host, I would ideally want to declare isGraphical and other system-wide fields on the host level, so it's impossible to configure conflicting per-user fields for what is truly a host-level fact. But in my case, I'm currently only managing my own user in my configs, so this wouldn't be a problem for me in practice.

And I imagine there might be other clean ways to enforce that consistency with just the user registry. Like a policy where a host can mandate a field on the user schema have a certain value for all users on the host. Based on my still-limited understanding of policies, that seems doable today so it might just need a documentation example, rather than a full-fledged abstraction over homes & hosts!

Smona avatar Nov 23 '26 23:11 Smona

You're right that isGraphical is a fact about the machine rather than the user, and it belongs on the host. That also makes it impossible for two users on one host to disagree about it.

For a standalone home you can do that today: describe the machine as a host that den doesn't build, as in my earlier comment.

den.schema.host.options.isGraphical = lib.mkOption {
  type = lib.types.bool;
  default = false;
};

den.hosts.x86_64-linux.igloo = { isGraphical = true; users.tux = { }; };
den.hosts.x86_64-linux.work = { isGraphical = true; intoAttr = [ ]; }; # described, not built
den.homes.x86_64-linux."tux@work" = { };

den.aspects.tux.includes = [
  ({ host, ... }: lib.optionalAttrs host.isGraphical { includes = [ den.aspects.gui ]; })
];

intoAttr = [ ] means den outputs nothing for that host, but a tux@work home still binds it as its host. So one include reading host.isGraphical covers tux on igloo and the work laptop, and the fact lives in one place per machine.

A policy that copies a host field into every user would also work, but it puts the same fact into each user, which is the duplication you're trying to avoid.

The general rule we're building toward in #781 is to put each fact on the entity it describes, and let aspects read whichever entity is in scope:

  • Machine facts on the host, including machines den only describes. #781 gives hosts a dedicated facts namespace (den.schema.host.facts) for these.
  • Facts about the person on the user, in the registry (den.users.<name>), so they reach every host and every standalone home of that user.
  • Facts about one person on one machine on the user@host relation, where a per-host value wins over the registry's within the same priority, field by field.

Once #781 lands, conditional includes will also accept a data condition such as den.lib.cond.eq [ "host" "facts" "isGraphical" ] true. It's checked against the fact schema, so a misspelling is an error, and it needs no or false.

The same shape covers a lot of what people currently branch on by hand:

  • Machine capabilities: graphical versus headless, laptop power management, an NVIDIA driver on GPU hosts, Wayland or X11, VM guest tooling. Set a fact per host, then add one conditional include per aspect.
  • Environment and role: prod and staging, or a server's role. With #781's assignment policies (den.lib.policy.assign), the same facts decide which users get accounts on which hosts, for example admins on every prod host.
  • Personal settings that follow you everywhere: shell, editor and git identity on den.users.tux, reaching your NixOS hosts and your standalone homes alike.
  • Personal settings that differ per machine: a work git email on the work laptop only, set on tux@work, without touching your other machines.
  • Machines you don't manage: described-but-unbuilt hosts like work above, so a standalone home there still sees the machine's facts.

You're right that this needs a documentation example. I'll add one to the Standalone Home Manager guide.

sini avatar Nov 24 '26 01:11 sini