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: add mapDir and mapDirFiles #63871
Conversation
Funnily enough, a much older implementation of what is basically the same thing can be found at http://chriswarbo.net/projects/nixos/useful_hacks.html under "Importing Directories". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering that nixpkgs/lib has already become somewhat of a standard library for Nix, I wouldn't mind such a function, even if it isn't used in nixpkgs. Would like to hear more opinions on that. You do need to add docs to it though.
lib/filesystem.nix
Outdated
@@ -42,4 +44,13 @@ | |||
type = (builtins.readDir parent).${base} or null; | |||
in file == /. || type == "directory"; | |||
in go (if isDir then file else parent); | |||
|
|||
mapDir = filter: f: path: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be called something like filterMapDir
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I saw something rather involved for recursing and filtering a directory tree (probably doing something else too) somewhere, I should probably look at that, if anyone remembers what it was.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it was just locateDominatingFile
from this same file. I suppose it would make sense to factor out the mapDir
equivalent component. I didn't even notice the similarities when I was making the PR.
Edit: I would probably want to make a separate PR for that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I added some documentation. Is it good?
Yeah thanks for making that explicit. I was sort of thinking about it, but I wasn't sure how to write the documentation without ending up overexplaining and saying the same exact thing as what reading the code would tell you. |
I share the opinion of @infinisil regarding having these functions. |
# It doesn't recurse over subdirectories. | ||
filterMapDir = filter: f: path: | ||
#Sets are lexically sorted by key | ||
let files = builtins.readDir path; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why hard-code the readDir
?
If you change the signature to take the resulting data structure of readDir
instead of a Path
, the user can chain multiple invocations (and do other transformations), and the function becomes more composable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intention is to have a convenience function that lets you apply a function to a directory without having to do anything else, but sure, I don't see why I couldn't make another utility function that does what you said - and then use that to implement the convenience function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’d argue it’s still convenient, as long as the usage is in the example. We already have waaaay too many non-composing functions in lib
I didn't think about this much, mostly note to self: but maybe |
Thank you for your contributions.
|
I marked this as stale due to inactivity. → More info |
Closing as abandoned |
Adds a function and a convenience function to
lib/filesystems.nix
for mapping a function over a directory.