forked from mirrors/gomod2nix
builder: Use new format for building
This commit is contained in:
parent
53270b071e
commit
44bb5b9495
3 changed files with 50 additions and 27 deletions
|
@ -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" ];
|
||||
}
|
||||
|
|
10
builder/fetch.sh
Normal file
10
builder/fetch.sh
Normal 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
|
|
@ -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 {
|
||||
|
||||
|
|
Loading…
Reference in a new issue