From 1fa04c19c88349572749a996d0149b7852abcf89 Mon Sep 17 00:00:00 2001 From: Rijk van Putten Date: Sat, 2 Nov 2024 09:24:44 +0100 Subject: [PATCH 01/13] Add `cargoFileset` --- checks/default.nix | 36 ++++++++++++++++++++++++++++++++++++ lib/cargoFileset.nix | 10 ++++++++++ lib/default.nix | 1 + 3 files changed, 47 insertions(+) create mode 100644 lib/cargoFileset.nix diff --git a/checks/default.nix b/checks/default.nix index 39e1d4b5..ee25c6ca 100644 --- a/checks/default.nix +++ b/checks/default.nix @@ -346,6 +346,42 @@ in features = callPackage ./features { }; + fileset = myLib.buildPackage { + src = lib.fileset.toSource { + root = ./simple; + fileset = myLib.cargoFileset ./simple; + }; + }; + + filesetBuildScript = myLib.buildPackage { + src = lib.fileset.toSource { + root = ./with-build-script; + fileset = myLib.cargoFileset ./with-build-script; + }; + }; + + filesetWorkspace = myLib.buildPackage { + src = lib.fileset.toSource { + root = ./workspace; + fileset = myLib.cargoFileset ./workspace; + }; + }; + + filesetWorkspaceInheritance = myLib.buildPackage { + src = lib.fileset.toSource { + root = ./workspace-inheritance; + fileset = myLib.cargoFileset ./workspace-inheritance; + }; + }; + + filesetWorkspaceRoot = myLib.buildPackage { + src = lib.fileset.toSource { + root = ./workspace-root; + fileset = myLib.cargoFileset ./workspace-root; + }; + pname = "workspace-root"; + }; + gitOverlappingRepo = myLib.buildPackage { src = ./git-overlapping; }; diff --git a/lib/cargoFileset.nix b/lib/cargoFileset.nix new file mode 100644 index 00000000..84eff880 --- /dev/null +++ b/lib/cargoFileset.nix @@ -0,0 +1,10 @@ +{ lib }: +# A fileset that includes the minimum files needed to build a Rust project with Cargo +with lib.fileset; +path: unions [ + # Cargo files + (fileFilter (file: file.name == "Cargo.toml" || file.name == "Cargo.lock") path) + (maybeMissing (path + ./.cargo/config.toml)) + # any Rust source files + (fileFilter (file: file.hasExt "rs") path) +] diff --git a/lib/default.nix b/lib/default.nix index 114dfe40..e5f2101f 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -60,6 +60,7 @@ let cargoDeny = callPackage ./cargoDeny.nix { }; cargoDoc = callPackage ./cargoDoc.nix { }; cargoDocTest = callPackage ./cargoDocTest.nix { }; + cargoFileset = callPackage ./cargoFileset.nix { }; cargoFmt = callPackage ./cargoFmt.nix { }; cargoHelperFunctionsHook = callPackage ./setupHooks/cargoHelperFunctions.nix { }; cargoLlvmCov = callPackage ./cargoLlvmCov.nix { }; From 93073226ace369c8c4d7717baf73f8d6b2e59f6f Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 8 Nov 2024 10:46:55 -0800 Subject: [PATCH 02/13] Avoid `with` expression --- lib/cargoFileset.nix | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/cargoFileset.nix b/lib/cargoFileset.nix index 84eff880..1f72efaa 100644 --- a/lib/cargoFileset.nix +++ b/lib/cargoFileset.nix @@ -1,6 +1,11 @@ { lib }: +let + inherit (lib.fileset) + fileFilter + maybeMissing + unions; +in # A fileset that includes the minimum files needed to build a Rust project with Cargo -with lib.fileset; path: unions [ # Cargo files (fileFilter (file: file.name == "Cargo.toml" || file.name == "Cargo.lock") path) From cfd21997f8500e80ab0d7e37b056f5eb1fd5917c Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 8 Nov 2024 10:51:24 -0800 Subject: [PATCH 03/13] Keep all .toml files They're commonly used to configure other cargo-based tools, plus this mirrors what filterCargoSources does. The caller can easily filter out any unwanted .toml files with an intersection/disjoint set --- lib/cargoFileset.nix | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/cargoFileset.nix b/lib/cargoFileset.nix index 1f72efaa..1f81a61b 100644 --- a/lib/cargoFileset.nix +++ b/lib/cargoFileset.nix @@ -2,14 +2,13 @@ let inherit (lib.fileset) fileFilter - maybeMissing unions; in # A fileset that includes the minimum files needed to build a Rust project with Cargo path: unions [ - # Cargo files - (fileFilter (file: file.name == "Cargo.toml" || file.name == "Cargo.lock") path) - (maybeMissing (path + ./.cargo/config.toml)) - # any Rust source files - (fileFilter (file: file.hasExt "rs") path) + # Cargo files (Cargo.toml handled below) + (fileFilter (file: file.name == "Cargo.lock") path) + # Keep all toml files as they are commonly used to configure other + # cargo-based tools + (fileFilter (file: lib.any file.hasExt [ "rs" "toml" ]) path) ] From a1802408be23735aab0612c20357247ba8d3d0cd Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 8 Nov 2024 11:00:17 -0800 Subject: [PATCH 04/13] Scope filesets --- checks/default.nix | 10 +++++----- lib/default.nix | 6 +++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/checks/default.nix b/checks/default.nix index ee25c6ca..cc53a1d0 100644 --- a/checks/default.nix +++ b/checks/default.nix @@ -349,35 +349,35 @@ in fileset = myLib.buildPackage { src = lib.fileset.toSource { root = ./simple; - fileset = myLib.cargoFileset ./simple; + fileset = myLib.fileset.cargoFileset ./simple; }; }; filesetBuildScript = myLib.buildPackage { src = lib.fileset.toSource { root = ./with-build-script; - fileset = myLib.cargoFileset ./with-build-script; + fileset = myLib.fileset.cargoFileset ./with-build-script; }; }; filesetWorkspace = myLib.buildPackage { src = lib.fileset.toSource { root = ./workspace; - fileset = myLib.cargoFileset ./workspace; + fileset = myLib.fileset.cargoFileset ./workspace; }; }; filesetWorkspaceInheritance = myLib.buildPackage { src = lib.fileset.toSource { root = ./workspace-inheritance; - fileset = myLib.cargoFileset ./workspace-inheritance; + fileset = myLib.fileset.cargoFileset ./workspace-inheritance; }; }; filesetWorkspaceRoot = myLib.buildPackage { src = lib.fileset.toSource { root = ./workspace-root; - fileset = myLib.cargoFileset ./workspace-root; + fileset = myLib.fileset.cargoFileset ./workspace-root; }; pname = "workspace-root"; }; diff --git a/lib/default.nix b/lib/default.nix index e5f2101f..080bbf8b 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -60,7 +60,6 @@ let cargoDeny = callPackage ./cargoDeny.nix { }; cargoDoc = callPackage ./cargoDoc.nix { }; cargoDocTest = callPackage ./cargoDocTest.nix { }; - cargoFileset = callPackage ./cargoFileset.nix { }; cargoFmt = callPackage ./cargoFmt.nix { }; cargoHelperFunctionsHook = callPackage ./setupHooks/cargoHelperFunctions.nix { }; cargoLlvmCov = callPackage ./cargoLlvmCov.nix { }; @@ -88,6 +87,11 @@ let downloadCargoPackage = callPackage ./downloadCargoPackage.nix { }; downloadCargoPackageFromGit = callPackage ./downloadCargoPackageFromGit.nix { }; filterCargoSources = callPackage ./filterCargoSources.nix { }; + + fileset = { + cargoFileset = callPackage ./cargoFileset.nix { }; + }; + findCargoFiles = callPackage ./findCargoFiles.nix { }; inheritCargoArtifactsHook = callPackage ./setupHooks/inheritCargoArtifacts.nix { }; installCargoArtifactsHook = callPackage ./setupHooks/installCargoArtifacts.nix { }; From 56c38c401a8a66eb4d6590335fcecc738b30bdff Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 8 Nov 2024 11:19:39 -0800 Subject: [PATCH 05/13] Add more fileset definitions to aid with composition --- lib/cargoFileset.nix | 20 +++++++++----------- lib/default.nix | 4 ++++ lib/fileset/cargoTomlAndLock.nix | 7 +++++++ lib/fileset/configToml.nix | 9 +++++++++ lib/fileset/rust.nix | 7 +++++++ lib/fileset/toml.nix | 7 +++++++ 6 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 lib/fileset/cargoTomlAndLock.nix create mode 100644 lib/fileset/configToml.nix create mode 100644 lib/fileset/rust.nix create mode 100644 lib/fileset/toml.nix diff --git a/lib/cargoFileset.nix b/lib/cargoFileset.nix index 1f81a61b..481f49cc 100644 --- a/lib/cargoFileset.nix +++ b/lib/cargoFileset.nix @@ -1,14 +1,12 @@ -{ lib }: -let - inherit (lib.fileset) - fileFilter - unions; -in -# A fileset that includes the minimum files needed to build a Rust project with Cargo -path: unions [ - # Cargo files (Cargo.toml handled below) - (fileFilter (file: file.name == "Cargo.lock") path) +{ fileset +, lib +}: + +path: +lib.fileset.unions [ + (fileset.cargoTomlAndLock path) + (fileset.rust path) # Keep all toml files as they are commonly used to configure other # cargo-based tools - (fileFilter (file: lib.any file.hasExt [ "rs" "toml" ]) path) + (fileset.toml path) ] diff --git a/lib/default.nix b/lib/default.nix index 080bbf8b..500ad004 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -90,6 +90,10 @@ let fileset = { cargoFileset = callPackage ./cargoFileset.nix { }; + cargoTomlAndLock = callPackage ./fileset/cargoTomlAndLock.nix { }; + configToml = callPackage ./fileset/configToml.nix { }; + rust = callPackage ./fileset/rust.nix { }; + toml = callPackage ./fileset/toml.nix { }; }; findCargoFiles = callPackage ./findCargoFiles.nix { }; diff --git a/lib/fileset/cargoTomlAndLock.nix b/lib/fileset/cargoTomlAndLock.nix new file mode 100644 index 00000000..abc810e8 --- /dev/null +++ b/lib/fileset/cargoTomlAndLock.nix @@ -0,0 +1,7 @@ +{ lib +}: + +path: +lib.fileset.fileFilter + (file: lib.elem file.name [ "Cargo.toml" "Cargo.lock" ]) + path diff --git a/lib/fileset/configToml.nix b/lib/fileset/configToml.nix new file mode 100644 index 00000000..0bae90ef --- /dev/null +++ b/lib/fileset/configToml.nix @@ -0,0 +1,9 @@ +{ lib +}: + +path: +lib.fileset.fileFilter + # Technically this should be scoped to `.cargo/config.toml` but (currently) + # there is no way to do this with file sets in a generic manner + (file: file.name == "config.toml") + path diff --git a/lib/fileset/rust.nix b/lib/fileset/rust.nix new file mode 100644 index 00000000..a8c99069 --- /dev/null +++ b/lib/fileset/rust.nix @@ -0,0 +1,7 @@ +{ lib +}: + +path: +lib.fileset.fileFilter + (file: file.hasExt "rs") + path diff --git a/lib/fileset/toml.nix b/lib/fileset/toml.nix new file mode 100644 index 00000000..f244831d --- /dev/null +++ b/lib/fileset/toml.nix @@ -0,0 +1,7 @@ +{ lib +}: + +path: +lib.fileset.fileFilter + (file: file.hasExt "toml") + path From ef9757c5d5c1225401d75f4c69d8ff13010fcb34 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 8 Nov 2024 11:22:40 -0800 Subject: [PATCH 06/13] Rename cargoFileset --- checks/default.nix | 10 +++++----- lib/default.nix | 2 +- .../commonCargoSources.nix} | 0 3 files changed, 6 insertions(+), 6 deletions(-) rename lib/{cargoFileset.nix => fileset/commonCargoSources.nix} (100%) diff --git a/checks/default.nix b/checks/default.nix index cc53a1d0..6d4e2fa9 100644 --- a/checks/default.nix +++ b/checks/default.nix @@ -349,35 +349,35 @@ in fileset = myLib.buildPackage { src = lib.fileset.toSource { root = ./simple; - fileset = myLib.fileset.cargoFileset ./simple; + fileset = myLib.fileset.commonCargoSources ./simple; }; }; filesetBuildScript = myLib.buildPackage { src = lib.fileset.toSource { root = ./with-build-script; - fileset = myLib.fileset.cargoFileset ./with-build-script; + fileset = myLib.fileset.commonCargoSources ./with-build-script; }; }; filesetWorkspace = myLib.buildPackage { src = lib.fileset.toSource { root = ./workspace; - fileset = myLib.fileset.cargoFileset ./workspace; + fileset = myLib.fileset.commonCargoSources ./workspace; }; }; filesetWorkspaceInheritance = myLib.buildPackage { src = lib.fileset.toSource { root = ./workspace-inheritance; - fileset = myLib.fileset.cargoFileset ./workspace-inheritance; + fileset = myLib.fileset.commonCargoSources ./workspace-inheritance; }; }; filesetWorkspaceRoot = myLib.buildPackage { src = lib.fileset.toSource { root = ./workspace-root; - fileset = myLib.fileset.cargoFileset ./workspace-root; + fileset = myLib.fileset.commonCargoSources ./workspace-root; }; pname = "workspace-root"; }; diff --git a/lib/default.nix b/lib/default.nix index 500ad004..9b5deb1b 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -89,8 +89,8 @@ let filterCargoSources = callPackage ./filterCargoSources.nix { }; fileset = { - cargoFileset = callPackage ./cargoFileset.nix { }; cargoTomlAndLock = callPackage ./fileset/cargoTomlAndLock.nix { }; + commonCargoSources = callPackage ./fileset/commonCargoSources.nix { }; configToml = callPackage ./fileset/configToml.nix { }; rust = callPackage ./fileset/rust.nix { }; toml = callPackage ./fileset/toml.nix { }; diff --git a/lib/cargoFileset.nix b/lib/fileset/commonCargoSources.nix similarity index 100% rename from lib/cargoFileset.nix rename to lib/fileset/commonCargoSources.nix From 73ca2b82b04ded7903ec809f1d409be6e629a092 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 8 Nov 2024 11:36:44 -0800 Subject: [PATCH 07/13] Update workspace example --- examples/quick-start-workspace/flake.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/quick-start-workspace/flake.nix b/examples/quick-start-workspace/flake.nix index 1989ebb5..894068ef 100644 --- a/examples/quick-start-workspace/flake.nix +++ b/examples/quick-start-workspace/flake.nix @@ -71,9 +71,9 @@ fileset = lib.fileset.unions [ ./Cargo.toml ./Cargo.lock - ./crates/my-common - ./crates/my-workspace-hack - crate + (craneLib.fileset.commonCargoSources ./crates/my-common) + (craneLib.fileset.commonCargoSources ./crates/my-workspace-hack) + (craneLib.fileset.commonCargoSources crate) ]; }; From 1f1870c628f0882a2838c97ab10afae9d2d81535 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 8 Nov 2024 12:03:08 -0800 Subject: [PATCH 08/13] Update trunk examples --- examples/trunk-workspace/flake.nix | 22 ++++++++++++---------- examples/trunk/flake.nix | 21 ++++++++++++--------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/examples/trunk-workspace/flake.nix b/examples/trunk-workspace/flake.nix index d28f2aa8..cea69391 100644 --- a/examples/trunk-workspace/flake.nix +++ b/examples/trunk-workspace/flake.nix @@ -40,19 +40,21 @@ }); # When filtering sources, we want to allow assets other than .rs files - src = lib.cleanSourceWith { - src = ./.; # The original, unfiltered source - filter = path: type: - (lib.hasSuffix "\.html" path) || - (lib.hasSuffix "\.scss" path) || + 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) + (lib.fileset.fileFilter + (file: lib.any file.hasExt [ "html" "scss" ]) + unfilteredRoot + ) # Example of a folder for images, icons, etc - (lib.hasInfix "/assets/" path) || - # Default filter from crane (allow .rs files) - (craneLib.filterCargoSources path type) - ; + (lib.fileset.maybeMissing ./assets) + ]; }; - # Arguments to be used by both the client and the server # When building a workspace with crane, it's a good idea # to set "pname" and "version". diff --git a/examples/trunk/flake.nix b/examples/trunk/flake.nix index cdf0eda8..2b2de6e2 100644 --- a/examples/trunk/flake.nix +++ b/examples/trunk/flake.nix @@ -40,16 +40,19 @@ }); # When filtering sources, we want to allow assets other than .rs files - src = lib.cleanSourceWith { - src = ./.; # The original, unfiltered source - filter = path: type: - (lib.hasSuffix "\.html" path) || - (lib.hasSuffix "\.scss" path) || + 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) + (lib.fileset.fileFilter + (file: lib.any file.hasExt [ "html" "scss" ]) + unfilteredRoot + ) # Example of a folder for images, icons, etc - (lib.hasInfix "/assets/" path) || - # Default filter from crane (allow .rs files) - (craneLib.filterCargoSources path type) - ; + (lib.fileset.maybeMissing ./assets) + ]; }; # Common arguments can be set here to avoid repeating them later From 68d63a371ce5a26823274895aac7ba51f0e0057d Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 8 Nov 2024 12:07:09 -0800 Subject: [PATCH 09/13] Update sqlx example --- examples/sqlx/flake.nix | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/sqlx/flake.nix b/examples/sqlx/flake.nix index ea9eccc5..f4b999a1 100644 --- a/examples/sqlx/flake.nix +++ b/examples/sqlx/flake.nix @@ -18,13 +18,15 @@ craneLib = crane.mkLib pkgs; - sqlFilter = path: _type: null != builtins.match ".*sql$" path; - sqlOrCargo = path: type: (sqlFilter path type) || (craneLib.filterCargoSources path type); - - src = lib.cleanSourceWith { - src = ./.; # The original, unfiltered source - filter = sqlOrCargo; - name = "source"; # Be reproducible, regardless of the directory name + 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) + # Include all the .sql migrations as well + ./migrations + ]; }; # Common arguments can be set here to avoid repeating them later From 5ba2e3643a4708aed902c2561461ab7f21236c33 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 8 Nov 2024 12:09:10 -0800 Subject: [PATCH 10/13] Simplify docs fileset --- pkgs/default.nix | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/pkgs/default.nix b/pkgs/default.nix index ea68d9b8..68b71307 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -4,20 +4,14 @@ book = let inherit (pkgs) lib; - root = myLib.path ./..; - rootPrefix = toString root; - cleanedSrc = lib.cleanSourceWith { - src = root; - filter = path: _: - let - relativePath = lib.removePrefix rootPrefix path; - in - lib.any (prefix: lib.hasPrefix prefix relativePath) [ - "/docs" # Build the docs directory - "/examples" # But also include examples as we cross-reference them - "/README.md" - "/CHANGELOG.md" - ]; + cleanedSrc = lib.fileset.toSource { + root = ./..; + fileset = lib.fileset.unions [ + ./../docs + ./../examples + ./../README.md + ./../CHANGELOG.md + ]; }; in pkgs.runCommand "crane-book" { } '' From fa99f9f9441c126e3b5742453d9d535f9fa23710 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 8 Nov 2024 12:17:26 -0800 Subject: [PATCH 11/13] Update API docs --- docs/API.md | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/docs/API.md b/docs/API.md index 6447ef51..761a2d67 100644 --- a/docs/API.md +++ b/docs/API.md @@ -1076,7 +1076,6 @@ cleanSourceWith { name = "source"; # Be reproducible, regardless of the directory name } ``` - Note that it is possible to compose source filters, especially if `filterCargoSources` omits files which are relevant to the build. For example: @@ -1094,6 +1093,47 @@ cleanSourceWith { } ``` +### `craneLib.fileset.cargoTomlAndLock` + +`cargoTomlAndLock :: path -> fileset` + +A [fileset] helper which will only include any `Cargo.toml` and `Cargo.lock` +files from the specified path. + +### `craneLib.fileset.commonCargoSources` + +`commonCargoSources :: path -> fileset` + +A [fileset] helper which will only include any files commonly used by cargo +projects from the specified path. Essentially a union of: + +* `craneLib.fileset.cargoTomlAndLock` +* `craneLib.fileset.rust` +* `craneLib.fileset.toml` + +### `craneLib.fileset.configToml` + +`configToml :: path -> fileset` + +A [fileset] helper which will only include `config.toml` files from the +specified path. + +Note that cargo usually only pays attention to `config.toml` files if they are +present inside of a directory named `.cargo`. This fileset will contain any +`config.toml` file, even if its parent directory is _not_ named `.cargo`. + +### `craneLib.fileset.rust` + +`rust :: path -> fileset` + +A [fileset] helper which will only include `*.rs` files from the specified path. + +### `craneLib.fileset.toml` + +`toml :: path -> fileset` + +A [fileset] helper which will only include `*.toml` files from the specified path. + ### `craneLib.mkCargoDerivation` `mkCargoDerivation :: set -> drv` @@ -1866,3 +1906,5 @@ Defines `replaceCargoLock()` which handles replacing or inserting a specified **Automatic behavior:** if `cargoLock` is set and `doNotReplaceCargoLock` is not set, then `replaceCargoLock "$cargoLock"` will be run as a pre patch hook. + +[fileset]: https://nixos.org/manual/nixpkgs/unstable/#sec-functions-library-fileset From 630b41f5157c3d50cec24069e5f33bb430604587 Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 8 Nov 2024 12:21:53 -0800 Subject: [PATCH 12/13] Update CHANGELOG --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff1eec24..e728802d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## Unreleased +### Added +* Added a number of fileset helpers to more easily compose source filtering: + * `fileset.cargoTomlAndLock`: for `Cargo.toml` and `Cargo.lock` files + * `fileset.commonCargoSources`: for files commonly used by cargo projects + * `fileset.configToml`: for `config.toml` files + * `fileset.rust`: for `*.rs` files + * `fileset.toml`: for `*.toml` files + ### Fixed * `buildTrunkPackage` will pass in `--release=true` (instead of just `--release`) for trunk versions 0.21 or higher to avoid argument ambiguities From 487924a994e44cba94bad3b964886b87f7b7ddbe Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Fri, 8 Nov 2024 12:31:51 -0800 Subject: [PATCH 13/13] 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