about summary refs log tree commit diff
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-11-27T15·47+0000
committerGitHub <noreply@github.com>2019-11-27T15·47+0000
commit9d6792609f0de0c3c9209f300756024f8c3524da (patch)
tree5074383f42d4ec838a96834729ee6bd7deb90058
parentae53bf30c3306eeb56731e6e7aefc2bab278c6e0 (diff)
parent0bd085f5d6602ab75689156b92af04e7f83d69fd (diff)
Merge pull request #10 from tazjin/feat/buildGo-dot-nix r/96
Introduce Bazel-style Nix build system for Go
-rw-r--r--overrides/buildGo.nix4
-rw-r--r--read-tree.nix39
-rw-r--r--tools/gotest/default.nix27
-rw-r--r--tools/gotest/lib.go11
-rw-r--r--tools/gotest/main.go16
-rw-r--r--tools/gotest/test.proto9
6 files changed, 93 insertions, 13 deletions
diff --git a/overrides/buildGo.nix b/overrides/buildGo.nix
new file mode 100644
index 0000000000..cbccfcec97
--- /dev/null
+++ b/overrides/buildGo.nix
@@ -0,0 +1,4 @@
+import "${builtins.fetchGit {
+  url = "https://github.com/tazjin/buildGo.nix";
+  rev = "28e587b348a8aaa7af00a004c05286af9d35ca9a";
+}}/buildGo.nix"
diff --git a/read-tree.nix b/read-tree.nix
index d883d12c81..d742c69ea4 100644
--- a/read-tree.nix
+++ b/read-tree.nix
@@ -1,29 +1,25 @@
-path: { pkgs, ... } @ args:
+initPath: { pkgs, ... } @ args:
 
 let
   inherit (builtins)
     attrNames
-    attrValues
     filter
     head
     isString
+    length
     listToAttrs
     map
     match
     readDir
+    split
     tail
     toPath
     toString;
 
-  zipAttrs = names: values:
-    if (names == []) || (values == [])
-    then []
-    else [{
-      name = head names;
-      value = head values;
-    }] ++ zipAttrs (tail names) (tail values);
-
-  attrsToList = attrs: zipAttrs (attrNames attrs) (attrValues attrs);
+  attrsToList = attrs: map (name: {
+    inherit name;
+    value = attrs."${name}";
+  }) (attrNames attrs);
 
   isFile = s: s == "regular";
   isDir = s: s == "directory";
@@ -44,6 +40,23 @@ let
         }) files;
     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:
     let nixFiles = filterNixFiles dir;
         imported = map (f: {
@@ -58,8 +71,8 @@ let
 
   importOr = path: dir: f:
     if dir ? "default.nix"
-      then import path args
+      then import path (argsWithPath args (pathParts path))
       else f path (attrsToList dir);
 
   readTree = path: importOr path (readDir path) traverse;
-in readTree path
+in readTree initPath
diff --git a/tools/gotest/default.nix b/tools/gotest/default.nix
new file mode 100644
index 0000000000..168d15748e
--- /dev/null
+++ b/tools/gotest/default.nix
@@ -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; }
diff --git a/tools/gotest/lib.go b/tools/gotest/lib.go
new file mode 100644
index 0000000000..0aeebb2aea
--- /dev/null
+++ b/tools/gotest/lib.go
@@ -0,0 +1,11 @@
+package somelib
+
+import "fmt"
+
+func Name() string {
+	return "edef"
+}
+
+func Greet(s string) string {
+	return fmt.Sprintf("Hello %s", s)
+}
diff --git a/tools/gotest/main.go b/tools/gotest/main.go
new file mode 100644
index 0000000000..99218c0776
--- /dev/null
+++ b/tools/gotest/main.go
@@ -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)))
+}
diff --git a/tools/gotest/test.proto b/tools/gotest/test.proto
new file mode 100644
index 0000000000..76af63072b
--- /dev/null
+++ b/tools/gotest/test.proto
@@ -0,0 +1,9 @@
+syntax = "proto3";
+package someproto;
+
+import "google/protobuf/timestamp.proto";
+
+message Person {
+  string name = 1;
+  google.protobuf.Timestamp last_updated = 2;
+}