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-11-25 09:17:28 +00:00
|
|
|
mkFilter = {
|
|
|
|
dirRootsToInclude, # Relative paths of directories to include
|
2019-07-29 08:33:35 +00:00
|
|
|
dirsToExclude ? [ ], # Base names of directories to exclude
|
|
|
|
filesToInclude ? [ ], # Relative path of files to include
|
|
|
|
filesToExclude ? [ ], # Relative path of files to exclude
|
|
|
|
root }:
|
2019-06-04 16:50:29 +00:00
|
|
|
let
|
2019-11-25 09:17:28 +00:00
|
|
|
allPathRootsAllowed = (length dirRootsToInclude) == 0;
|
2019-12-13 11:54:28 +00:00
|
|
|
# this removes superfluous slashes from the path
|
|
|
|
cleanRoot = "${toString (/. + root)}/";
|
2019-11-25 09:17:28 +00:00
|
|
|
in
|
|
|
|
path: type:
|
|
|
|
let
|
|
|
|
baseName = baseNameOf (toString path);
|
2019-12-13 11:54:28 +00:00
|
|
|
subpath = elemAt (splitString cleanRoot path) 1;
|
2019-11-25 09:17:28 +00:00
|
|
|
spdir = elemAt (splitString "/" subpath) 0;
|
2019-06-04 16:50:29 +00:00
|
|
|
|
2019-11-25 09:17:28 +00:00
|
|
|
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
|
|
|
|
((allPathRootsAllowed || (any (dirRootToInclude: isPathAllowed dirRootToInclude subpath) dirRootsToInclude)) && ! (
|
|
|
|
# Filter out version control software files/directories
|
|
|
|
(type == "directory" && (elem baseName dirsToExclude))
|
|
|
|
)));
|
2019-06-04 16:50:29 +00:00
|
|
|
|
|
|
|
in mkFilter
|