buildGoModule and vendoring - Nix

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Magnus Therning

I've posted this on the nix discourse at https://discourse.nixos.org/t/buildgomodule-with-local-src-inconsistent-vendoring/8641 as well, but sometimes the crowd here is a bit quicker :grinning:

Based on the post Use buildGoModule with local src I put together the following expression:

  thePkg = buildGoModule {
    pname = "mypkg";
    version = "0.0.1";
    src = lib.cleanSource ./.;
    vendorSha256 = sha256:0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5;
  };

That worked when I first started out. Then I had no required packages in go.mod, no go.sum, no internal package, just a cmd/my-tool/main.go, ... yes, it was pretty much a "Hello, World!" package.

Then I added code, a couple of internal packages (in internal/...) and some dependencies to go.mod, which of course resulted in a go.sum file. By now the expression above have stopped working, and I get the following output when I try to build:

nix-build --attr pkg
these derivations will be built:
  /nix/store/blhlxnn2fvahpvn4ascyi2wcxznj8iiv-mypkg-0.0.1.drv
building '/nix/store/blhlxnn2fvahpvn4ascyi2wcxznj8iiv-mypkg-0.0.1.drv'...
unpacking sources
unpacking source archive /nix/store/24bshfm5i0wpixrr9avigispybwvzr9f-source
source root is source
patching sources
configuring
building
Building subPackage ./cmd/my-tool
go: inconsistent vendoring in /build/source:
        github.com/beevik/etree@v1.1.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/golang-migrate/migrate/v4@v4.12.2: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/rs/zerolog@v1.19.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/google/uuid@v1.1.1: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/lib/pq@v1.8.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt

run 'go mod vendor' to sync, or use -mod=mod or -mod=readonly to ignore the vendor directory
builder for '/nix/store/blhlxnn2fvahpvn4ascyi2wcxznj8iiv-mypkg-0.0.1.drv' failed with exit code 1

I'm not really sure what I can do about it. Running go mod vendor manually doesn't help (I didn't really expect it to either).

Any help would be much appreciated.

Based on the post Use buildGoModule with local src I put together the following expression: thePkg = buildGoModule { pname = "mypkg"; version = "0.0.1"; src = lib.cleanSource ./.; vendorSha256 = sha256:0sjjj9z1dhilhpc8pq4154czrb79z9cm044jvn75kxcjv6v5l2m5; }; That worked when I first started out. Then I had no required packages in go.mod, no go.sum, no internal package, just a cmd/my-tool/main.go, … yes, it was pretty much a “Hello, World!” package. Then I added code, a co...
This may be an extremely stupid question but all the examples for buildGoModule use fetchFromGitHub. But I just want to do something like in this blog post which is using buildGoPackage. So I wrote the below code { pkgs ? (import ./nixpkgs.nix ) }: with pkgs; buildGoModule rec { pname = "fbrs-blog"; version = "latest"; modSha256 = lib.fakeSha256; src = "../go/"; } which fails with unpacking source archive ../go/ do not know how to unpack source archive ../go/ I looked at the sour...
Magnus Therning

I found out the cause, and also solution to the above issue. Posted it in the the Nix discourse, so if you follow the link above you'll find it.