about summary refs log tree commit diff
path: root/read-tree.nix
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2019-11-15T23·46+0000
committerGitHub <noreply@github.com>2019-11-15T23·46+0000
commitae53bf30c3306eeb56731e6e7aefc2bab278c6e0 (patch)
treeed66073f8c7dc2f01814ae8cc786bdf32988f0bd /read-tree.nix
parent9ba4bbb60954c3fafb5e5f0aa5f8ff478c09a600 (diff)
parentecd54d58b1863ccd84e6a85b161fb1ef066e5efd (diff)
Merge pull request #9 from tazjin/feat/read-tree r/95
Configure automatic package layouts via repository structure
Diffstat (limited to 'read-tree.nix')
-rw-r--r--read-tree.nix65
1 files changed, 65 insertions, 0 deletions
diff --git a/read-tree.nix b/read-tree.nix
new file mode 100644
index 000000000000..d883d12c8171
--- /dev/null
+++ b/read-tree.nix
@@ -0,0 +1,65 @@
+path: { pkgs, ... } @ args:
+
+let
+  inherit (builtins)
+    attrNames
+    attrValues
+    filter
+    head
+    isString
+    listToAttrs
+    map
+    match
+    readDir
+    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);
+
+  isFile = s: s == "regular";
+  isDir = s: s == "directory";
+
+  joinPath = p: f: toPath ((toString p) + "/" + f);
+
+  isNixFile = file:
+    let res = match "(.*)\.nix" file;
+    in if res == null then null else head res;
+
+  filterNixFiles = dir:
+    let files = filter (e: isFile e.value) dir;
+        nixFiles = map (f: {
+          # Name and value are intentionally flipped to get the
+          # correct attribute set structure back out
+          name = isNixFile f.name;
+          value = f.name; # i.e. the path
+        }) files;
+    in filter (f: isString f.name) nixFiles;
+
+  traverse = path: dir:
+    let nixFiles = filterNixFiles dir;
+        imported = map (f: {
+          inherit (f) name;
+          value = import (joinPath path f.value) args;
+        }) nixFiles;
+        dirs = map (d: {
+          inherit (d) name;
+          value = readTree (joinPath path d.name);
+        }) (filter (e: isDir e.value) dir);
+    in listToAttrs (imported ++ dirs);
+
+  importOr = path: dir: f:
+    if dir ? "default.nix"
+      then import path args
+      else f path (attrsToList dir);
+
+  readTree = path: importOr path (readDir path) traverse;
+in readTree path