From 487924a994e44cba94bad3b964886b87f7b7ddbe Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 8 Nov 2024 12:31:51 -0800 Subject: [PATCH] Update docs on source filtering --- docs/SUMMARY.md | 2 +- docs/source-filtering.md | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md index cb1cbf69..3ab047d7 100644 --- a/docs/SUMMARY.md +++ b/docs/SUMMARY.md @@ -21,7 +21,7 @@ * [Workspace with Trunk](./examples/trunk-workspace.md) * [End-to-End Testing](./examples/end-to-end-testing.md) * [Building with SQLx](./examples/sqlx.md) -* [Source filtering](./source-filtering.md) +* [Source filtering and filesets](./source-filtering.md) * [Local development](./local_development.md) * [Custom cargo commands](./custom_cargo_commands.md) * [Customizing builds](./customizing_builds.md) diff --git a/docs/source-filtering.md b/docs/source-filtering.md index 4c993671..ecc6f141 100644 --- a/docs/source-filtering.md +++ b/docs/source-filtering.md @@ -43,3 +43,30 @@ craneLib.buildPackage { }; } ``` + +## Fileset filtering + +A more composable alternative to source filtering is using [filesets]: + +```nix +let + unfilteredRoot = ./.; # The original, unfiltered source + src = lib.fileset.toSource { + root = unfilteredRoot; + fileset = lib.fileset.unions [ + # Default files from crane (Rust and cargo files) + (craneLib.fileset.commonCargoSources unfilteredRoot) + # Also keep any markdown files + (lib.fileset.fileFilter (file: file.hasExt == "md") unfilteredRoot) + # Example of a folder for images, icons, etc + (lib.fileset.maybeMissing ./assets) + ]; + }; +in +craneLib.buildPackage { + # other attributes omitted + inherit src; +} +``` + +[filesets]: https://nixos.org/manual/nixpkgs/unstable/#sec-functions-library-fileset