tvl-depot/read-tree.nix
Vincent Ambo c1c379848a chore(nix): Move files around to conform to new read-tree layout
Broadly speaking, the following things are included:

* there is now a uniform `args` struct that is passed to all
  derivations, package headers have been changed appropriately
* overrides are now loaded from a separate `override` folder just
  using read-tree.nix
* third-party packages have moved into the `third_party` attribute set
2019-11-15 15:26:08 +00:00

65 lines
1.6 KiB
Nix

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