Skip to content

go: Add an abstraction to fetch Go modules #124993

@primeos

Description

@primeos

This is basically the equivalent of #112500 for Go: Compiling non-Go packages that include Go code (possibly in a vendored subproject) that use vendored Go modules that we need to fetch using go mod vendor. An example (that prompted me to create this issue) can be seen in #124992.

It would be nice if we had a function like this for Go:

cargoDeps = rustPlatform.fetchCargoTarball {
  inherit src;
  sourceRoot = "...";
  name = "${pname}-${version}";
  hash = "...";
};

The current code for the fixed-output derivation that fetches vendored Go modules:

go-modules = if vendorSha256 != null then stdenv.mkDerivation (let modArgs = {
name = "${name}-go-modules";
nativeBuildInputs = (args.nativeBuildInputs or []) ++ [ go git cacert ];
inherit (args) src;
inherit (go) GOOS GOARCH;
patches = args.patches or [];
preBuild = args.preBuild or "";
sourceRoot = args.sourceRoot or "";
GO111MODULE = "on";
impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
"GIT_PROXY_COMMAND" "SOCKS_SERVER"
];
configurePhase = args.modConfigurePhase or ''
runHook preConfigure
export GOCACHE=$TMPDIR/go-cache
export GOPATH="$TMPDIR/go"
cd "${modRoot}"
runHook postConfigure
'';
buildPhase = args.modBuildPhase or ''
runHook preBuild
'' + lib.optionalString (deleteVendor == true) ''
if [ ! -d vendor ]; then
echo "vendor folder does not exist, 'deleteVendor' is not needed"
exit 10
else
rm -rf vendor
fi
'' + ''
if [ -d vendor ]; then
echo "vendor folder exists, please set 'vendorSha256 = null;' in your expression"
exit 10
fi
${if runVend then ''
echo "running 'vend' to rewrite vendor folder"
${vend}/bin/vend
'' else ''
go mod vendor
''}
mkdir -p vendor
runHook postBuild
'';
installPhase = args.modInstallPhase or ''
runHook preInstall
# remove cached lookup results and tiles
cp -r --reflink=auto vendor $out
runHook postInstall
'';
dontFixup = true;
}; in modArgs // (
{
outputHashMode = "recursive";
outputHashAlgo = "sha256";
outputHash = vendorSha256;
}
) // overrideModAttrs modArgs) else "";

I'm not familiar with Go or our Nixpkgs abstractions for it but it seems that if we convert go-modules into a function than we should be able to use it outside of buildGoModule and use something like this:

rm -rf $vendor
cp -r --reflink=auto ${go-modules} $vendor

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.status: stalehttps://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md6.topic: golangGo is a high-level general purpose programming language that is statically typed and compiled.

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions