Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ onlyDrvs (
workspaceCross = callPackage ./workspace-cross-compile { inherit full; };
customStdenv = callPackage ./custom-stdenv { };
nextest = callPackage ./nextest { };
sourceTests = callPackage ./source-tests.nix { };
mergeArgsTests = callPackage ./mergeArgs-tests.nix { };
craneMultiBuildTests = callPackage ./crane-multi-build-tests.nix { };
cargoCrapModule = callPackage ./cargo-crap-module.nix { inherit mkLib; };
Expand Down
7 changes: 2 additions & 5 deletions checks/nextest/default.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
{ pkgs, flakeboxLib }:
let
rootDir = builtins.path {
name = "nextest";
path = ./.;
};
rootDir = ./.;

buildPaths = [
"Cargo.toml"
Expand All @@ -14,7 +11,7 @@ let
multiOutput = (flakeboxLib.craneMultiBuild { }) (
craneLib':
let
src = flakeboxLib.filterSubPaths {
src = flakeboxLib.source.fromPaths {
root = rootDir;
paths = buildPaths;
};
Expand Down
2 changes: 2 additions & 0 deletions checks/source-fixtures/baseline/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = []
1 change: 1 addition & 0 deletions checks/source-fixtures/baseline/nested/keep/value.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
keep
1 change: 1 addition & 0 deletions checks/source-fixtures/baseline/nested/manifest-link
1 change: 1 addition & 0 deletions checks/source-fixtures/baseline/nested/specs/ignored.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
excluded one
1 change: 1 addition & 0 deletions checks/source-fixtures/baseline/other/specs/ignored.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
excluded two
1 change: 1 addition & 0 deletions checks/source-fixtures/baseline/specs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
regular specs
1 change: 1 addition & 0 deletions checks/source-fixtures/baseline/specs2/value.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
specs2 keep
2 changes: 2 additions & 0 deletions checks/source-fixtures/excluded-change/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = []
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
keep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
changed excluded
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
excluded two
1 change: 1 addition & 0 deletions checks/source-fixtures/excluded-change/specs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
regular specs
1 change: 1 addition & 0 deletions checks/source-fixtures/excluded-change/specs2/value.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
specs2 keep
2 changes: 2 additions & 0 deletions checks/source-fixtures/selected-change/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[workspace]
members = []
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
changed selected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
excluded one
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
excluded two
1 change: 1 addition & 0 deletions checks/source-fixtures/selected-change/specs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
regular specs
1 change: 1 addition & 0 deletions checks/source-fixtures/selected-change/specs2/value.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
specs2 keep
143 changes: 143 additions & 0 deletions checks/source-tests.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{ pkgs, flakeboxLib }:
let
inherit (pkgs) lib;
source = flakeboxLib.source;
fixture = ./source-fixtures;

selectedPaths = [
"Cargo.toml"
"nested"
"other"
"specs"
"specs2"
];
denySpecs = source.filters.excludeDirectoriesNamed [ "specs" ];

mkFixtureSource =
root:
source.fromPaths {
inherit root;
paths = selectedPaths;
filter = denySpecs;
};

baseline = mkFixtureSource (fixture + "/baseline");
excludedChange = mkFixtureSource (fixture + "/excluded-change");
selectedChange = mkFixtureSource (fixture + "/selected-change");

buildFiles = source.filesetFromPaths {
root = fixture + "/baseline";
paths = [
"Cargo.toml"
"nested"
"nested/keep"
"Cargo.toml"
];
};
composed = source.fromFileset {
root = fixture + "/baseline";
fileset = lib.fileset.union (lib.fileset.difference buildFiles (
fixture + "/baseline/nested/specs"
)) (lib.fileset.maybeMissing (fixture + "/baseline/optional"));
};

filtered = source.fromPaths {
root = fixture + "/baseline";
paths = selectedPaths;
filter = source.filters.all [
denySpecs
(source.filters.not (path: type: type == "regular" && lib.hasSuffix "value.txt" path))
];
};

globbed = source.fromGlobs {
root = fixture + "/baseline";
patterns = [
"Cargo.toml"
"nested/**"
"specs2/**"
];
filter = denySpecs;
};
emptyGlob = source.fromGlobs {
root = fixture + "/baseline";
patterns = [ "not-present/**" ];
};

fails = value: !(builtins.tryEval (builtins.deepSeq value true)).success;
in
assert lib.assertMsg (
toString baseline == toString excludedChange
) "changes to excluded content must preserve the source store path";
assert lib.assertMsg (
toString baseline != toString selectedChange
) "changes to selected content must alter the source store path";
assert lib.assertMsg (
!(source.filters.all [
(_path: _type: false)
(_path: _type: throw "all did not short-circuit")
] "unused" "regular")
) "source.filters.all must short-circuit";
assert lib.assertMsg (source.filters.any [
(_path: _type: true)
(_path: _type: throw "any did not short-circuit")
] "unused" "regular") "source.filters.any must short-circuit";
assert lib.assertMsg (fails (
source.filesetFromPaths {
root = fixture + "/baseline";
paths = [ "" ];
}
)) "empty subpaths must be rejected";
assert lib.assertMsg (fails (
source.filesetFromPaths {
root = fixture + "/baseline";
paths = [ "../baseline" ];
}
)) "parent subpaths must be rejected";
assert lib.assertMsg (fails (
source.filesetFromPaths {
root = fixture + "/baseline";
paths = [ "/Cargo.toml" ];
}
)) "absolute subpaths must be rejected";
assert lib.assertMsg (fails (
source.fromPaths {
root = fixture + "/baseline";
paths = [ "missing" ];
}
)) "missing selected paths must be rejected";
assert lib.assertMsg (fails (
source.fromFileset {
root = fixture + "/baseline";
fileset = ./mergeArgs-tests.nix;
}
)) "filesets outside root must be rejected";
pkgs.runCommand "source-tests" { } ''
test -f ${baseline}/Cargo.toml
test -f ${baseline}/nested/keep/value.txt
test -L ${baseline}/nested/manifest-link
test -f ${baseline}/specs
test -f ${baseline}/specs2/value.txt
test ! -e ${baseline}/nested/specs
test ! -e ${baseline}/other/specs

test -f ${composed}/Cargo.toml
test -f ${composed}/nested/keep/value.txt
test -L ${composed}/nested/manifest-link
test ! -e ${composed}/nested/specs

test -f ${filtered}/Cargo.toml
test ! -e ${filtered}/nested/keep/value.txt
test ! -e ${filtered}/specs2/value.txt
test ! -e ${filtered}/nested/specs

test -f ${globbed}/Cargo.toml
test -f ${globbed}/nested/keep/value.txt
test -L ${globbed}/nested/manifest-link
test -f ${globbed}/specs2/value.txt
test ! -e ${globbed}/nested/specs

test -z "$(find ${emptyGlob} -mindepth 1 -print -quit)"

touch $out
''
10 changes: 3 additions & 7 deletions checks/workspace-cross-compile/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@
let
inherit (pkgs) lib;

rootDir = builtins.path {
name = "workspace-sanity";
path = ./.;
};
rootDir = ./.;

# paths needed to build Rust code
buildPaths = [
"Cargo.toml"
"Cargo.lock"
".cargo"
"bin"
"lib"
"rocksdb"
Expand All @@ -29,7 +25,7 @@ let
multiOutput = (flakeboxLib.craneMultiBuild { }) (
craneLib':
let
src = flakeboxLib.filterSubPaths {
src = flakeboxLib.source.fromPaths {
root = rootDir;
paths = buildPaths;
};
Expand Down Expand Up @@ -88,7 +84,7 @@ let
./scripts/e2e-tests.sh
'';

src = flakeboxLib.filterSubPaths {
src = flakeboxLib.source.fromPaths {
root = rootDir;
paths = testPaths;
};
Expand Down
10 changes: 3 additions & 7 deletions checks/workspace-sanity/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
let
inherit (pkgs) lib;

rootDir = builtins.path {
name = "workspace-sanity";
path = ./.;
};
rootDir = ./.;

# paths needed to build Rust code
buildPaths = [
"Cargo.toml"
"Cargo.lock"
".cargo"
"bin"
"lib"
];
Expand All @@ -24,7 +20,7 @@ let
multiOutput = (flakeboxLib.craneMultiBuild { }) (
craneLib':
let
src = flakeboxLib.filterSubPaths {
src = flakeboxLib.source.fromPaths {
root = rootDir;
paths = buildPaths;
};
Expand Down Expand Up @@ -78,7 +74,7 @@ let
./scripts/e2e-tests.sh
'';

src = flakeboxLib.filterSubPaths {
src = flakeboxLib.source.fromPaths {
root = rootDir;
paths = testPaths;
};
Expand Down
4 changes: 2 additions & 2 deletions docs/best-practices.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ See [build profile Overrides section in The Cargo Book](https://doc.rust-lang.or
Avoiding rebuilds is an important part of optimized Nix-based CI. It's especially
important in slow to compile language like Rust.

Flakebox provides `flakeboxLib.filterSubPaths` function for convenient
source code filtering.
Flakebox provides `flakeboxLib.source.fromPaths` for validated, composable source filtering.
Pass a raw path such as `root = ./.` and list every build input explicitly.


### Use mold when possible
Expand Down
18 changes: 6 additions & 12 deletions docs/building-new-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,8 @@ index cb41316..08c8f65 100644
pkgs = nixpkgs.legacyPackages.${system};
flakeboxLib = flakebox.lib.mkLib pkgs { };
+
+ rustSrc = flakeboxLib.filterSubPaths {
+ root = builtins.path {
+ name = "flakebox-tutorial";
+ path = ./.;
+ };
+ rustSrc = flakeboxLib.source.fromPaths {
+ root = ./.;
+ paths = [
+ "Cargo.toml"
+ "Cargo.lock"
Expand Down Expand Up @@ -434,11 +431,8 @@ This is where you can enable, disable, and configure various Flakebox features.
The next binding is:

```nix
rustSrc = flakeboxLib.filterSubPaths {
root = builtins.path {
name = "flakebox-tutorial";
path = ./.;
};
rustSrc = flakeboxLib.source.fromPaths {
root = ./.;
paths = [
"Cargo.toml"
"Cargo.lock"
Expand All @@ -448,7 +442,7 @@ The next binding is:
};
```

This `filterSubPaths` is a function exposed by
This `source.fromPaths` function is exposed by
`flakeboxLib` and is used for easy source code filtering.
This is useful to avoid having to rebuild our Rust project
when only irrelevant files changed. It's not strictly
Expand Down Expand Up @@ -724,7 +718,7 @@ index a65ba7a..f4c64d7 100644
pkgs = nixpkgs.legacyPackages.${system};
flakeboxLib = flakebox.lib.mkLib pkgs { };

rustSrc = flakeboxLib.filterSubPaths {
rustSrc = flakeboxLib.source.fromPaths {
@@ -36,6 +41,10 @@
craneLib = (craneLib'.overrideArgs {
pname = "flexbox-multibuild";
Expand Down
Loading
Loading