Skip to content

Instantly share code, notes, and snippets.

@zimbatm
Created December 30, 2019 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zimbatm/a639ece627d6bc86cb0675c16eec4cf7 to your computer and use it in GitHub Desktop.
Save zimbatm/a639ece627d6bc86cb0675c16eec4cf7 to your computer and use it in GitHub Desktop.
{ pkgs }@orig:
{ pkgs ? orig.pkgs, rootDir }:
with builtins;
let
mapAttrsMerge = pred: set:
let
mapF = name:
let
v = pred name set.${name};
in
if v == null then []
else [ { name = name; value = v; } ];
in
listToAttrs (concatMap mapF (attrNames set));
importers = {
"bake.nix" = (file: callPackage file {});
};
traverse = dir:
let
contents = builtins.readDir dir;
# TODO: ignore based on the top-level
eachEntry = name: type:
if name == ".git" || name == "build" then
null # ignore
else if type == "directory" then
traverse "${dir}/${name}"
else null # ignore
;
eachImporter = name: fn:
if contents ? "${name}" then
fn "${dir}/${name}"
else
{};
entries = mapAttrsMerge eachEntry contents;
imported = mapAttrs eachImporter importers;
merged =
foldl'
(a: b: a // b)
{}
([ entries ] ++ (attrValues imported));
in
merged;
callPackage = pkgs.newScope (
pkgs // {
root = root;
#callPackage = callPackage;
}
);
collectAll = val:
if pkgs.lib.isDerivation val then [ val ]
else if isAttrs val then
concatMap collectAll (attrValues val)
else []
;
root = traverse rootDir;
in
root // {
# contains all derivations of the tree
all = collectAll root;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment