fileFilter
lib.fileset.fileFilter
Filter a file set to only contain files matching some predicate.
Inputs
predicate
-
The predicate function to call on all files contained in given file set. A file is included in the resulting file set if this function returns true for it.
This function is called with an attribute set containing these attributes:
-
name
(String): The name of the file -
type
(String, one of"regular"
,"symlink"
or"unknown"
): The type of the file. This matches result of callingbuiltins.readFileType
on the file's path. -
hasExt
(String -> Bool): Whether the file has a certain file extension.hasExt ext
is true only ifhasSuffix ".${ext}" name
.
This also means that e.g. for a file with name
.gitignore
,hasExt "gitignore"
is true.Other attributes may be added in the future.
-
path
-
The path whose files to filter
Type
fileFilter ::
({
name :: String,
type :: String,
hasExt :: String -> Bool,
...
} -> Bool)
-> Path
-> FileSet
Examples
lib.fileset.fileFilter
usage example
# Include all regular `default.nix` files in the current directory
fileFilter (file: file.name == "default.nix") ./.
# Include all non-Nix files from the current directory
fileFilter (file: ! file.hasExt "nix") ./.
# Include all files that start with a "." in the current directory
fileFilter (file: hasPrefix "." file.name) ./.
# Include all regular files (not symlinks or others) in the current directory
fileFilter (file: file.type == "regular") ./.
Noogle detected
Implementation
The following is the current implementation of this function.
fileFilter =
predicate: path:
if !isFunction predicate then
throw ''lib.fileset.fileFilter: First argument is of type ${typeOf predicate}, but it should be a function instead.''
else if !isPath path then
if path._type or "" == "fileset" then
throw ''
lib.fileset.fileFilter: Second argument is a file set, but it should be a path instead.
If you need to filter files in a file set, use `intersection fileset (fileFilter pred ./.)` instead.''
else
throw ''lib.fileset.fileFilter: Second argument is of type ${typeOf path}, but it should be a path instead.''
else if !pathExists path then
throw ''lib.fileset.fileFilter: Second argument (${toString path}) is a path that does not exist.''
else
_fileFilter predicate path;