switch from xOnly to mode option

This commit is contained in:
Robin Appelman 2023-06-04 21:40:50 +02:00 committed by Patryk Wychowaniec
parent 9327600ac6
commit fd1b74c213
5 changed files with 61 additions and 51 deletions

View file

@ -258,9 +258,8 @@ process, rest is passed-through into `mkDerivation`.
| `copyTarget` | When true, the `target/` directory is copied to `$out`. Default: `false` |
| `postInstall` | Optional hook to run after the compilation is done; inside this script, `$out/bin` contains compiled Rust binaries. Useful if your application needs e.g. custom environment variables, in which case you can simply run `wrapProgram $out/bin/your-app-name` in here. Default: `false` |
| `usePureFromTOML` | Whether to use the `fromTOML` built-in or not. When set to `false` the python package `remarshal` is used instead (in a derivation) and the JSON output is read with `builtins.fromJSON`. This is a workaround for old versions of Nix. May be used safely from Nix 2.3 onwards where all bugs in `builtins.fromTOML` seem to have been fixed. Default: `true` |
| `checkOnly` | When true, only runs `cargo check` during build instead of building the project, overwrites `cargoBuild`. Default: `false` |
| `testOnly` | When true, only runs `cargo test` during build instead of building the project, overwrites `cargoBuild`. Default: `false` |
| `testOnly` | When true, only runs `cargo test` during build instead of building the project, overwrites `cargoBuild`. Default: `false` |
| `mode` | What to do when building the derivation. Either `build`, `check`, `test` or `clippy`. <br/> When set to something other than `build`, no binaries are generated. Default: `"build"` |
## Examples

View file

@ -1,6 +1,6 @@
{ src
#| What command to run during the build phase
, cargoBuild
, cargoCommand
, cargoBuildOptions
, remapPathPrefix
, #| What command to run during the test phase
@ -36,9 +36,7 @@
# Which drops the run-time dependency on the crates-io source thereby
# significantly reducing the Nix closure size.
, removeReferencesToSrcFromDocs
, checkOnly ? false
, testOnly ? false
, clippyOnly ? false
, mode ? "build" # `build`, `check`, `test` or `clippy`
, gitDependencies
, pname
, version
@ -124,7 +122,7 @@ let
jq
rsync
] ++ nativeBuildInputs
++ lib.optionals clippyOnly [clippy];
++ lib.optionals (mode == "clippy") [clippy];
buildInputs = lib.optionals stdenv.isDarwin [
darwin.Security
@ -241,7 +239,7 @@ let
export SOURCE_DATE_EPOCH=1
cargo_ec=0
logRun ${cargoBuild} || cargo_ec="$?"
logRun ${cargoCommand} || cargo_ec="$?"
if [ "$cargo_ec" -ne "0" ]; then
cat "$cargo_build_output_json" | jq -cMr 'select(.message.rendered != null) | .message.rendered'

View file

@ -178,14 +178,9 @@ let
# fixed.
usePureFromTOML = attrs0.usePureFromTOML or true;
# When true, only run `cargo check`.
checkOnly = attrs0.checkOnly or false;
# When true, only run `cargo test`.
testOnly = attrs0.testOnly or false;
# When true, only run `cargo clippy`.
clippyOnly = attrs0.clippyOnly or false;
# What to do when building the derivation. Either `build`, `check`, `test` or `clippy`. <br/>
# When set to something other than `build`, no binaries are generated.
mode = attrs0.mode or "build";
};
argIsAttrs =
@ -240,18 +235,22 @@ let
usePureFromTOML = attrs.usePureFromTOML;
readTOML = builtinz.readTOML usePureFromTOML;
cargoBuildOverwrite = let
inherit (lib.attrsets) optionalAttrs;
in (optionalAttrs attrs.checkOnly {
cargoBuild = ''cargo $cargo_options check $cargo_build_options >> $cargo_build_output_json'';
}) // (optionalAttrs attrs.testOnly {
cargoBuild = ''cargo $cargo_options test $cargo_test_options >> $cargo_build_output_json'';
}) // (optionalAttrs attrs.clippyOnly {
cargoBuild = ''cargo $cargo_options clippy $cargo_build_options -- -D warnings >> $cargo_build_output_json'';
});
cargoCommand = let
mode = attrs.mode;
in
if (mode == "build") then
attrs.cargoBuild
else if (mode == "check") then
''cargo $cargo_options check $cargo_build_options >> $cargo_build_output_json''
else if (mode == "test") then
''cargo $cargo_options test $cargo_test_options >> $cargo_build_output_json''
else if (mode == "clippy") then
''cargo $cargo_options clippy $cargo_build_options -- -D warnings >> $cargo_build_output_json''
else throw "Unknown mode ${mode}, allowed modes: build, check, test, clippy";
# config used during build the prebuild and the final build
buildConfig = {
inherit cargoCommand;
inherit (attrs)
nativeBuildInputs
buildInputs
@ -260,7 +259,6 @@ let
cargoOptions
compressTarget
cargoBuild
cargoBuildOptions
remapPathPrefix
copyBins
@ -287,7 +285,7 @@ let
# Example:
# [ { name = "wabt", version = "2.0.6", sha256 = "..." } ]
crateDependencies = libb.mkVersions buildPlanConfig.cargolock;
} // cargoBuildOverwrite;
};
# config used when planning the builds
buildPlanConfig = rec {

View file

@ -1,12 +1,15 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1656928814,
"narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=",
"lastModified": 1685518550,
"narHash": "sha256-o2d0KcvaXzTrPRIo0kOLV0/QXHhDQ5DTi+OxcjO8xqY=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249",
"rev": "a1720a10a6cfe8234c0e93907ffe81be440f4cef",
"type": "github"
},
"original": {
@ -20,24 +23,22 @@
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1655042882,
"narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=",
"owner": "nix-community",
"repo": "naersk",
"rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f",
"type": "github"
"lastModified": 1685906393,
"narHash": "sha256-RPYzrgUszN8HsO5ONqZOZN/bO/E6Ix51dz6oZid0+e8=",
"path": "/home/robin/Projects/naersk",
"type": "path"
},
"original": {
"owner": "nix-community",
"repo": "naersk",
"type": "github"
"path": "/home/robin/Projects/naersk",
"type": "path"
}
},
"nixpkgs": {
"locked": {
"lastModified": 0,
"narHash": "sha256-0h1FzkYWei24IdKNpCX93onkF/FMiXQG8SdEbTc0r8A=",
"path": "/nix/store/fj7xz1cv9c8nrvdyd6bxhwq3l55k47xc-source",
"lastModified": 1685620773,
"narHash": "sha256-iQ+LmporQNdLz8uMJdP62TaAWeLUwl43/MYUBtWqulM=",
"path": "/nix/store/ipbqg8zvymxjlw96pl2mvgpigzc3wm7p-source",
"rev": "f0ba8235153dd2e25cf06cbf70d43efdd4443592",
"type": "path"
},
"original": {
@ -47,11 +48,11 @@
},
"nixpkgs_2": {
"locked": {
"lastModified": 1657292830,
"narHash": "sha256-ldfVSTveWceDCmW6gf3B4kR6vwmz/XS80y5wsLLHFJU=",
"lastModified": 1685866647,
"narHash": "sha256-4jKguNHY/edLYImB+uL8jKPL/vpfOvMmSlLAGfxSrnY=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "334ec8b503c3981e37a04b817a70e8d026ea9e84",
"rev": "a53a3bec10deef6e1cc1caba5bc60f53b959b1e8",
"type": "github"
},
"original": {
@ -67,6 +68,21 @@
"naersk": "naersk",
"nixpkgs": "nixpkgs_2"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",

View file

@ -1,8 +1,7 @@
{
inputs = {
flake-utils.url = "github:numtide/flake-utils";
# naersk.url = "github:nix-community/naersk";
naersk.url = "path:/home/robin/Projects/naersk";
naersk.url = "github:nix-community/naersk";
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
@ -24,17 +23,17 @@
# Run `nix build .#check` to check code
check = naersk'.buildPackage {
src = ./.;
checkOnly = true;
mode = "check";
};
# Run `nix build .#test` to run tests
test = naersk'.buildPackage {
src = ./.;
testOnly = true;
mode = "test";
};
# Run `nix build .#clippy` to lint code
clippy = naersk'.buildPackage {
src = ./.;
clippyOnly = true;
mode = "clippy";
};
};