From 8c72702cc7ac4737b42c1ae4e31834d2a66e744d Mon Sep 17 00:00:00 2001 From: Andy Hamon Date: Tue, 23 Apr 2024 10:34:29 -0700 Subject: [PATCH] Make parent dir available in fileset.fileFilter Currently, fileFilter only allows filtering based based on a files base name + file type. This is a bit limiting if you want to include files based on the name of the directory they reside in. I bumped in to this when using fileFilter to make a minimal set of files to make Cargo happy in a Rust workspace - specifically, I needed to pull in any `.rs` file that lived under a `bin/`, `examples/`, or `benches/` folder before Cargo would stop complaining. (Note that I am trying to avoid pulling in all rust files - I'm working in a large Rust workspace but trying to package a single small member of the workspace). ``` --- lib/fileset/default.nix | 2 ++ lib/fileset/internal.nix | 8 ++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/fileset/default.nix b/lib/fileset/default.nix index e29f30251c690..9d631176198c0 100644 --- a/lib/fileset/default.nix +++ b/lib/fileset/default.nix @@ -668,6 +668,8 @@ in { - `name` (String): The name of the file + - `dir` (Path): The path of the directory containing the file. + - `type` (String, one of `"regular"`, `"symlink"` or `"unknown"`): The type of the file. This matches result of calling [`builtins.readFileType`](https://nixos.org/manual/nix/stable/language/builtins.html#builtins-readFileType) on the file's path. diff --git a/lib/fileset/internal.nix b/lib/fileset/internal.nix index 0d97ef1745683..50f4ccc869ffc 100644 --- a/lib/fileset/internal.nix +++ b/lib/fileset/internal.nix @@ -825,11 +825,11 @@ rec { _fileFilter = predicate: root: let # Check the predicate for a single file - # Type: String -> String -> filesetTree - fromFile = name: type: + # Type: Path -> String -> String -> filesetTree + fromFile = dir: name: type: if predicate { - inherit name type; + inherit dir name type; hasExt = ext: hasSuffix ".${ext}" name; # To ensure forwards compatibility with more arguments being added in the future, @@ -848,7 +848,7 @@ rec { if type == "directory" then fromDir (path + "/${name}") else - fromFile name type + fromFile path name type ) (readDir path); rootType = pathType root;