From ade4c20234d6362b3d6688ae09dde7ad3ca3b580 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Mon, 13 Jun 2022 20:48:54 +0800 Subject: [PATCH 1/2] Factor out Go version selection from mkGoEnv and add it to buildGoApplication too --- builder/default.nix | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/builder/default.nix b/builder/default.nix index 4c753a6..5602e0e 100644 --- a/builder/default.nix +++ b/builder/default.nix @@ -86,6 +86,20 @@ let '' ); + # Select Go attribute based on version specified in go.mod + selectGo = attrs: goMod: attrs.go or (if goMod == null then pkgs.go else + ( + let + goVersion = goMod.go; + goAttr = "go_" + (lib.replaceStrings [ "." ] [ "_" ] goVersion); + in + ( + 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 + ) + )); + + mkGoEnv = { pwd }@attrs: @@ -93,16 +107,7 @@ 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 - goVersion = goMod.go; - goAttr = "go_" + (lib.replaceStrings [ "." ] [ "_" ] goVersion); - in - ( - 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 - ) - ); + go = selectGo attrs goMod; vendorEnv = mkVendorEnv { inherit go modulesStruct; @@ -147,7 +152,6 @@ let buildGoApplication = { modules - , go ? pkgs.go , src , pwd ? null , CGO_ENABLED ? "0" @@ -160,7 +164,10 @@ let let 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 = let localReplaceAttrs = lib.filterAttrs (n: v: lib.hasAttr "path" v) goMod.replace; @@ -176,6 +183,8 @@ let in if pwd != null then commands else [ ]; + go = selectGo attrs goMod; + removeReferences = [ ] ++ lib.optional (!allowGoReference) go; vendorEnv = mkVendorEnv { From aae59a6851a5ece674f4e64546d62fd453633b88 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Mon, 13 Jun 2022 20:49:36 +0800 Subject: [PATCH 2/2] Don't set CGO_ENABLED by default Instead let the compiler decide. This will result in less cryptic errors. --- builder/default.nix | 2 -- 1 file changed, 2 deletions(-) diff --git a/builder/default.nix b/builder/default.nix index 5602e0e..23ff061 100644 --- a/builder/default.nix +++ b/builder/default.nix @@ -154,7 +154,6 @@ let { modules , src , pwd ? null - , CGO_ENABLED ? "0" , nativeBuildInputs ? [ ] , allowGoReference ? false , meta ? { } @@ -195,7 +194,6 @@ let nativeBuildInputs = [ removeReferencesTo go ] ++ nativeBuildInputs; inherit (go) GOOS GOARCH; - inherit CGO_ENABLED; GO_NO_VENDOR_CHECKS = "1";