1
1
mirror of https://github.com/divnix/digga.git synced 2024-11-23 20:22:04 +03:00
digga/lib/utils.nix
2020-07-30 22:17:28 -06:00

42 lines
1.2 KiB
Nix

{ lib, ... }:
let
inherit (builtins) attrNames isAttrs readDir listToAttrs;
inherit (lib) filterAttrs hasSuffix mapAttrs' nameValuePair removeSuffix;
# mapFilterAttrs ::
# (name -> value -> bool )
# (name -> value -> { name = any; value = any; })
# attrs
mapFilterAttrs = seive: f: attrs: filterAttrs seive (mapAttrs' f attrs);
# Generate an attribute set by mapping a function over a list of values.
genAttrs' = values: f: listToAttrs (map f values);
in
{
inherit mapFilterAttrs genAttrs';
recImport = { dir, _import ? base: import "${dir}/${base}.nix" }:
mapFilterAttrs
(_: v: v != null)
(n: v:
if n != "default.nix" && hasSuffix ".nix" n && v == "regular"
then
let name = removeSuffix ".nix" n; in nameValuePair (name) (_import name)
else
nameValuePair ("") (null))
(readDir dir);
# Convert a list to file paths to attribute set
# that has the filenames stripped of nix extension as keys
# and imported content of the file as value.
pathsToImportedAttrs = paths:
genAttrs' paths (path: {
name = removeSuffix ".nix" (baseNameOf path);
value = import path;
});
}