Skip to content

fix(batteries): user-shell in den.default sets the shell once - #764

Closed
istvnurbn wants to merge 1 commit into
denful:mainfrom
istvnurbn:fix/user-shell-den-default
Closed

istvnurbn wants to merge 1 commit into
denful:mainfrom
istvnurbn:fix/user-shell-den-default

Conversation

@istvnurbn

Copy link
Copy Markdown
Contributor

(den.batteries.user-shell "<shell>") in den.default.includes should set each user's shell once, as it did on v0.19.0. The v0.20.0 release notes say that upgrading "changes nothing for your systems today", and #750 lists shells and "Anything in den.default" among the things that keep working unchanged. Since v0.20.0, users.users.<name>.shell is defined twice, both times at nixos@<provide>/ (darwin@<provide>/ on nix-darwin), so no host evaluates.

error: The option `users.users.tux.shell' is defined multiple times while it's expected to be unique.

Definition values:
- In `nixos@<provide>/': <derivation fish-4.6.0>
- In `nixos@<provide>/': <derivation fish-4.6.0>

The battery still works on a user aspect, on a host aspect or in den.schema.user.includes.

Reproduction

templates/ci/modules/deadbugs/user-shell-den-default.nix. The same tests also run as a bogus bug.nix:

test-user-shell-in-den-default = denTest (
  { den, igloo, ... }:
  {
    den.hosts.x86_64-linux.igloo.users.tux = { };

    den.default.includes = [
      den.batteries.define-user
      (den.batteries.user-shell "fish")
    ];

    expr = {
      shell = igloo.users.users.tux.shell.pname;
      fish = igloo.programs.fish.enable;
    };
    expected = {
      shell = "fish";
      fish = true;
    };
  }
);

The control in the same file, test-user-shell-on-user, puts the battery on den.aspects.tux instead.

Test v0.19.0 (37eb88c) v0.20.0 as released (36c499a) main (a66a778) main + fix
test-user-shell-in-den-default passes fails fails passes
test-user-shell-on-user (control) passes passes passes passes

The fix also makes the test pass on 36c499a. The v0.20.0 tag now points at a66a778, the same commit as main. 36c499a was main's head when the release was published.

First bad commit: 22b2097, "feat(core): an entity's content stays in its entity, with transition defaults (#694, 3/3) (#753)". git bisect ran from v0.19.0 (good) to 36c499a (bad), with the bogus test as the check, and skipped no commit. The parent, f2ec96b (#747), passes, even though the battery already had its current form there (#746).

Cause

Since #746 the battery hands the shell to the host through a policy record that userShell shell builds inside the battery's { host, user } include:

({ host, user }: { name = "user-shell/${user.userName}@${host.name}"; } // userShell shell)

So each evaluation of that include gives a new record with a new closure, fn = hostShell shell.

Since #753, den.default's { host, user } includes are registered at the host scope and again at the user scope. I traced this for define-user and user-shell. The policy-name claim (registerPolicy, moved to claim.nix by #756) tells registrations apart by ==. The second user-shell record is not equal to the first, so it gets a displaced identity. dedupProvides keys on that identity, so the two keys differ and both deliveries land.

A temporary builtins.trace in the claim and in dedupProvides, on 36c499a, for the test above (user hash shortened):

TRACE claim name=user-shell-host/fish scope=host=igloo,system=x86_64-linux earlierClaims=0 equalToAnEarlierClaim=no identity=user-shell-host/fish
TRACE claim name=user-shell-host/fish scope=host=igloo,system=x86_64-linux,user=tux earlierClaims=1 equalToAnEarlierClaim=no identity=user-shell/tux@igloo/user-shell-host/fish#1
TRACE dedupProvides key=user-shell-host/fish/nixos/@user:4c5a9a67…>host=igloo,system=x86_64-linux
TRACE dedupProvides key=user-shell/tux@igloo/user-shell-host/fish#1/nixos/@user:4c5a9a67…>host=igloo,system=x86_64-linux

On f2ec96b the same test claims user-shell-host/fish only once, at the user scope. On 22b2097 it is claimed at both scopes, as above.

define-user and primary-user take the same path, but their records hold a let-bound fn (fn = account;, fn = hostSettings;). Their second claim is equal to the first (equalToAnEarlierClaim=yes), keeps its identity and gives one key. That is why they still work from den.default.

Fix

Build the record once per user-shell call, in a let around the battery, so every scope its include reaches registers the same record. The second claim then compares equal and keeps the first identity, which is O5 in claim.nix. With the fix, the same trace shows equalToAnEarlierClaim=yes identity=user-shell-host/fish at the user scope and a single user-shell-host/fish key. Nothing under nix/lib changes.

Ignoring whitespace (git diff -w):

@@ -62,7 +62,16 @@ let
 
 in
 {
-  den.batteries.user-shell = shell: {
+  den.batteries.user-shell =
+    shell:
+    let
+      # Built once per call, so every scope the include below reaches gets the
+      # same policy record. From den.default it is registered at the host scope
+      # and again at the user scope; a second record that is not `==` to the
+      # first gets its own identity and delivers the shell again (claim.nix).
+      shared = userShell shell;
+    in
+    {
       inherit description;
       # A stable origin per call (#694 §12): excluding `den.batteries.user-shell`
       # matches every shell.
@@ -73,7 +82,7 @@ in
       ];
 
       includes = [
-      ({ host, user }: { name = "user-shell/${user.userName}@${host.name}"; } // userShell shell)
+        ({ host, user }: { name = "user-shell/${user.userName}@${host.name}"; } // shared)
         (
           { home }:
           {
Full diff (the rest of the battery is only re-indented, as just fmt lays out a let)
--- a/modules/aspects/batteries/user-shell.nix
+++ b/modules/aspects/batteries/user-shell.nix
@@ -62,25 +62,34 @@ let
 
 in
 {
-  den.batteries.user-shell = shell: {
-    inherit description;
-    # A stable origin per call (#694 §12): excluding `den.batteries.user-shell`
-    # matches every shell.
-    name = "user-shell(${shell})";
-    meta.aspect-chain = [
-      "den"
-      "batteries"
-    ];
+  den.batteries.user-shell =
+    shell:
+    let
+      # Built once per call, so every scope the include below reaches gets the
+      # same policy record. From den.default it is registered at the host scope
+      # and again at the user scope; a second record that is not `==` to the
+      # first gets its own identity and delivers the shell again (claim.nix).
+      shared = userShell shell;
+    in
+    {
+      inherit description;
+      # A stable origin per call (#694 §12): excluding `den.batteries.user-shell`
+      # matches every shell.
+      name = "user-shell(${shell})";
+      meta.aspect-chain = [
+        "den"
+        "batteries"
+      ];
 
-    includes = [
-      ({ host, user }: { name = "user-shell/${user.userName}@${host.name}"; } // userShell shell)
-      (
-        { home }:
-        {
-          name = "user-shell/${home.name}";
-          homeManager.programs.${shell}.enable = true;
-        }
-      )
-    ];
-  };
+      includes = [
+        ({ host, user }: { name = "user-shell/${user.userName}@${host.name}"; } // shared)
+        (
+          { home }:
+          {
+            name = "user-shell/${home.name}";
+            homeManager.programs.${shell}.enable = true;
+          }
+        )
+      ];
+    };
 }

Two separate calls, one on a host aspect and one on its user, still define the shell twice. They did on v0.19.0 too, so that is not a regression and this PR leaves it alone.

Checked

On NixOS with Nix 2.34.9, against main at a66a778:

Check main main + fix
just test user-shell-den-default fails passes
just ci user-shell-den-default fails passes
just ci-deep 2178/2178 pass 2180/2180 pass (+ the 2 new cells)
just fmt no changes
just unit (checkmate) passes

ci-deep and unit ran while the suite was still nested as deadbugs.user-shell-den-default. Moving it to the top level changed only that key, and just fmt changes nothing after it.

Also checked with the bogus template:

  • bug.nix passes with the fix on main and on 36c499a.
  • user-shell on a host aspect alone, or in den.schema.user.includes, passes on v0.19.0, on 36c499a and with the fix.
  • define-user and primary-user in den.default apply once (extraGroups = [ "wheel" "networkmanager" ]) on v0.19.0, on 36c499a and with the fix.

On my own config: three NixOS hosts and one nix-darwin host, with Den locked to the v0.20.0 tag (a66a778).

  • With user-shell in den.default.includes, every host fails with the error above (darwin@<provide>/ on the darwin host). With the fix, every host evaluates.
  • With user-shell in den.schema.user.includes, my current workaround, the fix leaves all four system derivations unchanged.
  • With the fix, moving user-shell back to den.default changes only the flake's own registry entry on three hosts (etc-nix-registry.json, or etc-registry.json on darwin). It changes nothing on the fourth host, which has no registry entry for the flake: that host doesn't include the aspect that adds the entry. That is a gap in my config, unrelated to Den and to this fix.

Discussion: #750, which asks for breakage reports there or on #694.

AI-assisted: the analysis and the patch were drafted with Claude. I reproduced and checked everything above on my own machine.

With `(den.batteries.user-shell "<shell>")` in `den.default.includes`,
`users.users.<name>.shell` is defined twice and the host does not
evaluate:

```
error: The option `users.users.tux.shell' is defined multiple times while it's expected to be unique.

Definition values:
- In `nixos@<provide>/': <derivation fish-4.7.1>
- In `nixos@<provide>/': <derivation fish-4.7.1>
```

This worked on v0.19.0. The battery on a user aspect, on a host aspect
or in `den.schema.user.includes` still works.

## Reproduction

`templates/ci/modules/deadbugs/user-shell-den-default.nix`: one host,
one user, `define-user` and `user-shell "fish"` in
`den.default.includes`, plus a control with the battery on the user.

```
expected: { fish = true; shell = "fish"; }
actual:   error: The option `users.users.tux.shell' is defined multiple times
```

The same test as a bogus `bug.nix` passes on v0.19.0 and fails on
v0.20.0 as released (36c499a) and on main. Bisecting with it names
22b2097 (denful#753) as the first bad commit.

## Cause

Since denful#746 the battery hands the shell to the host through a policy
record that `userShell shell` builds inside its `{ host, user }`
include. Each evaluation of that include gives a new record with a new
`fn` closure. Since denful#753 the `{ host, user }` includes of `den.default`
are registered at the host scope and again at the user scope (traced
for `define-user` and `user-shell`). The policy-name claim
(`registerPolicy`, moved to `claim.nix` by denful#756) tells registrations
apart by `==`. The second record is not equal to the first, so it gets
the identity `user-shell/tux@igloo/user-shell-host/fish#1`.
`dedupProvides` keys on that identity, so both deliveries land.

`define-user` and `primary-user` put a `let`-bound `fn` in their
records. Those compare equal, keep one identity and are delivered once.

## Fix

Build the record once per `user-shell` call, in a `let` around the
battery, so every scope its include reaches registers the same record.
The second claim then compares equal and keeps the first identity
(O5 in `claim.nix`). Nothing under `nix/lib` changes.

Two separate calls, `user-shell` on a host aspect and again on its
user, still define the shell twice, as on v0.19.0.

## Checked

On NixOS with Nix 2.34.9, against main at a66a778:

| Check | main | main + fix |
| --- | --- | --- |
| `just test user-shell-den-default` | fails | passes |
| `just ci user-shell-den-default` | fails | passes |
| `just ci-deep` | 2178/2178 pass | 2180/2180 pass (+ the 2 new cells) |
| `just fmt` | | no changes |
| `just unit` (checkmate) | | passes |

`ci-deep` and `unit` ran while the suite was still nested as
`deadbugs.user-shell-den-default`. Moving it to the top level changed
only that key, and `just fmt` changes nothing after it.

With the fix, the bogus `bug.nix` passes on main and on 36c499a.
`user-shell` on a host aspect alone or in `den.schema.user.includes`
still passes, and `define-user` with `primary-user` in `den.default`
still applies once.

Also tested locally on my own config.

AI-assisted: the analysis and the patch were drafted with Claude. I
reproduced and checked everything above on my own machine.
@istvnurbn
istvnurbn requested a review from sini as a code owner November 12, 2026 20:25
sini added a commit that referenced this pull request Nov 12, 2026
## Summary

Reported, bisected and traced by @istvnurbn in #764:
`(den.batteries.user-shell "fish")` in `den.default.includes` defines
`users.users.<name>.shell` twice since v0.20.0, so no host evaluates.
Their bisect to 22b2097 (#753) and their claim trace are what pointed at
the cause. Thank you!

#764 fixes the battery. The same failure also hits any policy record
that a `{ host, user }` include in `den.default` builds inline, so this
PR fixes it in core:

```nix
den.default.includes = [
  ({ host, user }: {
    includes = [{
      __isPolicy = true;
      name = "my-shell";
      fn = { host, user, ... }: [ (den.lib.policy.provide { … receiver = host; }) ];
    }];
  })
];
```

**Cause.** Since #753, a `{ host, user }` include that the descendant
also gets (`den.default`) is fanned out at the host scope too, once per
user. R6 drops that copy's class content for an accepted user, but its
policies still register at the host scope, and the user's own copy
registers again. The policy-name claim (`claim.nix`) tells registrations
apart by whole-value `==`. A record built inside the include is a new
value in every copy, so each copy is displaced to its own identity and
delivers again: twice for one user, three times each for two. A
`let`-bound record compares equal and collapses, which is why
`define-user` and `primary-user` worked, and why #764's `let` fixes the
battery.

**Fix.** A policy record now carries the include site it was written at
(`children.nix` `policySiteKey`):
- the parametric include as written;
- the include positions below it;
- the bound entities its `fn` doesn't take as arguments, so a record
that captured the outer `user` keeps one registration per user.

Every copy of one include is walked from the same value, and `==`
compares a function inside a container by pointer, so copies' sites are
equal. The site isn't the wrapped `__fn`: a functor's is rebuilt on
every walk (`normalize.nix` `wrapFunctorChild`).

A claim with an equal site is a copy of a registration already made, so
it isn't registered again. Two different records under one name keep
different sites and stay two registrations (O4). A record with no site,
and a value-equal match (O5), behave as before.

## Behaviour against main

Each row holds in transition and acknowledged mode.

| `den.default` `{ host, user }` include | v0.19.0 | main | this PR |
|---|---|---|---|
| inline policy, unique option | once | defined twice | once |
| inline policy, list option | 1 | 2 | 1 |
| two users | `[0 1]` | `[3 3]` | `[1 1]` |
| two records under one name, separate includes | `[1 1]` | `[2 2]` |
`[1 1]` |
| two records under one name, one include | `[1 1]` | `[2 2]` | `[1 1]`
|
| record in a static aspect inside the include | 1 | 2 | 1 |
| `fn` reads the outer `user`, two users | `[0 1]` | `[2 2]` | `[1 1]` |
| bare `policy.include` of host content, two users | `[0 1]` | `[2 2]` |
`[1 1]` |
| `den.batteries.user-shell` | once | defined twice | once |
| the same include on the user aspect (control) | once | once | once |

v0.19.0's `[0 1]` is an older bug: a user's registration overwrote its
sibling's. This PR fixes that too.

## Validation

- New cells:
`templates/ci/modules/deadbugs/den-default-inline-policy.nix`, 10 cells,
each in transition and acknowledged mode. 20/20 here, 4/20 on main.
- Full suite (`ci.bash`, 4 workers): 2200/2200.
- Every template's drvs are equal to main: 14 configurations, 9 build
and 5 fail to evaluate on both.
- Evaluator cost (`NIX_SHOW_STATS`) on the 9 template configurations
that evaluate: at most +0.08% function calls and +0.03% thunks.
- `#764`'s battery passes unmodified, so its change isn't needed once
this lands.
@istvnurbn istvnurbn closed this Nov 12, 2026
@istvnurbn

Copy link
Copy Markdown
Contributor Author

#765 fixes the root cause

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant