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: add mapDir and mapDirFiles #63871

Closed
wants to merge 1 commit into from

Conversation

deliciouslytyped
Copy link
Contributor

Adds a function and a convenience function to lib/filesystems.nix for mapping a function over a directory.

@deliciouslytyped
Copy link
Contributor Author

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".
Coincidence?
I think not.

@Mic92 Mic92 requested a review from infinisil June 28, 2019 06:03
Copy link
Member

@infinisil infinisil left a 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.

@@ -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:
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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?

@deliciouslytyped
Copy link
Contributor Author

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.

@Mic92
Copy link
Member

Mic92 commented Jun 28, 2019

I share the opinion of @infinisil regarding having these functions.

lib/filesystem.nix Outdated Show resolved Hide resolved
lib/filesystem.nix Outdated Show resolved Hide resolved
lib/filesystem.nix Outdated Show resolved Hide resolved
# It doesn't recurse over subdirectories.
filterMapDir = filter: f: path:
#Sets are lexically sorted by key
let files = builtins.readDir path;
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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

@deliciouslytyped
Copy link
Contributor Author

deliciouslytyped commented Aug 7, 2019

I didn't think about this much, mostly note to self: but maybe filterMapDir should be changed to (Path -> String -> (Bool, a)) -> Path -> { Path = a }

@stale
Copy link

stale bot commented Jun 1, 2020

Thank you for your contributions.
This has been automatically marked as stale because it has had no activity for 180 days.
If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.
Here are suggestions that might help resolve this more quickly:

  1. Search for maintainers and people that previously touched the
    related code and @ mention them in a comment.
  2. Ask on the NixOS Discourse. 3. Ask on the #nixos channel on
    irc.freenode.net.

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 1, 2020
@stale stale bot removed the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Oct 3, 2020
@stale
Copy link

stale bot commented Jun 6, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Jun 6, 2021
@Profpatsch
Copy link
Member

Closing as abandoned

@Profpatsch Profpatsch closed this Jun 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.status: merge conflict 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 10.rebuild-darwin: 0 10.rebuild-linux: 0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants