From 01bdc186ae9baf635bdfb732e3d81ac41da02ca4 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Fri, 22 May 2026 14:14:40 +0200 Subject: [PATCH] lib.sources.sourceByGlobs: address review comments --- lib/sources.nix | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/sources.nix b/lib/sources.nix index 81c5305842a1..43a374c16e73 100644 --- a/lib/sources.nix +++ b/lib/sources.nix @@ -521,44 +521,50 @@ let throw "repoRevToName: invalid kind"; /** - Filter sources by a list of double star glob patterns. + Filter a source tree by a list of doublestar-style glob patterns, + returning a source that only contains paths matching at least one + pattern. `*` matches a single path component, and `**` matches any + number of components. # Inputs `src` - : 1\. Function argument + : The source tree to filter. `patterns` - : 2\. Function argument + : List of glob patterns to include, e.g. `[ "*.py" "src/**" ]`. + A leading `**` (e.g. `**\/*.py` for all `.py` files at any depth) + is also supported; the `\` here is just a Nix string escape used + to avoid closing this comment. # Examples :::{.example} ## `sourceByGlobs` usage example - - Include all .py files recursively + - Include everything under a subdirectory ```nix - src = sourceByGlobs ./my-subproject ["**\/*.py" ] + src = sourceByGlobs ./. [ "src/**" "tests/**" ] ``` - Include all .py files in root directory only ```nix - src = sourceByGlobs ./my-subproject ["*.py" ] + src = sourceByGlobs ./. [ "*.py" ] ``` ::: */ sourceByGlobs = let - splitPath = path: filter isString (split "\/" path); + splitPath = path: filter isString (split "/" path); # Make component regex mkRe = s: if s == "**" then ".*" # Has special handling below else - concatStrings (map (tok: if isList tok then "[^\/]*" else escapeRegex tok) (split "\\*+" s)); + concatStrings (map (tok: if isList tok then "[^/]*" else escapeRegex tok) (split "\\*+" s)); # Make a source filter function from pattern mkMatcher =