Skip to content

Commit

Permalink
buildGoModule: format expression
Browse files Browse the repository at this point in the history
  • Loading branch information
ShamrockLee committed May 25, 2023
1 parent 98a399e commit 2270c8f
Showing 1 changed file with 115 additions and 106 deletions.
221 changes: 115 additions & 106 deletions pkgs/build-support/go/module.nix
Original file line number Diff line number Diff line change
@@ -1,55 +1,61 @@
{ go, cacert, git, lib, stdenv }:
{ lib
, stdenv
, cacert
, git
, go
}:

{ name ? "${args'.pname}-${args'.version}"
, src
, nativeBuildInputs ? []
, passthru ? {}
, patches ? []
, nativeBuildInputs ? [ ]
, passthru ? { }
, patches ? [ ]

# Go tags, passed to go via -tag
, tags ? []
# Go tags, passed to go via -tag
, tags ? [ ]

# A function to override the go-modules derivation
, overrideModAttrs ? (_oldAttrs : {})
# A function to override the go-modules derivation
, overrideModAttrs ? (_oldAttrs: { })

# path to go.mod and go.sum directory
# path to go.mod and go.sum directory
, modRoot ? "./"

# vendorHash is the SRI hash of the vendored dependencies
#
# if vendorHash is null, then we won't fetch any dependencies and
# rely on the vendor folder within the source.
# vendorHash is the SRI hash of the vendored dependencies
#
# if vendorHash is null, then we won't fetch any dependencies and
# rely on the vendor folder within the source.
, vendorHash ? "_unset"
# same as vendorHash, but outputHashAlgo is hardcoded to sha256
# so regular base32 sha256 hashes work
# same as vendorHash, but outputHashAlgo is hardcoded to sha256
# so regular base32 sha256 hashes work
, vendorSha256 ? "_unset"
# Whether to delete the vendor folder supplied with the source.
# Whether to delete the vendor folder supplied with the source.
, deleteVendor ? false
# Whether to fetch (go mod download) and proxy the vendor directory.
# This is useful if your code depends on c code and go mod tidy does not
# include the needed sources to build or if any dependency has case-insensitive
# conflicts which will produce platform dependant `vendorHash` checksums.
# Whether to fetch (go mod download) and proxy the vendor directory.
# This is useful if your code depends on c code and go mod tidy does not
# include the needed sources to build or if any dependency has case-insensitive
# conflicts which will produce platform dependant `vendorHash` checksums.
, proxyVendor ? false

# We want parallel builds by default
# We want parallel builds by default
, enableParallelBuilding ? true

# Do not enable this without good reason
# IE: programs coupled with the compiler
# Do not enable this without good reason
# IE: programs coupled with the compiler
, allowGoReference ? false

, CGO_ENABLED ? go.CGO_ENABLED

, meta ? {}
, meta ? { }

# Not needed with buildGoModule
# Not needed with buildGoModule
, goPackagePath ? ""

# needed for buildFlags{,Array} warning
# needed for buildFlags{,Array} warning
, buildFlags ? ""
, buildFlagsArray ? ""

, ... }@args':
, ...
}@args':

assert goPackagePath != "" -> throw "`goPackagePath` is not needed with `buildGoModule`";
assert (vendorSha256 == "_unset" && vendorHash == "_unset") -> throw "either `vendorHash` or `vendorSha256` is required";
Expand All @@ -68,96 +74,99 @@ let

args = removeAttrs args' [ "overrideModAttrs" "vendorSha256" "vendorHash" ];

go-modules = if (!hasAnyVendorHash) then "" else
go-modules =
if (!hasAnyVendorHash) then "" else
(stdenv.mkDerivation {

name = "${name}-go-modules";

nativeBuildInputs = (args.nativeBuildInputs or []) ++ [ go git cacert ];

inherit (args) src;
inherit (go) GOOS GOARCH;

# The following inheritence behavior is not trivial to expect, and some may
# argue it's not ideal. Changing it may break vendor hashes in Nixpkgs and
# out in the wild. In anycase, it's documented in:
# doc/languages-frameworks/go.section.md
prePatch = args.prePatch or "";
patches = args.patches or [];
patchFlags = args.patchFlags or [];
postPatch = args.postPatch or "";
preBuild = args.preBuild or "";
postBuild = args.modPostBuild or "";
sourceRoot = args.sourceRoot or "";

GO111MODULE = "on";

impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
"GIT_PROXY_COMMAND" "SOCKS_SERVER" "GOPROXY"
];

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 ''
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 'vendorHash = null;' or 'vendorSha256 = null;' in your expression"
exit 10
fi
name = "${name}-go-modules";

nativeBuildInputs = (args.nativeBuildInputs or [ ]) ++ [ go git cacert ];

inherit (args) src;
inherit (go) GOOS GOARCH;

# The following inheritence behavior is not trivial to expect, and some may
# argue it's not ideal. Changing it may break vendor hashes in Nixpkgs and
# out in the wild. In anycase, it's documented in:
# doc/languages-frameworks/go.section.md
prePatch = args.prePatch or "";
patches = args.patches or [ ];
patchFlags = args.patchFlags or [ ];
postPatch = args.postPatch or "";
preBuild = args.preBuild or "";
postBuild = args.modPostBuild or "";
sourceRoot = args.sourceRoot or "";

GO111MODULE = "on";

impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
"GIT_PROXY_COMMAND"
"SOCKS_SERVER"
"GOPROXY"
];

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 ''
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 'vendorHash = null;' or 'vendorSha256 = null;' in your expression"
exit 10
fi
${if proxyVendor then ''
mkdir -p "''${GOPATH}/pkg/mod/cache/download"
go mod download
'' else ''
if (( "''${NIX_DEBUG:-0}" >= 1 )); then
goModVendorFlags+=(-v)
fi
go mod vendor "''${goModVendorFlags[@]}"
''}
${if proxyVendor then ''
mkdir -p "''${GOPATH}/pkg/mod/cache/download"
go mod download
'' else ''
if (( "''${NIX_DEBUG:-0}" >= 1 )); then
goModVendorFlags+=(-v)
fi
go mod vendor "''${goModVendorFlags[@]}"
''}
mkdir -p vendor
mkdir -p vendor
runHook postBuild
'');
runHook postBuild
'');

installPhase = args.modInstallPhase or ''
runHook preInstall
installPhase = args.modInstallPhase or ''
runHook preInstall
${if proxyVendor then ''
rm -rf "''${GOPATH}/pkg/mod/cache/download/sumdb"
cp -r --reflink=auto "''${GOPATH}/pkg/mod/cache/download" $out
'' else ''
cp -r --reflink=auto vendor $out
''}
${if proxyVendor then ''
rm -rf "''${GOPATH}/pkg/mod/cache/download/sumdb"
cp -r --reflink=auto "''${GOPATH}/pkg/mod/cache/download" $out
'' else ''
cp -r --reflink=auto vendor $out
''}
if ! [ "$(ls -A $out)" ]; then
echo "vendor folder is empty, please set 'vendorHash = null;' or 'vendorSha256 = null;' in your expression"
exit 10
fi
if ! [ "$(ls -A $out)" ]; then
echo "vendor folder is empty, please set 'vendorHash = null;' or 'vendorSha256 = null;' in your expression"
exit 10
fi
runHook postInstall
'';
runHook postInstall
'';

dontFixup = true;
dontFixup = true;

outputHashMode = "recursive";
outputHashAlgo = if vendorHashType == "sha256" then "sha256" else null;
outputHash = if vendorHashType == "sha256" then vendorSha256 else vendorHash;
}).overrideAttrs overrideModAttrs;
outputHashMode = "recursive";
outputHashAlgo = if vendorHashType == "sha256" then "sha256" else null;
outputHash = if vendorHashType == "sha256" then vendorSha256 else vendorHash;
}).overrideAttrs overrideModAttrs;

package = stdenv.mkDerivation (args // {
nativeBuildInputs = [ go ] ++ nativeBuildInputs;
Expand Down

0 comments on commit 2270c8f

Please sign in to comment.