How to generate deps.nix for derivation in home-manager config

I’m trying to make my first derivation in home-manager config that builds a dotnet project.

{ pkgs, ... }:
{
  home.packages = [
   (pkgs.buildDotnetModule rec {
      pname = "PowerShellEditorServices";
      version = "4.1.0";

      src = pkgs.fetchFromGitHub {
        owner = "PowerShell";
        repo = pname;
        rev = "v${version}";
        hash = "sha256-nOVPs/lnS3vm+mef796g5AnVV0UDoIeuifgkWCTNDyo=";
      };

      projectFile = "${pname}.sln";
      nugetDeps = ./deps.nix;
    })
  ];
}
   

As the Nixpkgs Reference Manual states:

When packaging a new application, you need to fetch its dependencies. Create an empty deps.nix , set nugetDeps = ./deps.nix , then run nix-build -A package.fetch-deps to generate a script that will build the lockfile for you.

But I am not sure how could I do this for a home-manager config. The following error raised for nix-build -A package.fetch-deps

error:
       … from call site
         at /home/sharpchen/.config/home-manager/packages/language-server/default.nix:1:1:
            1| { pkgs, ... }:
             | ^
            2| {

       error: function 'anonymous lambda' called without required argument 'pkgs'
       at /home/sharpchen/.config/home-manager/packages/language-server/default.nix:1:1:
            1| { pkgs, ... }:
             | ^
            2| {

Or am I following the wrong manual that actually guides for nixpkgs packaging only?

I thought that this would only be possible as an overlay.
Are you sure that the home manager can do that?

I am afraid that you will have to create the deps.nix manually!

No, you just need to callPackage. See Packaging existing software with Nix — nix.dev documentation and Package parameters and overrides with callPackage — nix.dev documentation. That has nothing to do with HM btw, this is all available from nixpkgs.

Sort of, the problem is that if you are putting this in nixpkgs, then it’s likely callPackage-d for you already, whereas you must do it manually if in your own config. So again, see above.

From the CLI, the equivalent call would be something like

 nix-build -E 'with (import <nixpkgs> {}); (callPackage ./path/to/package.nix {}).fetch-deps'

And really it should be structured like a package expression, not as a module, see the links above.

Once you fix that, and then generate the deps.nix, you could write in your HM config:

{ pkgs, ... }:
{
  home.packages = [
    (pkgs.callPackage ./path/to/package.nix { })
  ];
}

Ok, I didn’t know that, but that doesn’t solve the problem that the deps.nix have to be created manually.

And I gave instructions on doing so. I wouldn’t exactly call it “manual”, nix is still doing the heavy lifting.

Thanks, I am now able to get it work but I felt it a little trivial.
What I did is using import <nixpkgs> in default.nix first to generate the lockfile and then change it to the form what callPackage would accept. Is there a better practice? I think I would have to repeat it each time I update the package.