Why does buildGoModule return "cp: cannot stat '/build/go/pkg/mod/cache/download': No such file or directory"

I’m trying to download a private go repo files from GitHub, then build and run a program from one of the directories.

Here’s my nix file:

{ stdenv, config, lib, pkgs, fetchurl, buildGoModule, ... }:
let commit = "fffffffffffffffffffffffffffffffffffffffff";
in let token = "ffffffffffffffffffffffffffffffffffffffff";
in let
  github = fetchurl {
  url =
  sha256 = "fffffffffffffffffffffffffffffffffffffffff";
  name = "my_repo.tar.gz";
in let
  derivative = pkgs.runCommand "derivative" { } ''
    mkdir $out
    mkdir /tmp/junk
    tar -zxf ${github} -C /tmp/junk --strip-components=2
    # mv everything correct
    find $out
in buildGoModule rec {
  name = "main-project";
  version = "2";
  src = "${derivative}";
  modSha256 = "16vbps22dg50i25f6rwc82cvdhdk71i281a9v00wsa21g7bca4pa";

So I get …


building ‘…’

unpacking sources
unpacking source archive …
source root is derivative
patching sources

 cp: cannot stat '/build/go/pkg/mod/cache/download': No such file or directory

What I don’t understand is if I clone that repo on my main system, cd into that directory and run go build ., everything works. Why doesn’t this work?

BTW you can write all these in a single let, as in

  commit = "…";
  token = "…";
  github = fetchurl { … };
  derivative = …;
in buildGoModule rec { …