Home-manager share modules between multiple users

Hello!

I have home-manager installed as standalone on two users (user0, user1).
user0 has a flake.nix file that sets up the home-manager config for user0:

/home/user0/.dotfiles/flake.nix

{
	description = "home flake";

	inputs = {
	  nixpkgs.url = "nixpkgs/nixos-23.11";
	  nixpkgs-unstable.url = "nixpkgs/nixos-unstable";
	  home-manager.url = "github:nix-community/home-manager/release-23.11";
	  home-manager.inputs.nixpkgs.follows = "nixpkgs";
	};

	outputs = {self, nixpkgs, nixpkgs-unstable, home-manager, ...}@inputs:
    let
      system = "x86_64-linux";
      pkgs = nixpkgs.legacyPackages.${system};
      pkgs-unstable = nixpkgs-unstable.legacyPackages.${system};
	in {
    homeConfigurations.user0 = home-manager.lib.homeManagerConfiguration {

      inherit pkgs;

      extraSpecialArgs = {
        inherit pkgs-unstable;
      };

      modules = [
        ./home.nix 
      ]; 
    };   
  };
}

and in home.nix i have:

{ config, pkgs, pkgs-unstable, ... }:
{
  imports = [
    .hypr/hypr.nix
  ];
...

I want to also import this module hypr.nix inside user1’s flake.nix.

How to share this module between users? Or maybe how to setup home-manager differently such that it is possible to share a module, but it still allows each user to set their own module options and add their own modules.
Obviously i want to avoid just copy-pasting the hypr.nix module to user1, because i will have to copy it every time and there might be more modules i would want to share.

Thanks in advance!

Probably the most straightforward approach is:

  1. Have a single flake for both users by adding homeConfigurations.user1 to the user0’s flake.

  2. Put the single flake in a location where both users can access it. This location could be on the local file system or in a git forge like GitHub or wherever.

  3. For modules you want to share across users (like hypr.nix) – you could import the modules in modules = [ ] argument for homeManagerConfiguration.

  4. Use home manager switch --flake <flake-uri> for both users. Exact <flake-uri> value depends on step 2.

    man home-manager sez:

       --flake flake-uri[#name]
          Build Home Manager configuration from the flake, which must contain the output homeConfigurations.name.
          If no name is specified it will first try username@hostname and then username.
    

    Home-manager will try to guess which of the homeConfigurations.[user0|user1] to use for the current user.

1 Like