Building golang tool with dependency in private repo

I’m once again trying to create a flake for a golang project at work, and since it has one dependency that’s in a private repo I’m once again struggling. I can’t believe there’s still no documented solution to this problem. Anyway, in a discussion on Matrix I was told someone else had gotten it to work by

  1. add the private repos of the dependencies to the inputs (marking them with flake = false), and
  2. modifying the go.mod using go mod edit --replace github.com/private/dep=${input_dev}.

So I made a flake.nix like this

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-24.05";
    flake-utils.url = "github:numtide/flake-utils";
    priv-dep = {
      url = "git+ssh://git@github.com/private/dep.git?ref=v1.0.0";
      flake = false;
    };
  };

  outputs = { self, nixpkgs, flake-utils, priv-dep, ... }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs { inherit system; };

        proj = pkgs.buildGo123Module {
          pname = "tool";
          version = "1.0.0";
          src = ./.;
          vendorHash = pkgs.lib.fakeHash;

          preBuild = ''
            echo 'preBuild - rewriting go.mod'
            go mod edit --replace github.com/private/dep=${priv-dep}
          '';
        };
      in {
        packages.default = proj;
      });
}

This seems to make the golang tools happy. The build phase succeeds, but then it fails with

error: illegal path references in fixed-output derivation '/nix/store/h68mm85avmcai30wkr5kpzak5av2d11b-tool-1.0.0-go-modules.drv'
error: 1 dependencies of derivation '/nix/store/496hnhbx856i6ynlpc7xcy5dzi4jdmr7-tool-1.0.0.drv' failed to build

and I’m at a loss how to solve this. TBH, I’m not even completely sure what the error means.

All help will be appreciated… or maybe I’m no the wrong path completely and and need to rethink this completely.

Why did you specify flake = false? Also, you didn’t specify priv-dep as a dependency in pkgs.buildGo123Module.

Why did you specify flake = false?

Because it isn’t a flake, it’s an ordinary go module in a git repo

Also, you didn’t specify priv-dep as a dependency in pkgs.buildGo123Module.

How would I do that?

I tried to simplify the flake before posting here, but I forgot to change it in some places. Maybe that caused some confusion. I’ve fixed the flake in the post above now.

Not sure it’ll work but put nativeBuildInputs = [ priv-dep ]; above preBuild.

It doesn’t

error: Dependency is not of a valid type: element 1 of nativeBuildInputs for tool-1.0.0-go-modules

which makes sense, since priv-dep isn’t a derivation, it’s just a git repo containing a go module that is listed in go.mod.

I thought maybe the error was caused by my changing a file that’s part of the source. However, restoring it isn’t as straightforward as I’d hoped as there’s a bunch of hooks that aren’t run, despite there being calls to runHook <hook name> in the shell code of the phases. E.g none of the install hooks are run, the preBuild hook is run but not postBuild.

This is perplexing to the point of madness.

So, the way I was planning on doing it wasn’t working out, but then I found this reply on another post, and it works beautifully.