Nix flake wrapping a nix module using home-manager?

I was able to create a simple nix flake wrapping a nix module I defined which just adds pkgs.hello to system env packages, and added it as a module in my main system flake for NixOS and got it rebuilt and working.

desktop app flake

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

  outputs = { self, home-manager, ... } @ inputs: {
    nixosModules.default = { config, pkgs, lib, ... }:
    {
      options = {};

      config = {
        home-manager.users.j = {
          config.xdg.desktopEntries = {
            abc = {
              name = "abc";
              exec = "/bin/sh";
              terminal = true;
              categories = [ "Utility" ];
              type = "Application";
            };
          };
        };
      };
    };
  };
}

usage in my simplified system flake

{
  inputs = {
    nixpkgs2311.url = "github:nixos/nixpkgs/nixos-23.11";
    home-manager.url = "github:nix-community/home-manager/release-23.11";
    home-manager.inputs.nixpkgs.follows = "nixpkgs2311";
    test1.url = "path:/home/j/temp/flaket";
  };

  outputs = { self, nixpkgs2311, ... } @ inputs:
  let
    system = "x86_64-linux";

    pkgs = import nixpkgs2311 {
      inherit system;
      config.allowUnfree = true;
    };

    lib = nixpkgs2311.lib;
    config = nixpkgs2311.config;

    vars = {
      user = "j";
    };
  in
  {
    nixosConfigurations = {
      desktop = nixpkgs2311.lib.nixosSystem {
        inherit system;

        modules = [
          # ...
          inputs.test1.nixosModules.default

          home-manager.nixosModules.home-manager {
            home-manager.useGlobalPkgs = true;
            home-manager.useUserPackages = true;
          }

          # defining nix home-manager modules: https://nix-community.github.io/home-manager/options.xhtml
          home-manager.nixosModules.home-manager {
            home-manager.users.j = import ./home-manager-modules/default.nix { inherit vars pkgs lib config; };
          }
        ];
      };
    };
  };
}

Or should I not be using a flake for this? Can I import or add to modules a remote nix module, from github? Would it help if I made a derivation for the nix module instead then added to flake outputs packages?

Not related to your question, but judging from the formatting you seem to be under the impression that home-manager.nixosModules.home-manager is a function that is being called with an attribute set after it:

{
  modules = [
    # It looks like you are calling a funciton, you're not
    home-manager.nixosModules.home-manager {
      home-manager.useGlobalPkgs = true;
      home-manager.useUserPackages = true;
    }
  ];
}

When in reality they are item 1 and 2 of the modules list:

{
  modules = [
    (home-manager.nixosModules.home-manager) 
    # No relation between the two
    ({
      home-manager.useGlobalPkgs = true;
      home-manager.useUserPackages = true;
    })
  ];
}

Conventionally you’d use the homeManagerModules output, this also means you can use the module outside of NixOs:

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

  outputs = { self, home-manager, ... } @ inputs: {
    # homeManagerModules not nixosModules
    homeManagerModules.default = { config, pkgs, lib, ... }: {
      config.xdg.desktopEntries = {
        abc = {
          name = "abc";
          exec = "/bin/sh";
          terminal = true;
          categories = [ "Utility" ];
          type = "Application";
        };
      };
    };
  };
}
System flake
{
  inputs = {
    nixpkgs2311.url = "github:nixos/nixpkgs/nixos-23.11";
    home-manager.url = "github:nix-community/home-manager/release-23.11";
    home-manager.inputs.nixpkgs.follows = "nixpkgs2311";
    test1.url = "path:../module";
  };

  outputs = { self, home-manager, nixpkgs2311, ... } @ inputs:
    let
      system = "x86_64-linux";

      pkgs = import nixpkgs2311 {
        inherit system;
        config.allowUnfree = true;
      };

      lib = nixpkgs2311.lib;
      config = nixpkgs2311.config;

      vars = {
        user = "j";
      };
    in
    {
      nixosConfigurations = {
        desktop = nixpkgs2311.lib.nixosSystem {
          inherit system;

          modules = [
            # Import the *NixOs module* that load home-manager
            home-manager.nixosModules.home-manager

            # Import your own *NixOs module*
            {
              home-manager.useGlobalPkgs = true;
              home-manager.useUserPackages = true;
              home-manager.users.j = {
                imports = [
                  # Import your external *HomeManager module*
                  inputs.test1.homeManagerModules.default
                  # Import your other *HomeManager module*
                  (import ./home-manager-modules/default.nix {
                    inherit vars pkgs lib config;
                  })
                ];
              };
            }
          ];
        };
      };
    };
}

Thanks for pointing out the mis-use of home-manager nix flake module. Got confused reading import usage and imports and modules spec. That’s interesting nix module spec can be a function or an attr set.

I don’t think flake outputs homeManagerModules is a standard or part of any spec that I can see. Trying it, I get an error in my system flake that homeManagerModules is missing from inputs.test1.homeManagerModules.default.

       error: attribute 'homeManagerModules' missing

       at /nix/store/2hz82rzwqpgbgw0iv2ly2411l2bnkws7-source/flake.nix:65:33:

           64|               imports = [
           65|                                 inputs.test1.homeManagerModules.default
             |                                 ^

This may have some ideas to try to help Home Manager - NixOS Wiki

I could also be packaging the module flake importing as nix module using home-manager.sharedModules instead of defining a home manager module. Maybe that makes it easier.

Testing the home-manager config inline works, but the goal is to package as a nix flake module.

                ({
                  config.xdg.desktopEntries = {
                    abc = {
                      name = "abc";
                      exec = "/bin/sh";
                      terminal = true;
                      categories = [ "Utility" ];
                      type = "Application";
                    };
                  };
                })

It’s unclear how homeManagerModules is added to the nix flake spec by home-manager. I’m on ver 23.11 and it’s not working for me. I see some mention of it here Document using out-of-tree modules with flakes · Issue #1783 · nix-community/home-manager · GitHub

My solution is to package my desktop app as a nix module instead of nix flake. Then in my system flake.nix I can fetch the remote nix module in declaration then import it.

# outputs = ...
let
  myModule = builtins.fetchTarball {
    url = "https://github.com/<user-or-org>/<repo>/archive/main.tar.gz";
    sha256 = "";
  };
in
{
  # ...
  modules = [ (import "${myModule}/nix-module" { inherit pkgs; }) ];
}

I know myModule assumes home-manager is setup so if there something in nix to throw or print an error if home-manager is not defined, let me know.

@jtara1

It’s not a part of any spec or standard, it’s just convention.

Maybe you forgot to nix flake lock --update-input test1 or just nix flake update? Even path: inputs need to be updated for changes to propagate.

Thanks for all the help, was missing nix flake lock --update-input <my-input>. Ended up defining output nixosModules.default for my module flake.

For reference, this is what I was actually packaging https://github.com/jtara1/dictation/blob/6ef22c476cd07462725787d5251830418c4e3df1/flake.nix