feat: move to umport for automatic module import

This commit is contained in:
2025-06-21 11:30:35 +08:00
parent 89aa6186f2
commit 13cc8234e1
22 changed files with 314 additions and 416 deletions

View File

@@ -9,5 +9,6 @@ in
stdlib.extend (
self: super: {
my = mkMyLib { lib = self; };
umport = import ./umport.nix { lib = self; };
}
)

48
lib/umport.nix Normal file
View File

@@ -0,0 +1,48 @@
# This function is modified from:
# https://github.com/yunfachi/nypkgs/blob/master/lib/umport.nix
#
# !!! REMOVING THIS NOTICE VIOLATES THE MIT LICENSE OF THE UMPORT PROJECT !!!
# This notice must be retained in all copies of this function, including modified versions!
# The MIT License can be found here:
# https://github.com/yunfachi/nypkgs/blob/master/LICENSE
{ lib, ... }:
let
umport =
{
path ? null,
paths ? [ ],
include ? [ ],
exclude ? [ ],
recursive ? true,
}:
with lib;
with fileset;
let
excludedFiles = filter (path: pathIsRegularFile path) exclude;
excludedDirs = filter (path: pathIsDirectory path) exclude;
isExcluded =
path:
if elem path excludedFiles then
true
else
(filter (excludedDir: lib.path.hasPrefix excludedDir path) excludedDirs) != [ ];
in
unique (
(filter
(file: pathIsRegularFile file && hasSuffix ".nix" (builtins.toString file) && !isExcluded file)
(
concatMap (
_path:
if recursive then
toList _path
else
mapAttrsToList (
name: type: _path + (if type == "directory" then "/${name}/default.nix" else "/${name}")
) (builtins.readDir _path)
) (unique (if path == null then paths else [ path ] ++ paths))
)
)
++ (if recursive then concatMap (path: toList path) (unique include) else unique include)
);
in
umport