Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/languages-frameworks/go.section.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ The following is an example expression using `buildGoModule`:

```nix
{
pet = buildGoModule rec {
pet = buildGoModule (finalAttrs: {
pname = "pet";
version = "0.3.4";

src = fetchFromGitHub {
owner = "knqyf263";
repo = "pet";
rev = "v${version}";
rev = "v${finalAttrs.version}";
hash = "sha256-Gjw1dRrgM8D3G7v6WIM2+50r4HmTXvx0Xxme2fH9TlQ=";
};

Expand All @@ -58,7 +58,7 @@ The following is an example expression using `buildGoModule`:
license = lib.licenses.mit;
maintainers = with lib.maintainers; [ kalbasit ];
};
};
});
}
```

Expand Down
59 changes: 32 additions & 27 deletions pkgs/build-support/go/module.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{ go, cacert, git, lib, stdenv }:

{ name ? "${args'.pname}-${args'.version}"
let postArgsFunc =
{ name ? "${prevAttrs.pname}-${prevAttrs.version}"
, src
, nativeBuildInputs ? [ ]
, passthru ? { }
Expand All @@ -17,7 +18,7 @@
# if vendorHash is null, then we won't fetch any dependencies and
# rely on the vendor folder within the source.
, vendorHash ? throw (
if args'?vendorSha256 then
if prevAttrs?vendorSha256 then
"buildGoModule: Expect vendorHash instead of vendorSha256"
else
"buildGoModule: vendorHash is missing"
Expand Down Expand Up @@ -53,54 +54,52 @@
, buildFlagsArray ? ""

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

assert goPackagePath != "" -> throw "`goPackagePath` is not needed with `buildGoModule`";

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

GO111MODULE = "on";
GOTOOLCHAIN = "local";

goModules = if (vendorHash == null) then "" else
(stdenv.mkDerivation {
name = "${name}-go-modules";

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

inherit (args) src;
inherit (prevAttrs) src;
inherit (go) GOOS GOARCH;
inherit GO111MODULE GOTOOLCHAIN;

# 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 "";
env = args.env or { };
prePatch = prevAttrs.prePatch or "";
patches = prevAttrs.patches or [ ];
patchFlags = prevAttrs.patchFlags or [ ];
postPatch = prevAttrs.postPatch or "";
preBuild = prevAttrs.preBuild or "";
postBuild = prevAttrs.modPostBuild or "";
sourceRoot = prevAttrs.sourceRoot or "";
env = prevAttrs.env or { };

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

configurePhase = args.modConfigurePhase or ''
configurePhase = prevAttrs.modConfigurePhase or ''
runHook preConfigure
export GOCACHE=$TMPDIR/go-cache
export GOPATH="$TMPDIR/go"
cd "${modRoot}"
runHook postConfigure
'';

buildPhase = args.modBuildPhase or (''
buildPhase = prevAttrs.modBuildPhase or (''
runHook preBuild
'' + lib.optionalString deleteVendor ''
if [ ! -d vendor ]; then
Expand Down Expand Up @@ -130,7 +129,7 @@ let
runHook postBuild
'');

installPhase = args.modInstallPhase or ''
installPhase = prevAttrs.modInstallPhase or ''
runHook preInstall

${if proxyVendor then ''
Expand All @@ -156,8 +155,12 @@ let
# error: empty hash requires explicit hash algorithm
outputHashAlgo = if vendorHash == "" then "sha256" else null;
}).overrideAttrs overrideModAttrs;

package = stdenv.mkDerivation (args // {
in
(builtins.removeAttrs prevAttrs [
"overrideModAttrs"
"vendorSha256"
"vendorHash"
]) // {
nativeBuildInputs = [ go ] ++ nativeBuildInputs;

inherit (go) GOOS GOARCH;
Expand All @@ -172,7 +175,7 @@ let
# If not set to an explicit value, set the buildid empty for reproducibility.
ldflags = ldflags ++ lib.optional (!lib.any (lib.hasPrefix "-buildid=") ldflags) "-buildid=";

configurePhase = args.configurePhase or (''
configurePhase = prevAttrs.configurePhase or (''
runHook preConfigure

export GOCACHE=$TMPDIR/go-cache
Expand All @@ -198,7 +201,7 @@ let
runHook postConfigure
'');

buildPhase = args.buildPhase or (
buildPhase = prevAttrs.buildPhase or (
lib.warnIf (buildFlags != "" || buildFlagsArray != "")
"`buildFlags`/`buildFlagsArray` are deprecated and will be removed in the 24.11 release. Use the `ldflags` and/or `tags` attributes instead of `buildFlags`/`buildFlagsArray`"
lib.warnIf (builtins.elem "-buildid=" ldflags)
Expand Down Expand Up @@ -285,8 +288,8 @@ let
runHook postBuild
'');

doCheck = args.doCheck or true;
checkPhase = args.checkPhase or ''
doCheck = prevAttrs.doCheck or true;
checkPhase = prevAttrs.checkPhase or ''
runHook preCheck
# We do not set trimpath for tests, in case they reference test assets
export GOFLAGS=''${GOFLAGS//-trimpath/}
Expand All @@ -298,7 +301,7 @@ let
runHook postCheck
'';

installPhase = args.installPhase or ''
installPhase = prevAttrs.installPhase or ''
runHook preInstall

mkdir -p $out
Expand All @@ -318,6 +321,8 @@ let
# Add default meta information
platforms = go.meta.platforms or lib.platforms.all;
} // meta;
});
};
in
package
args: stdenv.mkDerivation (finalAttrs:
postArgsFunc (if builtins.isAttrs args then args else args finalAttrs)
)