Gomod2nix with go workspaces

I’m trying to use gomod2nix to build a simple http server that listen to a requests. Everything works well with my local modules. go run and nix run works perfectly, however when I add an outside module, go run works but nix run stop working. This is what I get after using an external module and trying to build/run the application with nix:

error: builder for '/nix/store/192rxacfr8al42fd0zaq74wk8dcqbh0g-sim-cards-bot-0.1.drv' failed with exit code 1;
       last 10 log lines:
       > Running phase: updateAutotoolsGnuConfigScriptsPhase
       > Running phase: configurePhase
       > Running phase: buildPhase
       > Building subPackage .
       > go: inconsistent vendoring in /build/bot:
       > 	github.com/joho/godotenv@v1.5.1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
       >
       > 	To ignore the vendor directory, use -mod=readonly or -mod=mod.
       > 	To sync the vendor directory, run:
       > 		go work vendor
       For full logs, run 'nix log /nix/store/192rxacfr8al42fd0zaq74wk8dcqbh0g-bot-0.1.drv'.

After reading the output I tried to add the -mod=readonly and -mod=mod. to the default.nix but it gets the same output.

{ pkgs ? (
    let
      inherit (builtins) fetchTree fromJSON readFile;
      inherit ((fromJSON (readFile ./flake.lock)).nodes) nixpkgs gomod2nix;
    in
    import (fetchTree nixpkgs.locked) {
      overlays = [
        (import "${fetchTree gomod2nix.locked}/overlay.nix")
      ];
    }
  )
, buildGoApplication ? pkgs.buildGoApplication
}:

buildGoApplication {
  pname = "bot";
  version = "0.1";
  pwd = ./.;
  src = ./.;
  modules = ./gomod2nix.toml;
  ldflags = [
    "-mod=mod"
    "-mod=readonly"
  ];
}

This is my tree:

├── container.nix
├── default.nix
├── Dockerfile
├── flake.lock
├── flake.nix
├── go.mod
├── gomod2nix.toml
├── go.sum
├── go.work
├── go.work.sum
├── helpers
│   ├── auth
│   ├── checkRequest.go
│   ├── go.mod
│   ├── go.sum
│   └── secrets.go
├── main.go
├── README.md
├── result -> /nix/store/az2pfxrkr8s6q66ryawmias66by388m2-docker-image-example.tar.gz
├── shell.nix
└── vendor
    ├── github.com
    │   └── joho
    │       └── godotenv
    │           ├── godotenv.go
    │           ├── LICENCE
    │           ├── parser.go
    │           └── README.md
    └── modules.txt

Any help would be much appreciated it!!!

For anyone still looking for a solution, go-overlay now supports Go workspaces with its buildGoWorkspace function.

{
  inputs.go-overlay.url = "github:purpleclay/go-overlay";

  outputs = { nixpkgs, go-overlay, ... }:
    let
      pkgs = import nixpkgs {
        system = "x86_64-linux";
        overlays = [ go-overlay.overlays.default ];
      };
    in {
      packages.api = pkgs.buildGoWorkspace {
        pname = "api";
        version = "1.0.0";
        src = ./.;
        go = pkgs.go-bin.fromGoMod ./go.mod;
        modules = ./govendor.toml;
        subPackages = [ "api" ];
      };
    };
}

It works by generating the correct vendor/modules.txt format that Go expects for workspace vendoring, where workspace dependencies use the ## workspace header and are resolved from the source tree.

To generate the manifest, run govendor in your workspace root (where go.work lives). It detects workspace modules that are dependencies of other modules and includes them in govendor.toml.

Both in-tree vendor mode (using an existing vendor/ from go work vendor) and manifest mode are supported.

Documentation: GitHub - purpleclay/go-overlay: Nix overlay for Go development. Pure, reproducible, auto-updated. 100+ Go Toolchains from 1.17. Build your Go applications with ease!