Gomod2nix "cannot find module providing package"?

G’day,

I’m pretty new to nix and trying to use gomod2nix ( Announcing Gomod2nix ) to build a little go tool. ( Once this is done, I’m also hoping to use nix to build a container with this go binary inside it. )

The instructions are a little hard to follow (probably because I’m silly):

I tried using the flake method but there were various conflicts, and it tried to break go.mod

nix flake init -t github:nix-community/gomod2nix#app

So I ended up compiling gomod2nix separately (go build -o gomod2nix main.go), and then I can run that happily enough to generate the gomod2nix.toml.

The gomod2nix.toml looks vaguely reasonable, in that all the modules from go.mod seem to be there and the NAR hashes are all there.

[das@t:~/Downloads/xtcp2]$ cat gomod2nix.toml | head
schema = 3

[mod]
  [mod."github.com/beorn7/perks"]
    version = "v1.0.1"
    hash = "sha256-h75GUqfwJKngCJQVE5Ao5wnO3cfKD9lSIteoLp/3xJ4="
  [mod."github.com/cespare/xxhash/v2"]
    version = "v2.3.0"
    hash = "sha256-7hRlwSR+fos1kx4VZmJ/7snR7zHh8ZFKX+qqqqGcQpY="
  [mod."github.com/dgryski/go-rendezvous"]
...

Then I use “nix flake init -t github:nix-community/gomod2nix#app” to create flake.nix, default.nix, and the shell.nix. Running “nix flake show” then generates the flake.lock

[das@t:~/Downloads/xtcp2]$ ls -la
total 144
drwxr-xr-x   8 das users  4096 Oct 18 13:46 .
drwxr-xr-x 152 das  1000 36864 Oct 18 08:41 ..
-rw-r--r--   1 das users  1564 Aug 28 12:44 buf.gen.yaml
-rw-r--r--   1 das users   299 Aug 27 16:24 buf.yaml
drwxr-xr-x   3 das users  4096 Sep  6 11:37 build
drwxr-xr-x   3 das users  4096 Aug 27 16:19 cmd
-rw-r--r--   1 das users   463 Oct 18 13:44 default.nix   <-----
-rw-r--r--   1 das users  2095 Oct 18 13:44 flake.lock   <-----
-rw-r--r--   1 das users  1081 Oct 18 13:42 flake.nix     <-----
drwxr-xr-x   4 das users  4096 Jun 12 17:00 gen
-rwxr-xr-x   1 das users   326 Aug 27 16:22 generate.bash
drwxr-xr-x   8 das users  4096 Oct 18 13:47 .git
-rw-r--r--   1 das users    20 Oct 18 13:42 .gitignore
-rw-r--r--   1 das users  1621 Oct 18 13:45 go.mod
-rw-r--r--   1 das users  4156 Oct 18 13:38 gomod2nix.toml <------ generated by gomod2nix
-rw-r--r--   1 das users 13158 Oct 18 13:35 go.sum
-rw-r--r--   1 das users  1072 Aug 27 16:18 LICENSE
-rw-r--r--   1 das users  2962 Oct 18 13:30 Makefile
drwxr-xr-x   7 das users  4096 Aug 28 12:36 pkg
-rw-r--r--   1 das users   187 Sep 18 17:34 README.md
-rw-r--r--   1 das users   455 Oct 18 13:42 shell.nix         <-----
drwxr-xr-x   6 das users  4096 Sep 10 08:34 tools
-rw-r--r--   1 das users     5 Sep 13 15:07 VERSION

However, doing “nix build” throws lots of errors about not being able to find the go dependencies.

[das@t:~/Downloads/xtcp2]$ nix build
warning: Git tree '/home/das/Downloads/xtcp2' is dirty
error: builder for '/nix/store/vkily1bf98gykij02sx5g8v9mfd0ybxy-xtcp2-0.1.drv' failed with exit code 1;
       last 10 log lines:
       > pkg/xtcppb/flatxtcppb.pb.go:31:2: cannot find module providing package google.golang.org/protobuf/runtime/protoimpl: import lookup disabled by -mod=vendor
       > pkg/xtcppb/flatxtcppb.pb.validate.go:19:2: cannot find module providing package google.golang.org/protobuf/types/known/anypb: import lookup disabled by -mod=vendor
       > pkg/xtcpnl/xtcpnl.go:23:2: cannot find module providing package golang.org/x/sys/unix: import lookup disabled by -mod=vendor
       > pkg/xtcp/destinations.go:13:2: cannot find module providing package github.com/redis/go-redis/v9: import lookup disabled by -mod=vendor
       > pkg/xtcp/destinations.go:14:2: cannot find module providing package github.com/twmb/franz-go/pkg/kgo: import lookup disabled by -mod=vendor
       > pkg/xtcp/destinations.go:15:2: cannot find module providing package github.com/twmb/franz-go/plugin/kprom: import lookup disabled by -mod=vendor
       > pkg/xtcp/marshalers.go:7:2: cannot find module providing package github.com/vmihailenco/msgpack/v5: import lookup disabled by -mod=vendor
       > pkg/xtcp/marshalers.go:8:2: cannot find module providing package google.golang.org/protobuf/encoding/protojson: import lookup disabled by -mod=vendor
       > pkg/xtcp/marshalers.go:9:2: cannot find module providing package google.golang.org/protobuf/encoding/prototext: import lookup disabled by -mod=vendor
       > pkg/xtcp/marshalers.go:10:2: cannot find module providing package google.golang.org/protobuf/proto: import lookup disabled by -mod=vendor
       For full logs, run 'nix log /nix/store/vkily1bf98gykij02sx5g8v9mfd0ybxy-xtcp2-0.1.drv'.

I’ve got the code, including the gomod2nix all in a branch here.

Thanks in advance,
Dave

1 Like

Following up on this for others:

I’ve found this excellent blog about building go packages and putting them into containers:

Added this to a repo and tagged it for others to clone and try