Merge pull request #10 from tazjin/feat/buildGo-dot-nix
Introduce Bazel-style Nix build system for Go
This commit is contained in:
commit
9d6792609f
6 changed files with 93 additions and 13 deletions
4
overrides/buildGo.nix
Normal file
4
overrides/buildGo.nix
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
import "${builtins.fetchGit {
|
||||||
|
url = "https://github.com/tazjin/buildGo.nix";
|
||||||
|
rev = "28e587b348a8aaa7af00a004c05286af9d35ca9a";
|
||||||
|
}}/buildGo.nix"
|
||||||
|
|
@ -1,29 +1,25 @@
|
||||||
path: { pkgs, ... } @ args:
|
initPath: { pkgs, ... } @ args:
|
||||||
|
|
||||||
let
|
let
|
||||||
inherit (builtins)
|
inherit (builtins)
|
||||||
attrNames
|
attrNames
|
||||||
attrValues
|
|
||||||
filter
|
filter
|
||||||
head
|
head
|
||||||
isString
|
isString
|
||||||
|
length
|
||||||
listToAttrs
|
listToAttrs
|
||||||
map
|
map
|
||||||
match
|
match
|
||||||
readDir
|
readDir
|
||||||
|
split
|
||||||
tail
|
tail
|
||||||
toPath
|
toPath
|
||||||
toString;
|
toString;
|
||||||
|
|
||||||
zipAttrs = names: values:
|
attrsToList = attrs: map (name: {
|
||||||
if (names == []) || (values == [])
|
inherit name;
|
||||||
then []
|
value = attrs."${name}";
|
||||||
else [{
|
}) (attrNames attrs);
|
||||||
name = head names;
|
|
||||||
value = head values;
|
|
||||||
}] ++ zipAttrs (tail names) (tail values);
|
|
||||||
|
|
||||||
attrsToList = attrs: zipAttrs (attrNames attrs) (attrValues attrs);
|
|
||||||
|
|
||||||
isFile = s: s == "regular";
|
isFile = s: s == "regular";
|
||||||
isDir = s: s == "directory";
|
isDir = s: s == "directory";
|
||||||
|
|
@ -44,6 +40,23 @@ let
|
||||||
}) files;
|
}) files;
|
||||||
in filter (f: isString f.name) nixFiles;
|
in filter (f: isString f.name) nixFiles;
|
||||||
|
|
||||||
|
# Some packages require that their position in the tree is passed in
|
||||||
|
# as an argument. To do this the root directory (i.e. $PWD during
|
||||||
|
# imports) is chopped off the front of the path components in
|
||||||
|
# imports.
|
||||||
|
pathParts = p: tail (filter isString (split "/" (toString p)));
|
||||||
|
initLen = length (pathParts ./.);
|
||||||
|
drop = n: l:
|
||||||
|
if n == 0
|
||||||
|
then l
|
||||||
|
else if l == []
|
||||||
|
then []
|
||||||
|
else drop (n - 1) (tail l);
|
||||||
|
|
||||||
|
argsWithPath = args: parts: args // {
|
||||||
|
locatedAt = drop initLen parts;
|
||||||
|
};
|
||||||
|
|
||||||
traverse = path: dir:
|
traverse = path: dir:
|
||||||
let nixFiles = filterNixFiles dir;
|
let nixFiles = filterNixFiles dir;
|
||||||
imported = map (f: {
|
imported = map (f: {
|
||||||
|
|
@ -58,8 +71,8 @@ let
|
||||||
|
|
||||||
importOr = path: dir: f:
|
importOr = path: dir: f:
|
||||||
if dir ? "default.nix"
|
if dir ? "default.nix"
|
||||||
then import path args
|
then import path (argsWithPath args (pathParts path))
|
||||||
else f path (attrsToList dir);
|
else f path (attrsToList dir);
|
||||||
|
|
||||||
readTree = path: importOr path (readDir path) traverse;
|
readTree = path: importOr path (readDir path) traverse;
|
||||||
in readTree path
|
in readTree initPath
|
||||||
|
|
|
||||||
27
tools/gotest/default.nix
Normal file
27
tools/gotest/default.nix
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
# This file demonstrates how to make use of pkgs.buildGo.
|
||||||
|
#
|
||||||
|
# It introduces libraries and protobuf support, however gRPC support
|
||||||
|
# is not yet included.
|
||||||
|
#
|
||||||
|
# From the root of this repository this example can be built with
|
||||||
|
# `nix-build -A tools.gotest`
|
||||||
|
{ pkgs, ... }:
|
||||||
|
|
||||||
|
let
|
||||||
|
inherit (pkgs) buildGo;
|
||||||
|
|
||||||
|
somelib = buildGo.package {
|
||||||
|
name = "somelib";
|
||||||
|
srcs = [ ./lib.go ];
|
||||||
|
};
|
||||||
|
|
||||||
|
someproto = buildGo.proto {
|
||||||
|
name = "someproto";
|
||||||
|
proto = ./test.proto;
|
||||||
|
};
|
||||||
|
|
||||||
|
in buildGo.program {
|
||||||
|
name = "gotest";
|
||||||
|
srcs = [ ./main.go ];
|
||||||
|
deps = [ somelib someproto ];
|
||||||
|
} // { meta.enableCI = true; }
|
||||||
11
tools/gotest/lib.go
Normal file
11
tools/gotest/lib.go
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
package somelib
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func Name() string {
|
||||||
|
return "edef"
|
||||||
|
}
|
||||||
|
|
||||||
|
func Greet(s string) string {
|
||||||
|
return fmt.Sprintf("Hello %s", s)
|
||||||
|
}
|
||||||
16
tools/gotest/main.go
Normal file
16
tools/gotest/main.go
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
// This program just exists to import some libraries and demonstrate
|
||||||
|
// that the build works, it doesn't do anything useful.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"somelib"
|
||||||
|
"someproto"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
p := someproto.Person{
|
||||||
|
Name: somelib.Name(),
|
||||||
|
}
|
||||||
|
fmt.Println(somelib.Greet(fmt.Sprintf("%v", p)))
|
||||||
|
}
|
||||||
9
tools/gotest/test.proto
Normal file
9
tools/gotest/test.proto
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
syntax = "proto3";
|
||||||
|
package someproto;
|
||||||
|
|
||||||
|
import "google/protobuf/timestamp.proto";
|
||||||
|
|
||||||
|
message Person {
|
||||||
|
string name = 1;
|
||||||
|
google.protobuf.Timestamp last_updated = 2;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue