Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gomod2nix: init at 1.5.0 #188272

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
435 changes: 435 additions & 0 deletions pkgs/build-support/go/gomod2nix/default.nix

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions pkgs/build-support/go/gomod2nix/fetch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
source $stdenv/setup

export HOME=$(mktemp -d)

# Call once first outside of subshell for better error reporting
go mod download "$goPackagePath@$version"

dir=$(go mod download --json "$goPackagePath@$version" | jq -r .Dir)

chmod -R +w $dir
find $dir -iname ".ds_store" | xargs -r rm -rf

cp -r $dir $out
59 changes: 59 additions & 0 deletions pkgs/build-support/go/gomod2nix/install/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"fmt"
"go/parser"
"go/token"
"io"
"os"
"os/exec"
"strconv"
)

const filename = "tools.go"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it actually enforced that the file is called tools.go or just that the build tag is tools?

https://marcofranssen.nl/manage-go-tools-via-go-modules

// +build tools

I can't really find any docs on this


func main() {
fset := token.NewFileSet()

var src []byte
{
f, err := os.Open(filename)
if err != nil {
panic(err)
}

src, err = io.ReadAll(f)
if err != nil {
panic(err)
}
}

f, err := parser.ParseFile(fset, filename, src, parser.ImportsOnly)
if err != nil {
fmt.Println(err)
return
}

for _, s := range f.Imports {
path, err := strconv.Unquote(s.Path.Value)
if err != nil {
panic(err)
}

cmd := exec.Command("go", "install", path)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

fmt.Printf("Executing '%s'\n", cmd)

err = cmd.Start()
if err != nil {
panic(err)
}

err = cmd.Wait()
if err != nil {
panic(err)
}
}
}
173 changes: 173 additions & 0 deletions pkgs/build-support/go/gomod2nix/parser.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
# Parse go.mod in Nix
# Returns a Nix structure with the contents of the go.mod passed in
# in normalised form.

let
inherit (builtins) elemAt mapAttrs split foldl' match filter typeOf hasAttr length;

# Strip lines with comments & other junk
stripStr = s: elemAt (split "^ *" (elemAt (split " *$" s) 0)) 2;
stripLines = initialLines: foldl' (acc: f: f acc) initialLines [
# Strip comments
(lines: map
(l: stripStr (elemAt (splitString "//" l) 0))
lines)

# Strip leading tabs characters
(lines: map (l: elemAt (match "(\t)?(.*)" l) 1) lines)

# Filter empty lines
(filter (l: l != ""))
];

# Parse lines into a structure
parseLines = lines: (foldl'
(acc: l:
let
m = match "([^ )]*) *(.*)" l; # Match the current line
directive = elemAt m 0; # The directive (replace, require & so on)
rest = elemAt m 1; # The rest of the current line

# Maintain parser state (inside parens or not)
adisbladis marked this conversation as resolved.
Show resolved Hide resolved
inDirective =
if rest == "(" then directive
else if rest == ")" then null
else acc.inDirective
;

in
{
data = (acc.data // (
# If a we're in a directive and it's closing, no-op
if directive == "" && rest == ")" then { }

# If a directive is opening create the directive attrset
else if inDirective != null && rest == "(" && ! hasAttr inDirective acc.data then {
${inDirective} = { };
}

# If we're closing any paren, no-op
else if rest == "(" || rest == ")" then { }

# If we're in a directive that has rest data assign it to the directive in the output
else if inDirective != null then {
${inDirective} = acc.data.${inDirective} // { ${directive} = rest; };
}

# Replace directive has unique structure and needs special casing
else if directive == "replace" then
(
let
# Split `foo => bar` into segments
segments = split " => " rest;
getSegment = elemAt segments;
in
assert length segments == 3; {
# Assert well formed
replace = acc.data.replace // {
# Structure segments into attrset
${getSegment 0} = "=> ${getSegment 2}";
};
}
)

# The default operation is to just assign the value
else {
${directive} = rest;
}
)
);
inherit inDirective;
})
{
# Default foldl' state
inDirective = null;
# The actual return data we're interested in (default empty structure)
data = {
require = { };
replace = { };
exclude = { };
};
}
lines
).data;

# Normalise directives no matter what syntax produced them
# meaning that:
# replace github.com/nix-community/trustix/packages/go-lib => ../go-lib
#
# and:
# replace (
# github.com/nix-community/trustix/packages/go-lib => ../go-lib
# )
#
# gets the same structural representation.
#
# Addtionally this will create directives that are entirely missing from go.mod
# as an empty attrset so it's output is more consistent.
normaliseDirectives = data: (
let
normaliseString = s:
let
m = builtins.match "([^ ]+) (.+)" s;
in
{
${elemAt m 0} = elemAt m 1;
};
require = data.require or { };
replace = data.replace or { };
exclude = data.exclude or { };
in
data // {
require =
if typeOf require == "string" then normaliseString require
else require;
replace =
if typeOf replace == "string" then normaliseString replace
else replace;
}
);

# Parse versions and dates from go.mod version string
parseVersion = ver:
let
m = elemAt (match "([^-]+)-?([^-]*)-?([^-]*)" ver);
v = elemAt (match "([^+]+)\\+?(.*)" (m 0));
in
{
version = v 0;
versionSuffix = v 1;
date = m 1;
rev = m 2;
};

# Parse package paths & versions from replace directives
parseReplace = data: (
data // {
replace =
mapAttrs
(_: v:
let
m = match "=> ([^ ]+) (.+)" v;
m2 = match "=> ([^ ]+)" v;
in
if m != null then {
goPackagePath = elemAt m 0;
version = elemAt m 1;
} else {
path = elemAt m2 0;
})
data.replace;
}
);

splitString = sep: s: filter (t: t != [ ]) (split sep s);

in
contents:
foldl' (acc: f: f acc) (splitString "\n" contents) [
stripLines
parseLines
normaliseDirectives
parseReplace
]
109 changes: 109 additions & 0 deletions pkgs/build-support/go/gomod2nix/symlink/symlink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package main
adisbladis marked this conversation as resolved.
Show resolved Hide resolved

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
)

type Package struct {
GoPackagePath string `json:"-"`
Version string `json:"version"`
Hash string `json:"hash"`
ReplacedPath string `json:"replaced,omitempty"`
}

type Sources map[string]string

func populateStruct(path string, data interface{}) {
pathVal := os.Getenv(path)
if len(path) == 0 {
panic(fmt.Sprintf("env var '%s' was unset", path))
}
path = pathVal

b, err := os.ReadFile(path)
if err != nil {
panic(err)
}

err = json.Unmarshal(b, &data)
if err != nil {
panic(err)
}
}

func main() {
sources := make(Sources)
pkgs := make(map[string]*Package)

populateStruct("sourcesPath", &sources)

populateStruct("jsonPath", &pkgs)

keys := make([]string, 0, len(pkgs))
for key := range pkgs {
keys = append(keys, key)
}
sort.Strings(keys)

makeSymlinks(keys, sources)
}

func makeSymlinks(keys []string, sources Sources) {
// Iterate, in reverse order
for i := len(keys) - 1; i >= 0; i-- {
key := keys[i]
src := sources[key]

paths := []string{key}

for _, path := range paths {
vendorDir := filepath.Join("vendor", filepath.Dir(path))
if err := os.MkdirAll(vendorDir, 0o755); err != nil {
panic(err)
}

vendorPath := filepath.Join("vendor", path)
if _, err := os.Stat(vendorPath); err == nil {
populateVendorPath(vendorPath, src)
continue
}

// If the file doesn't already exist, just create a simple symlink
err := os.Symlink(src, vendorPath)
if err != nil {
panic(err)
}
}
}
}

func populateVendorPath(vendorPath string, src string) {
files, err := os.ReadDir(src)
if err != nil {
panic(err)
}

for _, f := range files {
innerSrc := filepath.Join(src, f.Name())
dst := filepath.Join(vendorPath, f.Name())
if err := os.Symlink(innerSrc, dst); err != nil {
// assume it's an existing directory, try to link the directory content instead.
// TODO should we do this recursively?
files, err := os.ReadDir(innerSrc)
if err != nil {
panic(err)
}
for _, f := range files {
srcFile := filepath.Join(innerSrc, f.Name())
dstFile := filepath.Join(dst, f.Name())
if err := os.Symlink(srcFile, dstFile); err != nil {
fmt.Println("ignoring symlink error", srcFile, dstFile)
}
}
}
}
}
Loading