How to introduce user defined flake package in home-manager?

Hi,

I have created a flake to wrap a software ds9.

I referred it in my /etc/nixos/flake.nix as

{
  description = "A simple NixOS flake";

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

    ds9-flake={url = "github:astrojhgu/ds9-flake"; #####<---HERE is the ds9 flake
    inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, home-manager, ... }@inputs: {
    nixosConfigurations.laptop = nixpkgs.lib.nixosSystem rec {
      system = "x86_64-linux";
        specialArgs = {
    	myinputs=inputs;
  	};
      modules = [
        # Import the previous configuration.nix we used,
        # so the old configuration file still takes effect
        ./configuration.nix

        home-manager.nixosModules.home-manager
        {
          home-manager.useGlobalPkgs = true;
          home-manager.useUserPackages = true;
          home-manager.users.astrojhgu = import ./users/astrojhgu.nix;
        }
      ];
    };
  };
}

I’m able to add it to the system’s package tree in the /etc/nixos/configuration.nix as

{ config, pkgs, python-packages, myinputs, ... }:
{
    environment.systemPackages = with pkgs; [
     ###...
     ###...
     ###...
     myinputs.ds9-flake.packages.${pkgs.system}.default    
    ];
}

But I cannot find a method to declare this package in my home-manager configuration.

Do anyone know how to do this?

Thanks!

I finally found the solution:
in /etc/nixos/flake.nix:

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

in the home-manager config (astrojhgu.nix)

{ config, pkgs, lib, inputs, ... }:
{
home.packages = with pkgs; [
inputs.ds9-flake.packages.${pkgs.system}.default
];
}
1 Like

While you’re at it, you can (and IMO should, especially as you start adding more) put all home-manager NixOS module config in configuration.nix:

# configuration.nix
{ myinputs, ... }: {
  imports = [
    myinputs.home-manager.nixosModules.home-manager
  ];

  home-manager = {
    useGlobalPkgs = true;
    useUserPackages = true;
    extraSpecialArgs = {
      # You can rename `myinputs` to `inputs` in `flake.nix`
      # if you want to continue using `inherit`
      inputs = myinputs;
    };

    users.astrojhgu = import ./users/astrojhgu.nix;
  };
}

This also makes it a bit more clear what’s going on, I have a feeling people think the home-manager module is a function because of the way it looks in the modules list.

It also keeps your flake.nix cleaner; As an entrypoint it really shouldn’t contain all this business logic.

1 Like