diff options
-rw-r--r-- | overrides/buildGo.nix | 4 | ||||
-rw-r--r-- | read-tree.nix | 39 | ||||
-rw-r--r-- | tools/gotest/default.nix | 27 | ||||
-rw-r--r-- | tools/gotest/lib.go | 11 | ||||
-rw-r--r-- | tools/gotest/main.go | 16 | ||||
-rw-r--r-- | tools/gotest/test.proto | 9 |
6 files changed, 93 insertions, 13 deletions
diff --git a/overrides/buildGo.nix b/overrides/buildGo.nix new file mode 100644 index 000000000000..cbccfcec976d --- /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 d883d12c8171..d742c69ea411 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 000000000000..168d15748e1f --- /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 000000000000..0aeebb2aea69 --- /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 000000000000..99218c077617 --- /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 000000000000..76af63072be3 --- /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; +} |