Factor out Go version selection from mkGoEnv and add it to buildGoApplication too

This commit is contained in:
adisbladis 2022-06-13 20:48:54 +08:00
parent c3c8aa564d
commit ade4c20234

View file

@ -86,14 +86,9 @@ let
'' ''
); );
mkGoEnv = # Select Go attribute based on version specified in go.mod
{ pwd selectGo = attrs: goMod: attrs.go or (if goMod == null then pkgs.go else
}@attrs: (
let
goMod = parseGoMod (builtins.readFile "${builtins.toString pwd}/go.mod");
modulesStruct = builtins.fromTOML (builtins.readFile "${builtins.toString pwd}/gomod2nix.toml");
go = attrs.go or (
let let
goVersion = goMod.go; goVersion = goMod.go;
goAttr = "go_" + (lib.replaceStrings [ "." ] [ "_" ] goVersion); goAttr = "go_" + (lib.replaceStrings [ "." ] [ "_" ] goVersion);
@ -102,7 +97,17 @@ let
if builtins.hasAttr goAttr pkgs then pkgs.${goAttr} if builtins.hasAttr goAttr pkgs then pkgs.${goAttr}
else builtins.trace "go.mod specified Go version ${goVersion} but doesn't exist. Falling back to ${pkgs.go.version}." pkgs.go else builtins.trace "go.mod specified Go version ${goVersion} but doesn't exist. Falling back to ${pkgs.go.version}." pkgs.go
) )
); ));
mkGoEnv =
{ pwd
}@attrs:
let
goMod = parseGoMod (builtins.readFile "${builtins.toString pwd}/go.mod");
modulesStruct = builtins.fromTOML (builtins.readFile "${builtins.toString pwd}/gomod2nix.toml");
go = selectGo attrs goMod;
vendorEnv = mkVendorEnv { vendorEnv = mkVendorEnv {
inherit go modulesStruct; inherit go modulesStruct;
@ -147,7 +152,6 @@ let
buildGoApplication = buildGoApplication =
{ modules { modules
, go ? pkgs.go
, src , src
, pwd ? null , pwd ? null
, CGO_ENABLED ? "0" , CGO_ENABLED ? "0"
@ -160,7 +164,10 @@ let
let let
modulesStruct = builtins.fromTOML (builtins.readFile modules); modulesStruct = builtins.fromTOML (builtins.readFile modules);
goMod = parseGoMod (builtins.readFile "${builtins.toString pwd}/go.mod"); goMod =
if pwd != null
then parseGoMod (builtins.readFile "${builtins.toString pwd}/go.mod")
else null;
localReplaceCommands = localReplaceCommands =
let let
localReplaceAttrs = lib.filterAttrs (n: v: lib.hasAttr "path" v) goMod.replace; localReplaceAttrs = lib.filterAttrs (n: v: lib.hasAttr "path" v) goMod.replace;
@ -176,6 +183,8 @@ let
in in
if pwd != null then commands else [ ]; if pwd != null then commands else [ ];
go = selectGo attrs goMod;
removeReferences = [ ] ++ lib.optional (!allowGoReference) go; removeReferences = [ ] ++ lib.optional (!allowGoReference) go;
vendorEnv = mkVendorEnv { vendorEnv = mkVendorEnv {