den icon indicating copy to clipboard operation
den copied to clipboard

User aspects deliver host-class content (nixos/darwin/os) to their hosts by default

Open sini opened this issue 2 months ago • 8 comments

If you got here from den's warning: A better migration announcement and guide is available here: Heads-up: user aspects will stop leaking into hosts (#750). It explains who's affected and what to do with each warning entry. This issue is the original bug report.

Anything a user aspect wrote for the operating system (nixos, darwin, os) used to reach every host that user is on, with no opt-in. So one user's aspect could change a shared host for everyone on it, while the other direction (a host's home-manager content reaching its users) was already opt-in.

den.hosts.x86_64-linux.igloo.users.tux = { };
den.aspects.tux.nixos.environment.etc.probe.text = "from-tux";
# igloo gets /etc/probe, even though only tux wrote it

What's happening about it: since v0.20.0 den keeps the old behaviour by default but warns for every host that depends on it. On 2027-01-01 the old behaviour goes away (#759), and a user's host-class content only reaches a host that accepts it. You can switch early with den.migrations.user-host-content.acknowledged = true;. The announcement has the options for keeping content on a host.

Original write-up

Summary

A user aspect's host-class content (nixos, darwin, os) is merged into every host the user is on, by default. This breaks isolation between users and hosts: any user's aspect can change a shared host's system for everyone on it. The inverse direction is already isolated — a host's homeManager content does not reach its users unless they opt in with den.batteries.host-aspects — so the two directions are asymmetric.

User → host delivery should be off by default and opt-in, mirroring host-aspects. PR #601 (batteries.user-modules / user-aspects) is prior art for the opt-in; its test plan observed this delivery as off in June, so the current behaviour may also be a regression.

Reproduction (HEAD ba9ee3a5)

den.hosts.x86_64-linux.igloo.users.tux = { };
den.aspects.tux.nixos.environment.etc.probe.text = "from-tux";
# igloo.environment.etc.probe.text == "from-tux"   (expected: absent)

Measured: the value reaches igloo in all of these forms, with the user's classes left at the default or set to [ "homeManager" ]:

Form Reaches host?
den.aspects.tux.nixos (direct) yes
den.aspects.tux.includes = [ feat ], feat.nixos yes
den.aspects.tux = { user, ... }: { nixos = …; } yes
den.aspects.tux.nixos = { pkgs, ... }: … yes
den.aspects.tux.os yes

Three public-api tests currently pin this behaviour and would flip with a fix: test-user-owned-configures-all-hosts, test-user-static-unidirectional-configures-all-hosts and test-user-function-configures-all-hosts in templates/ci/modules/public-api/user-host-mutual-config.nix.

Root cause

assembleSubtree in nix/lib/aspects/fx/edges/materialize.nix builds a host's class module list (for nixos / darwin) by merging that class over the host's whole subtree, via defaultFoldEdges and collectMerge. User scopes are descendants of the host scope and are not isolated, so every user scope's host-class bucket is concatenated into the host's modules. Nothing distinguishes authored user content from the channels den intends to cross the boundary.

Home Manager does not leak the other way because each home is built by a forward whose source is resolved from the user's own scope (userForward in nix/lib/home-env.nix), not from a merge over the host subtree.

Additionally, the os-to-host route (modules/aspects/batteries/os-class.nix) fires in every scope that binds host, user scopes included, so a user's os content is routed into the host as well.

Why den.schema.user.isolated = true is not the fix

Measured with den's own batteries included (define-user, primary-user, user-shell "fish", unfree), a user aspect setting nixos, os, user and homeManager:

Channel from tux's scope Default today With den.schema.user.isolated = true
authored tux.nixos reaches host blocked (correct)
authored tux.os reaches host blocked (correct)
tux.user class → users.users.tux reaches host blocked (wrong)
define-user (isNormalUser) reaches host blocked (wrong)
primary-user (wheel) reaches host blocked (wrong)
user-shell (programs.fish.enable) reaches host blocked (wrong)
home-manager forward (home-manager.users.tux) works home-manager option missing entirely (wrong)
unfree host fallback reaches host reaches host

Isolation closes the breach but also cuts every sanctioned channel.

Proposed design

  1. Close the default merge. A host's class module list collects from the host scope only, not from descendant user scopes. This drops authored tux.nixos / tux.darwin. Restrict os-to-host to fire at host scope only, which drops authored tux.os.
  2. Keep the sanctioned channels, each explicitly routed:
    • the user class → users.users.<self> (user-to-host; it can only touch the user's own account);
    • home-environment forwards (home-manager / hjem / maid) → their own …users.<self> path;
    • den's batteries that deliberately bridge user → host (define-user, primary-user, user-shell, the unfree fallback) move to an explicit effect (policy.provide into host.class, or the user class where the content is account-scoped).
  3. Opt-in battery: den.batteries.user-aspects. A user-scope policy that routes that user's nixos / darwin / os content into its host. Included on one user's aspect it opts in that user; in den.schema.user.includes it opts in every user. This matches the review on #601 asking for the opt-in to be explicit per user, and supersedes that PR.
  4. Tests and migration:
    • flip the three public-api tests above, and add a deadbugs/ regression test covering each form in the reproduction table, with controls showing each sanctioned channel still delivering;
    • release note and migration line: "add den.batteries.user-aspects to den.schema.user.includes to keep the old behaviour";
    • docs: "Where Does My Config Land?" and "Choosing a Mechanism" currently document the leak as behaviour and will need their user → host rows updated.

Alternative considered

Default user scopes to isolated = true and teach each sanctioned channel to cross isolation. That adds less new machinery to the default merge, but isolation also governs pipe collection and spawn behaviour, so the change reaches further than the merge.

Related

  • #601 — batteries.user-modules (prior art for the opt-in battery)
  • #609 — host homeManager content silently dropped (the inverse direction, which is isolated by design)
  • host-aspects battery — modules/aspects/batteries/host-aspects.nix

sini avatar Sep 24 '26 20:09 sini

This feature change would break my configuration right now.

I'm using the current behavior to merge user-specific modules to a system configuration. Minimal example from my config;

den = {
  schema.user.includes = [den.aspects.gaming.policies.user-share-dispatch];
  aspects.gaming = {
    policies.user-share-dispatch = {host, user, ...}:
      lib.optional
      (host.gaming.enable && (builtins.elem "games" user.classes))
      (den.lib.policy.include den.aspects.gaming._.user-share);
    provides.user-share = {host, user}: {
      nixos = {lib, ...}: {
        config = {
          fileSystems."${user.homeDirectory}/game-share" = {
            device = host.gaming.shareDir;
            fsType = "none";
            options = [
              "bind"
              "nofail"
              "x-systemd.after=systemd-tmpfiles-setup.service"
            ];
          };
        };
      };
    };
  };
};

I'm using a policy to include a parametric aspect in a user scope; to change nixos settings. The to-hosts provide routing, or a policy included in the parametric aspect doesn't work because parametric aspects are not resolved before policy resolution.

This is one morsel of config that I stripped for simplicity; but my den setup relies heavily on the current feature; so this bug fix would cause me major headaches.

bbaserdem avatar Sep 30 '26 15:09 bbaserdem

@bbaserdem That's why I haven't fixed it yet as it would manifest as an invisible behavior change. When I make the change I'll provide a battery that reproduces the behavior identically to switch on, so a migration would be a one line update for you. I just need to first figure out a way to surface this change so you can see it before you deploy a broken config -- I very much don't want that.

sini avatar Sep 30 '26 15:09 sini

A concrete case where the default delivery causes harm today, from a fleet that runs a microVM guest as an isolated child entity.

Setup. The guest host never includes the fleet's host baseline (roles.default: impermanence, openssh, agenix, users, nix settings, and so on). Some of its users include roles.default in their user aspects, which is a common pattern so a user brings the same environment to every host. Because of this issue, the nixos content those user aspects author is merged into the guest.

Effects.

  1. The guest silently gets a host configuration it never asked for. Impermanence, sshd, agenix, user accounts and nix settings all reach the guest, but only through its users. Nothing in the guest's own definition says so, and the configuration changes when a user is added to or removed from the host.

  2. Host-scope policies can't see the pool that is actually consumed. persist-collector resolves at each user's scope, so the guest's environment.persistence is built from the users' persist quirk pools. A pipe.transform on persist included in the guest's own aspect runs at the guest scope and filters a pool that nothing reads. In our case, dropping /etc/machine-id from the guest's persisted files had no effect. Even a pipe.append probe changed nothing, which looked like a pipe bug until we traced the scopes. A user-scope policy gated on the host name does filter it, but that's the workaround, not a fix.

  3. The leaked content conflicts with the guest's real setup. The guest pins its own /etc/machine-id and has a tmpfs root with no persistent /persist. The leaked impermanence still binds /etc/machine-id from /persist, so persist-persist-etc-machine-id.service fails on every boot ("A file already exists at /etc/machine-id"). It also adds bind mounts for paths that can't persist on this guest: SSH host keys (the guest's real key comes from a virtiofs share), /var/log, tailscale state, and others.

  4. The same leak hits ordinary hosts invisibly. A host that includes roles.default itself also receives every user's copy. The lists merge to the same values, so nothing breaks, but the host's configuration is the union of its own baseline and every user's, and it depends on who is on the host.

  5. It makes the eventual fix a breaking change. Once user → host delivery is opt-in, a host that only had a baseline through its users loses it outright, and nothing at build time flags that. This is also why surfacing the change before it ships, as discussed above, matters.

Workaround we're using until this is fixed: the guest includes core.impermanence at its own scope, so persist-collector resolves there and reads the guest's own pool. A machine-id filter policy in the guest's includes then applies as expected.

sini avatar Oct 15 '26 08:10 sini

Here is another class of error the default user → host delivery causes. This one comes from a support request in Matrix, reported by @FluxZA, and it's a case where the docs and the behaviour disagree.

Setup. A parametric aspect that takes only { host } sits in den.default.includes and sets a host option:

den.default.includes = [ sys.boot ];
den.hosts.x86_64-linux.hellfire.users.flux = { };
sys.boot = { host, ... }: {
  nixos = { pkgs, ... }: { boot.kernelPackages = pkgs.linuxPackages_latest; };
};

Result at den 436350e:

error: The option `boot.kernelPackages' is defined multiple times while it's expected to be unique.
Definition values:
- In `nixos@sys/boot/<anon>:0'
- In `nixos@sys/boot/<anon>:0'

Why.

  • den.default is walked in the host scope and in every user scope.
  • A user context also carries host, so the { host } function is applied there too.
  • Each application is its own instance, so the scope-keyed dedup keeps them apart.
  • Each user scope's nixos content is then delivered into the host, which is this issue.

The host therefore gets one copy, plus one per user. Counting a marker package placed in the same nixos block:

setup copies on the host
{ host } in den.default.includes, 1 user 2 (and the error above)
same, 2 users 3
same, no users 1
{ host } in den.schema.host.includes, 1 user 1
static aspect, nixos = { host, ... }:, 1 user 1

Why this supports the fix.

  • The author wrote nothing on a user. The only user involvement is den.default being shared across scopes, so the leak is invisible from the code.
  • The failure scales with the user count. A host that evaluates fine with no users breaks when the first user is added.
  • For options that merge silently (lists, environment.systemPackages, etc text with equal values), the duplicates never error at all.
  • The guide told the user the opposite: "a function taking { host } only runs in host contexts". #734 corrects that docs paragraph for now.

With user → host delivery off by default, the user-scope copies would stay in the user's own classes, and the host would get exactly one copy with no change to the aspect.

sini avatar Oct 20 '26 05:10 sini

The design for this issue is written up here: https://gist.github.com/sini/de16b6cb510067d6d31bf5f365db832a

In short: a user's own host-class content (nixos, darwin, os) stays with the user, and anything that crosses to a host goes through an explicit delivery. Two batteries cover today's uses:

  • den.batteries.mutual-provider keeps provides.to-hosts and provides.<host> working. It is on by default, so if you use to-hosts today you won't see a change.
  • den.batteries.user-aspects is the host-side opt-in that restores today's behaviour. den.schema.host.includes = [ den.batteries.user-aspects ]; enables it fleet-wide.

It ships in one release. Evaluation fails only on a host that would actually lose content, and the error names each case and its one-line fix. Feedback on the design is welcome here before the first PR lands.

sini avatar Nov 05 '26 22:11 sini

The design gist linked above has been revised after two rounds of adversarial review: https://gist.github.com/sini/de16b6cb510067d6d31bf5f365db832a

The rulings and the overall shape are the same. The revision fixes how deliveries are routed in four tricky topologies:

  • guests that rebind host;
  • child entities whose scope collapses onto their parent (such as the microvm template's microvm-host);
  • per-user provides being deduplicated away;
  • routing-only scopes such as flake-system.

It also states which checks each of the three PRs carries, and lists the decisions taken during the revision (§10) for review.

sini avatar Nov 06 '26 00:11 sini

Status update

This is for anyone following here rather than on Matrix. The design is in the spec gist. §11 has the decisions made during implementation, and §12 has a follow-up that's now in scope.

PRs (stacked, all drafts)

  1. #746, receiver: a delivery (provide, route, forward) names the entity that receives it. Den's own channels move onto it: home-manager/hjem/maid forwards, user-to-host, account and shell settings, the unfree/insecure fallbacks, and the microvm template. The merge isn't changed. This PR's one disclosed behaviour change: if you set den.schema.user.isolated = true as a workaround for this issue, your users' home-manager, account and shell channels now reach the host. To keep one off, exclude its policy.
  2. #747, batteries:
    • den.batteries.mutual-provider is on by default for hosts, so provides.to-hosts and provides.<host> keep working. You can opt out with an exclude.
    • den.batteries.user-aspects is a host-side opt-in that restores today's behaviour, either for all of a host's users or for a list: (den.batteries.user-aspects { users = [ "tux" ]; }).
  3. PR 3, the flip (in progress): a user's host-class content (nixos, darwin, os) stays with the user. If a host would lose content, evaluation fails with an error that names each case and its fix. This PR also adds a docs page on providing context between scopes.

Each PR has had a review pass, and the findings are being fixed. CI runs once the PRs leave draft. A cost comparison against main is running for each PR.

Changes worth knowing about before the flip

  • A policy that binds { host, user } and uses include with host-class content places that content at the user's scope, whoever registered the policy. After the flip it no longer reaches the host. The error points to the fix: emit provide { receiver = host; class = host.class; … } instead.
  • A user-authored den.aspects.<user>.policies.to-hosts that includes nixos is one case of the above. provides.to-hosts is unaffected.

Follow-up, landing alongside the flip

  • include.at <entity>, so a policy says which entity its content attaches to;
  • a deprecation warning for a bare include in a policy that binds more than one entity;
  • receiver acceptance for every named delivery. Den's channels and the default batteries are accepted by default.

The design points for the follow-up are still being settled, and I'll post them here when they are. An announcement will go out before the flip merges.

sini avatar Nov 06 '26 17:11 sini

Fixed on main by the #694 stack, merged in order:

  • #749: claim and trace lookups
  • #751: late dispatch at any depth, once per instance
  • #752: binding-scoped offers
  • #746: receivers
  • #747: the mutual-provider and user-aspects batteries
  • #753: content stays in its entity, with transition defaults
  • #756: admits, include.at, origin-matched excludes and policy enforcement

A user aspect's nixos, darwin and os content now stays with the user. Until 2027-01-01, transition defaults keep today's behaviour and warn about every host that depends on them. den.migrations.user-host-content.acknowledged = true; adopts the new behaviour early. #750 covers what to do with each warning, and the new What's changed in Den page summarises it.

The cutover itself, removing the transition defaults on 2027-01-01, is tracked in #759.

sini avatar Nov 10 '26 23:11 sini