How to refer to another user's UID/GID?

I need to refer to another user’s UID and GID in a user configuration. Specifically, I want to replace the startUid and startGid values in the below with references to the other user:

  users.users = {
    root = {
      subUidRanges = [
        {
          startUid = 1001;
          count = 65536;
        }
      ];
      subGidRanges = [
        {
          startGid = 100;
          count = 65536;
        }
      ];
    };
    username = {
      …
    };
  };

So instead I want something like this:

  users.users = {
    root = {
      subUidRanges = [
        {
          startUid = username.uid;
          count = 65536;
        }
      ];
      subGidRanges = [
        {
          startGid = username.mainGroup.gid;
          count = 65536;
        }
      ];
    };
    username = {
      …
    };
  };

However, username.uid (or users.users.username.uid) is not found, and I can’t find any way to get the GID of the main group of a user. I guess something like users.groups.${users.users.username.group}.gid, but I don’t know the syntax and it would run into the same problem as before.

Assuming username has an explicitly-defined uid field, then you should be able to just stick rec on the outer attrset, e.g.

users.users = rec {
  root = {
    subUidRanges = [
      {
        startUid = username.uid;
        count = 65536;
      }
    ];
    …
  };
  username = {
    uid = …;
    …
  };
};

Assuming username has an explicitly-defined uid field, then you
should be able to just stick rec on the outer attrset, e.g.

If you want to do this in another file, something like this will work:

{ config, ... }:

{
  users.users.root = {
    startUidRanges = [
      { startUid = config.users.users.username.uid; count = 65536; }
    ];
  };
}

If you are not manually setting uids, you won’t be able to do this at
all, because in that case uids are assigned at activation time, and so
are not available at build time.