2019-06-04 16:50:29 +00:00
|
|
|
{ lib }:
|
|
|
|
|
|
|
|
# This Nix expression allows filtering a local directory by specifying dirRootsToInclude, dirsToExclude and filesToInclude.
|
|
|
|
# It also filters out symlinks to result folders created by nix-build, as well as backup/swap/generated files
|
|
|
|
|
|
|
|
let
|
2019-07-29 08:33:35 +00:00
|
|
|
inherit (lib)
|
|
|
|
any compare compareLists elem elemAt hasPrefix length min splitString take;
|
2019-06-04 16:50:29 +00:00
|
|
|
|
2019-07-29 08:33:35 +00:00
|
|
|
isPathAllowed = allowedPath: path:
|
2019-06-04 16:50:29 +00:00
|
|
|
let
|
|
|
|
count = min (length allowedPathElements) (length pathElements);
|
|
|
|
pathElements = splitString "/" path;
|
|
|
|
allowedPathElements = splitString "/" allowedPath;
|
|
|
|
pathElementsSubset = take count pathElements;
|
|
|
|
allowedPathElementsSubset = take count allowedPathElements;
|
|
|
|
in (compareLists compare allowedPathElementsSubset pathElementsSubset) == 0;
|
|
|
|
|
2019-07-29 08:33:35 +00:00
|
|
|
mkFilter = { dirRootsToInclude, # Relative paths of directories to include
|
|
|
|
dirsToExclude ? [ ], # Base names of directories to exclude
|
|
|
|
filesToInclude ? [ ], # Relative path of files to include
|
|
|
|
filesToExclude ? [ ], # Relative path of files to exclude
|
|
|
|
root }:
|
|
|
|
path: type:
|
2019-06-04 16:50:29 +00:00
|
|
|
let
|
|
|
|
baseName = baseNameOf (toString path);
|
|
|
|
subpath = elemAt (splitString "${toString root}/" path) 1;
|
|
|
|
spdir = elemAt (splitString "/" subpath) 0;
|
|
|
|
|
|
|
|
in lib.cleanSourceFilter path type && (
|
|
|
|
(type != "directory" && (elem spdir filesToInclude) && !(elem spdir filesToExclude)) ||
|
|
|
|
# check if any part of the directory path is described in dirRootsToInclude
|
|
|
|
((any (dirRootToInclude: isPathAllowed dirRootToInclude subpath) dirRootsToInclude) && ! (
|
|
|
|
# Filter out version control software files/directories
|
|
|
|
(type == "directory" && (elem baseName dirsToExclude))
|
|
|
|
)));
|
|
|
|
|
|
|
|
in mkFilter
|