From 36f0c546554b25cc06dbfc89d3a9af3237afe658 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Fri, 2 Sep 2022 16:34:23 +1200 Subject: [PATCH] Use the highest compatible Go version whenever possible It should be generally safe to compile any older Go project with a newer compiler and in the few cases where it isn't it could be manually passed. --- builder/default.nix | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/builder/default.nix b/builder/default.nix index 44a92ba..3861f3e 100644 --- a/builder/default.nix +++ b/builder/default.nix @@ -126,16 +126,22 @@ let '' ); - # Select Go attribute based on version specified in go.mod + # Return a Go attribute and error out if the Go version is older than was specified in go.mod. selectGo = attrs: goMod: attrs.go or (if goMod == null then pkgs.go else ( let goVersion = goMod.go; - goAttr = "go_" + (replaceStrings [ "." ] [ "_" ] goVersion); + goAttrs = lib.reverseList (builtins.filter + ( + attr: lib.hasPrefix "go_" attr && lib.versionAtLeast pkgs.${attr}.version goVersion + ) + (lib.attrNames pkgs)); + goAttr = elemAt goAttrs 0; in ( - if hasAttr goAttr pkgs then pkgs.${goAttr} - else trace "go.mod specified Go version ${goVersion} but doesn't exist. Falling back to ${pkgs.go.version}." pkgs.go + if goAttrs != [ ] + then pkgs.${goAttr} + else throw "go.mod specified Go version ${goVersion}, but no compatible Go attribute could be found." ) ));