builder: Use new format for building

This commit is contained in:
adisbladis 2022-05-28 21:49:49 +08:00
parent 53270b071e
commit 44bb5b9495
3 changed files with 50 additions and 27 deletions

View file

@ -1,10 +1,13 @@
{ stdenv { stdenv
, stdenvNoCC
, runCommand , runCommand
, buildEnv , buildEnv
, lib , lib
, fetchgit , fetchgit
, removeReferencesTo , removeReferencesTo
, go , go
, jq
, cacert
}: }:
let let
@ -12,6 +15,23 @@ let
removeExpr = refs: ''remove-references-to ${lib.concatMapStrings (ref: " -t ${ref}") refs}''; 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 = buildGoApplication =
{ modules { modules
, src , src
@ -47,17 +67,14 @@ let
nativeBuildInputs = [ go ]; nativeBuildInputs = [ go ];
json = builtins.toJSON modulesStruct; json = builtins.toJSON modulesStruct;
sources = builtins.toJSON (lib.mapAttrs sources = builtins.toJSON (
(goPackagePath: meta: lib.mapAttrs
let (goPackagePath: meta: fetchGoModule {
src = fetchgit { goPackagePath = meta.replaced or goPackagePath;
inherit (meta.fetch) url sha256 rev; inherit (meta) version hash;
fetchSubmodules = true; })
}; modulesStruct.mod
srcPath = "${src}/${meta.relPath or ""}"; );
in
srcPath)
modulesStruct);
passAsFile = [ "json" "sources" ]; passAsFile = [ "json" "sources" ];
} }

10
builder/fetch.sh Normal file
View file

@ -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

View file

@ -9,23 +9,21 @@ import (
"sort" "sort"
) )
type fetchInfo struct { type Package struct {
Type string `toml:"type"` GoPackagePath string `json:"-"`
URL string `toml:"url"` Version string `json:"version"`
Rev string `toml:"rev"` Hash string `json:"hash"`
Sha256 string `toml:"sha256"` ReplacedPath string `json:"replaced,omitempty"`
} }
type packageT struct { type Output struct {
SumVersion string `toml:"sumVersion"` SchemaVersion int `json:"schema"`
RelPath string `toml:"relPath,omitempty"` Mod map[string]*Package `json:"mod"`
VendorPath string `toml:"vendorPath,omitempty"`
Fetch *fetchInfo `toml:"fetch"`
} }
func main() { func main() {
pkgs := make(map[string]*packageT) var output Output
sources := make(map[string]string) sources := make(map[string]string)
b, err := ioutil.ReadFile(os.Getenv("sourcesPath")) b, err := ioutil.ReadFile(os.Getenv("sourcesPath"))
@ -43,11 +41,13 @@ func main() {
panic(err) panic(err)
} }
err = json.Unmarshal(b, &pkgs) err = json.Unmarshal(b, &output)
if err != nil { if err != nil {
panic(err) panic(err)
} }
pkgs := output.Mod
keys := make([]string, 0, len(pkgs)) keys := make([]string, 0, len(pkgs))
for key := range pkgs { for key := range pkgs {
keys = append(keys, key) keys = append(keys, key)
@ -58,12 +58,8 @@ func main() {
for i := len(keys) - 1; i >= 0; i-- { for i := len(keys) - 1; i >= 0; i-- {
key := keys[i] key := keys[i]
src := sources[key] src := sources[key]
pkg := pkgs[key]
paths := []string{key} paths := []string{key}
if pkg.VendorPath != "" {
paths = append(paths, pkg.VendorPath)
}
for _, path := range paths { for _, path := range paths {