From 9e25340e88cb73b66c7637d697d6f80e262ccf40 Mon Sep 17 00:00:00 2001 From: geekiot Date: Fri, 26 Sep 2025 21:55:37 +0500 Subject: [PATCH] add (lib): add function to scan all files from dir --- lib/utils.nix | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 lib/utils.nix diff --git a/lib/utils.nix b/lib/utils.nix new file mode 100644 index 0000000..02d0723 --- /dev/null +++ b/lib/utils.nix @@ -0,0 +1,51 @@ +# This function is copied 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 = + inputs@{ + 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 +{ + scan = umport; +}