From 44bb5b94954a15bb252eafbb26912cd37d208e83 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Sat, 28 May 2022 21:49:49 +0800 Subject: [PATCH] builder: Use new format for building --- builder/default.nix | 39 ++++++++++++++++++++++++++++----------- builder/fetch.sh | 10 ++++++++++ builder/symlink.go | 28 ++++++++++++---------------- 3 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 builder/fetch.sh diff --git a/builder/default.nix b/builder/default.nix index 53584ff..77937dd 100644 --- a/builder/default.nix +++ b/builder/default.nix @@ -1,10 +1,13 @@ { stdenv +, stdenvNoCC , runCommand , buildEnv , lib , fetchgit , removeReferencesTo , go +, jq +, cacert }: let @@ -12,6 +15,23 @@ let removeExpr = refs: ''remove-references-to ${lib.concatMapStrings (ref: " -t ${ref}") refs}''; + fetchGoModule = + { hash + , goPackagePath + , version + }: + stdenvNoCC.mkDerivation { + name = "${baseNameOf goPackagePath}_${version}"; + builder = ./fetch.sh; + inherit goPackagePath version; + nativeBuildInputs = [ go jq ]; + outputHashMode = "recursive"; + outputHashAlgo = null; + outputHash = hash; + SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt"; + impureEnvVars = lib.fetchers.proxyImpureEnvVars; + }; + buildGoApplication = { modules , src @@ -47,17 +67,14 @@ let nativeBuildInputs = [ go ]; json = builtins.toJSON modulesStruct; - sources = builtins.toJSON (lib.mapAttrs - (goPackagePath: meta: - let - src = fetchgit { - inherit (meta.fetch) url sha256 rev; - fetchSubmodules = true; - }; - srcPath = "${src}/${meta.relPath or ""}"; - in - srcPath) - modulesStruct); + sources = builtins.toJSON ( + lib.mapAttrs + (goPackagePath: meta: fetchGoModule { + goPackagePath = meta.replaced or goPackagePath; + inherit (meta) version hash; + }) + modulesStruct.mod + ); passAsFile = [ "json" "sources" ]; } diff --git a/builder/fetch.sh b/builder/fetch.sh new file mode 100644 index 0000000..c1aed36 --- /dev/null +++ b/builder/fetch.sh @@ -0,0 +1,10 @@ +source $stdenv/setup + +export HOME=$(mktemp -d) + +# Call once first outside of subshell for better error reporting +go mod download -x "$goPackagePath@$version" + +dir=$(go mod download --json "$goPackagePath@$version" | jq -r .Dir) + +cp -r $dir $out diff --git a/builder/symlink.go b/builder/symlink.go index d37206e..2f22cf7 100644 --- a/builder/symlink.go +++ b/builder/symlink.go @@ -9,23 +9,21 @@ import ( "sort" ) -type fetchInfo struct { - Type string `toml:"type"` - URL string `toml:"url"` - Rev string `toml:"rev"` - Sha256 string `toml:"sha256"` +type Package struct { + GoPackagePath string `json:"-"` + Version string `json:"version"` + Hash string `json:"hash"` + ReplacedPath string `json:"replaced,omitempty"` } -type packageT struct { - SumVersion string `toml:"sumVersion"` - RelPath string `toml:"relPath,omitempty"` - VendorPath string `toml:"vendorPath,omitempty"` - Fetch *fetchInfo `toml:"fetch"` +type Output struct { + SchemaVersion int `json:"schema"` + Mod map[string]*Package `json:"mod"` } func main() { - pkgs := make(map[string]*packageT) + var output Output sources := make(map[string]string) b, err := ioutil.ReadFile(os.Getenv("sourcesPath")) @@ -43,11 +41,13 @@ func main() { panic(err) } - err = json.Unmarshal(b, &pkgs) + err = json.Unmarshal(b, &output) if err != nil { panic(err) } + pkgs := output.Mod + keys := make([]string, 0, len(pkgs)) for key := range pkgs { keys = append(keys, key) @@ -58,12 +58,8 @@ func main() { for i := len(keys) - 1; i >= 0; i-- { key := keys[i] src := sources[key] - pkg := pkgs[key] paths := []string{key} - if pkg.VendorPath != "" { - paths = append(paths, pkg.VendorPath) - } for _, path := range paths {