Enabling Home Manager for all users

Hi! I am using Home Manager as a NixOS module.

Owing to the need to prepare a system with multiple users, I used home-manager.sharedModules extensively and exclusively. I now wish to enable Home Manager for everyone, such that programs.home-manager.enable is true for all normal users. I first made the following attempt:

{
  # Attempt 0
  home-manager.sharedModules = [
    { programs.home-manager.enable = true; }
  ];
}

The evaluation appeared to complete but, judging by the content of my home directory, the program was not enabled. I checked the manual and could not find any information there. I then looked at the source code and found these lines of interests, which prompted me to try the following:

{
  # Attempt 1
  home-manager.sharedModules = [
    ({ config, ... }: {
      home.packages = [ config.programs.home-manager.package ];
    })
  ];
}

Unfortunately, the result was more or less the same. I was confused as to why and wrote this:

{
  # Attempt 2
  environment.systemPackages = [
    pkgs.home-manager
  ];
}

… which may not be my proudest attempt but prompted me to start browsing Noogle. After a while, I ended up with this:

{
  # Attempt 3
  home-manager.users = lib.listToAttrs (
    lib.map (name: {
      inherit name;
      value.programs.home-manager.enable = true;
    }) (lib.attrNames config.users.users)
  );
}

But that resulted in an infinite recursion error, presumably due to the use of config.

I am now lost. I had incorrectly assumed in the beginning that this would be a trivial task. Perhaps I am doing something horribly wrong? Any insight on the matter would be well appreciated!

Output of nix-info
  • system: "x86_64-linux"
  • host os: Linux 7.2.9-zen1, NixOS, 26.11 (Zokor), 26.11.20261010.6510408
  • multi-user?: yes
  • sandbox: yes
  • version: nix-env (Nix) 2.34.8
  • nixpkgs: /nix/store/ghasbyyjrmlg6sl7v04hiq8n57kcy9a9-source
Configuration for home-manager
{
  imports = [
    inputs.home-manager-stable.nixosModules.home-manager
  ];
  home-manager = {
    useGlobalPkgs = true;
    useUserPackages = true;
    extraSpecialArgs = { inherit inputs neolib os; };
    backupCommand = "${pkgs.trash-cli}/bin/trash-put";
  };
}

Can you be more specific about what you mean by this? useUserPackages implies no changes to your home directory, so if you mean what you say that is expected.

Besides, if you use the NixOS module programs.home-manager.enable doesn’t make sense, are you sure you understand what that does and want it?

I know it is silly but I tested my changes by deleting .config/libreoffice, disabling Home Manager, rebuilding, re-enabling Home Manager, then rebuilding again. If Home Manager is enabled, I figured that registrymodifications.xcu would be created in its .config/libreoffice/4/user subdirectory after the second rebuild. Another thing that gives me some hint as to whether or not Home Manager is enabled would be these messages:

  • restarting the following units: home-manager...
  • stopping the following units: home-manager...
  • the following new units were started: home-manager...

I had once asked myself the same question! The manual does not mention the need to enable it explicitly. After some observations, I assumed that it would be needed in my case due to a home-manager.users != { } check somewhere.

Ok, so let me first explain why your post feels a bit inconsistent.

programs.home-manager.enable only “installs” the home-manager command line application. You can use it to manage standalone home-manager installations that aren’t being managed by the NixOS module. If you use the NixOS module however, at best using it will give you an error, at worst it will fight with NixOS over who actually controls your home configuration. You should never* use this option on NixOS; it’s unnecessary.

* I’m sure someone has a use case for doing this, but you almost definitely don’t.

What makes me more confident that you’re misunderstanding something is that, even if you actually intend to use that option for some reason, you wouldn’t see any changes to your home directory from enabling that option. useUserPackages makes home-manager alias its home.packages to users.users.<user>.packages. This means that changing that setting changes nothing about your home-manager configuration, only your NixOS configuration (and all that option does is add a symlink to home-manager to a directory that is not in your home directory but referenced in $PATH).


Yet, you seem to say you already have a working home-manager setup. Now however you’re saying you have a libreoffice config that doesn’t get recreated when you enable this option, when ostensibly it’s already being managed by home-manager? That seems inconsistent with an already working home-manager config you just want to add one new option to.

What you’re asking doesn’t really make sense to me. Do you already have an installation with working home-manager modules? Do they stop working when you add programs.home-manager.enable? Are they not working but you think enabling that option will make them work?

I think this is an example of an XY problem - you seem to have some issue, but you’re halfway through figuring it out, and are asking us to autocomplete what you think your solution should be rather than giving us the problem to help you solve.

My apologies. I had meant that I noticed Home Manager linking the LO configuration when I declare home-manager.users.${username}.programs.home-manager.enable. I “disable” it by simply commenting out that line, at which point I observe the unit stopping upon rebuild. I then attempt whatever solution I thought of and rebuilding again. Suppose the solution “works,” I had expected to see either the start of the unit (home-manager-${username}.service) or the appearance of the configuration file. It appears that I have greatly misunderstood the option itself and explained the situation in a non-optimal manner.

Judging by my limited understanding then, I am assuming that home-manager.sharedModules would not apply unless I have something defined under home-manager.users.${username}, but not necessarily home-manager.users.${username}.programs.home-manager.enable itself?

(And that programs.home-manager.enable would be harmful or unnecessary to my setup.)

I tested this hypothesis just now by switching to home-manager.users.${username}.programs.librewolf.enable = true;. It does appear that Home Manager stops ignoring things in sharedModules so long as I have something and anything defined under home-manager.users.${username}… which is a bit silly. I had thought that sharedModules would define configuration entries for all users.

Ahh, I see what the problem is now. Yeah, to get sharedModules to actually do something the user must apparently have something in their configuration. See here in the code, all the settings that do anything are gated behind cfg.users != { } and then map over the set of users to boot.

That code doesn’t care if the user settings are empty though, so I believe you could do this instead of setting real options:

{
  home-manager.users = lib.pipe config.users.users [
    (lib.filterAttrs (_name: config: config.isNormalUser))
    (lib.mapAttrs (_name: _config: { })
  ];
}

Given the home-manager code comments that shouldn’t infrec, but I don’t really trust those comments. If it does infrec there’s no way around just manually listing every user:

{
  home-manager.users = {
    alice = { };
    bob = { };
  }:
}

Thanks for the link. :slightly_smiling_face: Things are starting to make sense now. If I understood correctly from the lines you linked, from what you said earlier, and from issue #594, useUserPackages references users.users to set its packages which causes the infinite recursion error I observed on attempt 3 and with your code. I confirmed this somewhat by setting useUserPackages to false which allowed me to use your code directly without errors.

I want to ask though if there are any bad consequences to disabling the said option to workaround the issue. It seems less than ideal to manually list every user.

EDIT: going through the documentation, it appears that I would, at minimum, lose the benefit of using nixos-rebuild build-vm.

It shouldn’t cause that, I think. The main difference in practice is where the $PATH variable is set - either on login via pam or through the hacky import you need to carry in your ~/.bashrc or equivalent.

In theory both end up with the same end result, but using NixOS’ proper user package provisioning is more robust against mistakes.

I don’t think this should behave any different with build-vm, but useUserPackages is definitely preferable.

Thank you so much for your help! I did not want to sacrifice useUserPackages, especially given the strong and scary wording in the manual and the mention of robustness, so I ended up opting to create a mirrored option (which I feel, out of personally preferences, is nicer than using let) like this:

{
  options.custom.users = lib.mkOption {
    inherit (options.users.users)
      type
      default
      defaultText
      description
      ;
  };
  config.users = { inherit (config.custom) users; };
}

… and using your code almost as is:

{
  home-manager.users = lib.pipe config.custom.users [
    (lib.filterAttrs (_name: config: config.isNormalUser))
    (lib.mapAttrs (_name: _config: { }))
  ];
}

… I would have to define my original users.users entries under custom.users, which is not ideal, but considering the alternatives of manually listing every user and setting useUserPackages to false, this looks like a compromise I can accept. I did manage to find commits of people attempting to do the same thing, but it appears that nothing official has reliably worked thus far.

Yes, in this case I would do that over a let as well - using let would forcibly couple the two blocks into the same scope. That’d be kinda fine, but ruins your ability to contextually group settings.

Personally I’d prefer the simplicity of just listing the users (assuming there aren’t hundreds that change often) and treating it as a home-manager opt-in. But your solution is the next best thing if you can’t accept relaxing a bit on the DRY :wink: