Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib.sourceSubdirs: init #234297

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ let
hiPrioSet getLicenseFromSpdxId getExe;
inherit (self.filesystem) pathType pathIsDirectory pathIsRegularFile;
inherit (self.sources) cleanSourceFilter
cleanSource sourceByRegex sourceFilesBySuffices
cleanSource sourceByRegex sourceFilesBySuffices sourceSubdirs
commitIdFromGitRepo cleanSourceWith pathHasContext
canCleanSource pathIsGitRepo;
inherit (self.modules) evalModules setDefaultModuleLocation
Expand Down
18 changes: 18 additions & 0 deletions lib/path/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ in /* No rec! Add dependencies on this file at the top. */ {
${subpathInvalidReason subpath}'';
path + ("/" + subpath);

/*
Test whether one path is a child of another.
This is just based on the string values of the provided paths.
It does not access the filesystem.

Type: sourceLike -> Path|String -> Path|String -> bool

Examples:
path.hasPrefix "foo" "foo/bar" # => true
path.hasPrefix "foo" "foo" # => true
path.hasPrefix "food" "foo" => false
path.hasPrefix "foo/bar" "foo" # => false
*/
hasPrefix = parent: child:
assert lib.isPath parent;
assert lib.isPath child;
lib.hasPrefix "${toString parent}/" "${toString child}/";

/* Whether a value is a valid subpath string.

- The value is a string
Expand Down
22 changes: 22 additions & 0 deletions lib/sources.nix
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,27 @@ let
in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts;
in cleanSourceWith { inherit filter src; };

/*
Filter a source directory down to only a specified list of
subdirectories.

Type: sourceLike -> [String] -> Source

Example:
sourceSubdirs src [ "host/rootfs" "scripts" ]
*/
sourceSubdirs = src: subdirs: lib.cleanSourceWith {
filter = path: _type:
lib.any (subdir:
let
subdir' = lib.path.append (src.origSrc or src) subdir;
path' = /. + path;
in
lib.path.hasPrefix path' subdir' || lib.path.hasPrefix subdir' path'
) subdirs;
inherit src;
};

pathIsGitRepo = path: (_commitIdFromGitRepoOrError path)?value;

/*
Expand Down Expand Up @@ -286,6 +307,7 @@ in {

sourceByRegex
sourceFilesBySuffices
sourceSubdirs

trace
;
Expand Down