# Is lib.cleanSourceWith the best way to get all markdown files in a directory?

**URL:** https://discourse.nixos.org/t/is-lib-cleansourcewith-the-best-way-to-get-all-markdown-files-in-a-directory/20529
**Category:** Help
**Created:** [July 22, 2022, 7:00pm UTC](https://discourse.nixos.org/t/is-lib-cleansourcewith-the-best-way-to-get-all-markdown-files-in-a-directory/20529 "2022-07-22T19:00:26Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![wpcarro](https://discourse.nixos.org/user_avatar/discourse.nixos.org/wpcarro/32/6630_2.png) [@wpcarro](https://discourse.nixos.org/u/wpcarro)
#### Post date: [July 22, 2022, 7:00pm UTC](https://discourse.nixos.org/t/is-lib-cleansourcewith-the-best-way-to-get-all-markdown-files-in-a-directory/20529/1 "2022-07-22T19:00:26Z")

</div>

…or is there some more idiomatic way to do this?

---

<div class="post-metadata">

### Author: ![wpcarro](https://discourse.nixos.org/user_avatar/discourse.nixos.org/wpcarro/32/6630_2.png) [@wpcarro](https://discourse.nixos.org/u/wpcarro)
#### Post date: [July 23, 2022, 8:23pm UTC](https://discourse.nixos.org/t/is-lib-cleansourcewith-the-best-way-to-get-all-markdown-files-in-a-directory/20529/2 "2022-07-23T20:23:07Z")

</div>

I’ve tried `pkgs.lib.sourceByRegex /path ["^.*\.md$"]`, but it only seems to copy the top-level Markdown files in `path` to the `/nix/store` instead of _all_ Markdown files in subdirectories.

Is this something I’ll have to write from scratch using `readDir` and company?

---

<div class="post-metadata">

### Author: ![gravndal](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/g/45deac/32.png) [@gravndal](https://discourse.nixos.org/u/gravndal)
#### Post date: [July 24, 2022, 4:13am UTC](https://discourse.nixos.org/t/is-lib-cleansourcewith-the-best-way-to-get-all-markdown-files-in-a-directory/20529/3 "2022-07-24T04:13:53Z")

</div>

You might want to have a look at [GitHub - numtide/nix-filter: a small self-container source filtering lib](https://github.com/numtide/nix-filter).

---

<div class="post-metadata">

### Author: ![wpcarro](https://discourse.nixos.org/user_avatar/discourse.nixos.org/wpcarro/32/6630_2.png) [@wpcarro](https://discourse.nixos.org/u/wpcarro)
#### Post date: [July 25, 2022, 6:05pm UTC](https://discourse.nixos.org/t/is-lib-cleansourcewith-the-best-way-to-get-all-markdown-files-in-a-directory/20529/4 "2022-07-25T18:05:58Z")

</div>

> You might want to have a look at [GitHub - numtide/nix-filter: a small self-contained source filtering lib](https://github.com/numtide/nix-filter)

Thanks for the recommendation. AFAICT it looks like this behaves similarly to `lib.sourceByRegex` where it only includes the top-level Markdown files.

I left a comment on an open issue with `nix-filter`. Let’s see how that pans-out: [Improve ergonomics for glob matching · Issue #9 · numtide/nix-filter · GitHub](https://github.com/numtide/nix-filter/issues/9#issuecomment-1194179705)

---

<div class="post-metadata">

### Author: ![hmenke](https://discourse.nixos.org/user_avatar/discourse.nixos.org/hmenke/32/2369_2.png) [@hmenke](https://discourse.nixos.org/u/hmenke)
#### Post date: [July 26, 2022, 11:48am UTC](https://discourse.nixos.org/t/is-lib-cleansourcewith-the-best-way-to-get-all-markdown-files-in-a-directory/20529/5 "2022-07-26T11:48:27Z")

</div>

Are you looking for [`lib.sourceFilesBySuffices`](https://nixos.org/manual/nixpkgs/stable/#function-library-lib.sources.sourceFilesBySuffices)?

---

<div class="post-metadata">

### Author: ![wpcarro](https://discourse.nixos.org/user_avatar/discourse.nixos.org/wpcarro/32/6630_2.png) [@wpcarro](https://discourse.nixos.org/u/wpcarro)
#### Post date: [July 27, 2022, 3:00pm UTC](https://discourse.nixos.org/t/is-lib-cleansourcewith-the-best-way-to-get-all-markdown-files-in-a-directory/20529/6 "2022-07-27T15:00:53Z")

</div>

> Are you looking for [`lib.sourceFilesBySuffices`](https://nixos.org/manual/nixpkgs/stable/#function-library-lib.sources.sourceFilesBySuffices)?

That’s recursive, but it includes a bunch of garbage empty directories in the output too.

---

<div class="post-metadata">

### Author: ![hmenke](https://discourse.nixos.org/user_avatar/discourse.nixos.org/hmenke/32/2369_2.png) [@hmenke](https://discourse.nixos.org/u/hmenke)
#### Post date: [July 27, 2022, 4:59pm UTC](https://discourse.nixos.org/t/is-lib-cleansourcewith-the-best-way-to-get-all-markdown-files-in-a-directory/20529/7 "2022-07-27T16:59:46Z")

</div>

I reported the bug: [lib.sourceFilesBySuffices creates empty directories · Issue #183110 · NixOS/nixpkgs · GitHub](https://github.com/NixOS/nixpkgs/issues/183110)
