about summary refs log tree commit diff
path: root/buildGo.nix
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2019-12-09T00·27+0000
committerVincent Ambo <mail@tazj.in>2019-12-13T00·39+0000
commitc40e8a4061b033282031cd1a8d64df9a591aa694 (patch)
treea8acced924a10b62c062fae326c9bec96b659fd0 /buildGo.nix
parent6a526620e25a349642b161eb687dec2e005360e5 (diff)
feat(buildGo): Add new traversing external' implementation
Adds an alternative implementation of a builder for external packages
which traverses packages and builds up an attribute set tree out of
their structure.

Currently this is not functional because there is no useable method of
specifying dependencies within that package set.
Diffstat (limited to 'buildGo.nix')
-rw-r--r--buildGo.nix55
1 files changed, 46 insertions, 9 deletions
diff --git a/buildGo.nix b/buildGo.nix
index 5f5118e8ce..366be770a9 100644
--- a/buildGo.nix
+++ b/buildGo.nix
@@ -14,10 +14,12 @@ let
     dirOf
     elemAt
     filter
+    listToAttrs
     map
     match
     readDir
-    replaceStrings;
+    replaceStrings
+    toString;
 
   inherit (pkgs) lib go runCommand fetchFromGitHub protobuf symlinkJoin;
 
@@ -34,14 +36,6 @@ let
   srcCopy = path: src: "cp ${src} $out/${path}/${srcBasename src}";
   srcList = path: srcs: lib.concatStringsSep "\n" (map (srcCopy path) srcs);
 
-  isGoFile = f: (match ".*\.go" f) != null;
-  isGoTest = f: (match ".*_test\.go" f) != null;
-  goFileFilter = k: v: (v == "regular") && (isGoFile k) && (!isGoTest k);
-  goFilesIn = dir:
-    let files = readDir dir;
-        goFiles = filter (f: goFileFilter f files."${f}") (attrNames files);
-    in map (f: dir + "/" + f) goFiles;
-
   allDeps = deps: lib.unique (lib.flatten (deps ++ (map (d: d.goDeps) deps)));
 
   xFlags = x_defs: spaceOut (map (k: "-X ${k}=${x_defs."${k}"}") (attrNames x_defs));
@@ -93,6 +87,41 @@ let
   # Build a Go library out of the specified gRPC definition.
   grpc = args: proto (args // { extraDeps = [ protoLibs.goGrpc ]; });
 
+  # Traverse an externally defined Go library to build up a tree of
+  # its packages.
+  #
+  # TODO(tazjin): Automatically infer which packages depend on which
+  # other packages, which currently requires overriding.
+  #
+  # TODO(tazjin): Add support for rewriting package paths.
+  external' = { src, path, deps ? [] }:
+    let
+      dir = readDir src;
+      isGoFile = f: (match ".*\.go" f) != null;
+      isGoTest = f: (match ".*_test\.go" f) != null;
+      goFileFilter = k: v: (v == "regular") && (isGoFile k) && (!isGoTest k);
+      goSources =
+        let goFiles = filter (f: goFileFilter f dir."${f}") (attrNames dir);
+        in map (f: src + ("/" + f)) goFiles;
+
+      subDirs = filter (n: dir."${n}" == "directory") (attrNames dir);
+      subPackages = map (name: {
+        inherit name;
+        value = external' {
+          inherit deps;
+          src = src + ("/" + name);
+          path = path + ("/" + name);
+        };
+      }) subDirs;
+      subAttrs = listToAttrs (filter (p: p.value != {}) subPackages);
+
+      current = package {
+        inherit deps path;
+        name = pathToName path;
+        srcs = goSources;
+      };
+    in if goSources == [] then subAttrs else (current // subAttrs);
+
   # Build an externally defined Go library using `go build` itself.
   #
   # Libraries built this way can be included in any standard buildGo
@@ -144,4 +173,12 @@ in {
   proto = makeOverridable proto;
   grpc = makeOverridable grpc;
   external = makeOverridable external;
+
+  # TODO: remove
+  inherit external';
+
+  extTest = external' {
+    src = /home/tazjin/go/src/cloud.google.com/go;
+    path = "cloud.google.com/go";
+  };
 }