lib: create 'my' extensions
Inspired by this [1] repo, I want to add some functions to `lib.my`. I would have liked to make it work without having to use an overlay, but I did not manage to do it... [1]: https://github.com/hlissner/dotfiles
This commit is contained in:
parent
11b25a0e0e
commit
331b7cf8e3
4 changed files with 63 additions and 3 deletions
7
lib/attrs.nix
Normal file
7
lib/attrs.nix
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
{ lib, ... }:
|
||||
let
|
||||
inherit (lib) filterAttrs mapAttrs';
|
||||
in
|
||||
{
|
||||
mapFilterAttrs = pred: f: attrs: filterAttrs pred (mapAttrs' f attrs);
|
||||
}
|
||||
21
lib/default.nix
Normal file
21
lib/default.nix
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
# Extension mechanism shamelessly stolen from [1].
|
||||
#
|
||||
# [1]: https://github.com/hlissner/dotfiles/blob/master/lib/default.nix
|
||||
{ lib, pkgs, inputs }:
|
||||
let
|
||||
inherit (lib) makeExtensible attrValues foldr;
|
||||
inherit (modules) mapModules;
|
||||
|
||||
modules = import ./modules.nix {
|
||||
inherit lib;
|
||||
self.attrs = import ./attrs.nix { inherit lib; self = { }; };
|
||||
};
|
||||
|
||||
mylib = makeExtensible (self:
|
||||
with self; mapModules ./.
|
||||
(file: import file { inherit self lib pkgs inputs; })
|
||||
);
|
||||
in
|
||||
mylib.extend (self: super:
|
||||
foldr (a: b: a // b) { } (attrValues super)
|
||||
)
|
||||
23
lib/modules.nix
Normal file
23
lib/modules.nix
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
{ self, lib, ... }:
|
||||
let
|
||||
inherit (builtins) readDir pathExists;
|
||||
inherit (lib) hasPrefix hasSuffix nameValuePair removeSuffix;
|
||||
inherit (self.attrs) mapFilterAttrs;
|
||||
in
|
||||
{
|
||||
mapModules = dir: fn:
|
||||
mapFilterAttrs
|
||||
(n: v:
|
||||
v != null &&
|
||||
!(hasPrefix "_" n))
|
||||
(n: v:
|
||||
let path = "${toString dir}/${n}"; in
|
||||
if v == "directory" && pathExists "${path}/default.nix"
|
||||
then nameValuePair n (fn path)
|
||||
else if v == "regular" &&
|
||||
n != "default.nix" &&
|
||||
hasSuffix ".nix" n
|
||||
then nameValuePair (removeSuffix ".nix" n) (fn path)
|
||||
else nameValuePair "" null)
|
||||
(readDir dir);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue