Dynamic users using a list in module

Hi ! I’ve created some modules to ease the managment at my house. I’m working on a new module that will allow me to set a list of users with an option in my machine.nix file. I’ve already created the option and specified a list of strings.

{
  ...
  local.users = [ "me" "my_wife" "guest" ];
  ...
}

usage I’ve created one folder for each of my users (me, my wife, my kid, guest) and I’d like to load the specified ones only. Do you have any idea of how I can do that ? I’ve tried to use the imports with the option but I’m stuck in an infinite recursion. Thanks a lot for your help !

What does that mean? Please show code.

Show the full error as well.

Hi ! Thanks a lot for your answer.

Here is the code I’ve tried :


let
  cfg = config.ncarr;
  USERS_FOLDER = builtins.toString ./../../users;
  make_user_path = user: "${USERS_FOLDER}/${user}/user.nix";
in
{
  imports = map make_user_path cfg.users;

  options.ncarr = {
    users = lib.mkOption {
      default = [];
      example = [ "nicolas" ];
      description = "";
      type = lib.types.nonEmptyListOf lib.types.str;
    };
  };
}

Here is the error as well :

[sudo] password for nicolas: 
building the system configuration...
error:
       … while calling the 'seq' builtin

         at /nix/store/hfz1qqd0z8amlgn8qwich1dvkmldik36-source/lib/modules.nix:334:18:

          333|         options = checked options;
          334|         config = checked (removeAttrs config [ "_module" ]);
             |                  ^
          335|         _module = checked (config._module);

       … while evaluating a branch condition

         at /nix/store/hfz1qqd0z8amlgn8qwich1dvkmldik36-source/lib/modules.nix:273:9:

          272|       checkUnmatched =
          273|         if config._module.check && config._module.freeformType == null && merged.unmatchedDefns != [] then
             |         ^
          274|           let

       (stack trace truncated; use '--show-trace' to show the full trace)

       error: infinite recursion encountered

       at /nix/store/hfz1qqd0z8amlgn8qwich1dvkmldik36-source/lib/modules.nix:245:21:

          244|           (regularModules ++ [ internalModule ])
          245|           ({ inherit lib options config specialArgs; } // specialArgs);
             |                     ^
          246|         in mergeModules prefix (reverseList collected);

There’s the problem line, imports can’t depend on config, that’ll just be infrec.

Yes.
That’s what I’ve understood. That is why I’m looking for another approach. Do you have any other idea ?

So, I’ve done some digging and I’ve found how to add user only specified on config. Now I’ve got an issue with home-manager. Once I import my user, I’ve got an error that shows up :

The option `home' does not exist. Definition values:
       - In `/nix/store/vvqh4jvr6ib03qjxjhqpcbf0ah500r66-source/users/nicolas/home.nix':

Here is how I use home inclusion within my user.nix file

  config = mkIf (builtins.elem "nicolas" cfg) {
    users.users.nicolas = {
      name = "nicolas";
      password = "1234";
      isNormalUser = true;
      extraGroups = [ "wheel" "networkmanager" ];
    };

    home-manager = {
      extraSpecialArgs = { inherit inputs; };
      users = { "nicolas" = import ./home.nix; };
    };
  };

Do you have any clue on how to add back home option please ?

Did you add the NixOS module from the HM repo into your imports at the NixOS level?

Yes you were right. I’ve imported each files in my user directory. I only needed to import “user.nix” files. Now all is working, thanks a lot