Using `go mod edit` in packages

I was writing a nixpkgs PR for a Go module, and I needed to patch one of the upstream modules. I thought I would use a go.mod replace directive for this, which is designed for this use case. I also thought I would use the go mod edit tool, which is designed for use in scripts to edit go.mod files.

However, it failed because the go binary got confused about where to put cache stuff. Instead I ended up writing a patch file just for the purpose of adding the replace directive.


Does anyone have any recommendations about how to make it possible to use go mod edit to script editing the go.mod file? It’s a powerful tool for packagers and I’d like to make it easier for people to use, but currently there are 0 usages of the string go mod edit in nixpkgs.

cc @kalbasit as buildGoModule maintainer.

Maybe could help?

I’m unsure how you think this would help me. I want to add replace directives to go.mod using go mod edit. You’ve linked to a PR that allows using the vend tool. vend explicitly does not support replace directives. Is there something I’m missing here?

Is there something I’m missing here?

No there isn’t, I didn’t know vend doesn’t support replace directives.

This is interesting because I was doing the same exact thing last night. However, using the replace directive just worked (once I figured out how to do it correctly, I’d never done this with Go modules before).

I did get stuck for a bit before remembering to invalidate vendorSha256 to actually get Nix to redo the vendoring.

In my case, I have a forked copy of sops, and I was updating a Go package inside sops-nix to replace sops. sops-nix has a module that builds the Go package. Ultimately, this ended up just working once I did it correctly and my forked code was definitely being used.

Can you elaborate on what didn’t work?

EDIT: Oh, I guess you’re trying to avoid patching go.mod, which is confusing. It will likely change go.sum and recalculating that in the build phase doesn’t seem appropriate. You kind of need to edit it, build it with Go locally, and then update the rev/sha256/vendorSha256. I don’t know how you can avoid that if you’re replacing part of the dep tree. Or I’m still missing something maybe, sorry.