Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Splice overrideToolchain #652

Merged
merged 10 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
`craneLib.cleanCargoSource (craneLib.path ./.)`): it is recommended to either
use `craneLib.cleanCargoSource ./.` directly (if the default source cleaning
is desired) or `craneLib.path ./.` (if not).
* `overrideToolchain` has been updated to better handle cross-compilation
splicing for a customized toolchain. This means that `overrideToolchain`
should now be called with a function which constructs said toolchain for any
given `pkgs` instantiation. For example: `craneLib.overrideToolchain (p:
p.rust-bin.stable.latest.default)`

### Fixed
* The cross compilation example also hows how to set the `TARGET_CC` environment
Expand Down
6 changes: 3 additions & 3 deletions checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ in
onlyDrvs (lib.makeScope myLib.newScope (self:
let
callPackage = self.newScope { };
myLibLlvmTools = myLib.overrideToolchain (pkgs.rust-bin.stable.latest.minimal.override {
myLibLlvmTools = myLib.overrideToolchain (p: p.rust-bin.stable.latest.minimal.override {
extensions = [ "llvm-tools" ];
});
x64Linux = pkgs.hostPlatform.system == "x86_64-linux";
Expand Down Expand Up @@ -407,7 +407,7 @@ in

noStd =
let
noStdLib = myLib.overrideToolchain (pkgs.rust-bin.stable.latest.minimal.override {
noStdLib = myLib.overrideToolchain (p: p.rust-bin.stable.latest.minimal.override {
targets = [
"thumbv6m-none-eabi"
"x86_64-unknown-none"
Expand All @@ -423,7 +423,7 @@ in

bindeps =
let
bindepsLib = myLib.overrideToolchain (pkgs.rust-bin.nightly.latest.minimal.override {
bindepsLib = myLib.overrideToolchain (p: p.rust-bin.nightly.latest.minimal.override {
targets = [
"wasm32-unknown-unknown"
"x86_64-unknown-none"
Expand Down
4 changes: 2 additions & 2 deletions checks/trunk.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
}:

let
wasmToolchain = pkgs.rust-bin.stable.latest.minimal.override {
wasmToolchainFor = p: p.rust-bin.stable.latest.minimal.override {
targets = [ "wasm32-unknown-unknown" ];
};

tarball = builtins.fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/4e6868b1aa3766ab1de169922bb3826143941973.tar.gz";
sha256 = "sha256:1q6bj2jjlwb10sfrhqmjpzsc3yc4x76cvky16wh0z52p7d2lhdpv";
};
myLibWasm = (myLib.overrideToolchain wasmToolchain).overrideScope (_final: _prev: {
myLibWasm = (myLib.overrideToolchain wasmToolchainFor).overrideScope (_final: _prev: {
inherit (import tarball { inherit (stdenv) system; }) wasm-bindgen-cli;
});

Expand Down
12 changes: 1 addition & 11 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
{ pkgs ? import <nixpkgs> { } }:

import ./lib {
inherit (pkgs) lib makeScopeWithSplicing';
otherSplices = {
selfBuildBuild = pkgs.pkgsBuildBuild;
selfBuildHost = pkgs.pkgsBuildHost;
selfBuildTarget = pkgs.pkgsBuildTarget;
selfHostHost = pkgs.pkgsHostHost;
selfHostTarget = pkgs.pkgsHostTarget;
selfTargetTarget = pkgs.pkgsTargetTarget;
};
}
pkgs.callPackage ./lib { }
9 changes: 7 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1193,15 +1193,20 @@ build caches. More specifically:

### `craneLib.overrideToolchain`

`overrideToolchain :: drv -> set`
`overrideToolchain :: (set -> drv) -> set`
`overrideToolchain :: drv -> set` (deprecated)
szlend marked this conversation as resolved.
Show resolved Hide resolved

A convenience method to override and use tools (like `cargo`, `clippy`,
`rustfmt`, `rustc`, etc.) from one specific toolchain. The input should be a
single derivation which contains all the tools as binaries. For example, this
can be the output of `oxalica/rust-overlay`.

Note that in order to best support cross compilation, `overrideToolchain` should
be provided a function (whose argument is a cross-compilation aware version of
`pkgs`) which constructs the toolchain:

```nix
craneLib.overrideToolchain myCustomToolchain
craneLib.overrideToolchain (p: myCustomToolchainForPkgs p)
```

### `craneLib.path`
Expand Down
8 changes: 3 additions & 5 deletions docs/faq/custom-nixpkgs.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ let
inherit system;
overlays = [ (import rust-overlay) ];
};
rustToolchain = pkgs.rust-bin.stable.latest.default.override {
targets = [ "wasm32-wasi" ];
};
in
(crane.mkLib pkgs).overrideToolchain rustToolchain
(crane.mkLib pkgs).overrideToolchain (p: p.rust-bin.stable.latest.default.override {
targets = [ "wasm32-wasi" ];
})
```

Finally, specific inputs can be overridden for the entire library via the
Expand Down
5 changes: 3 additions & 2 deletions docs/faq/invalid-metadata-files-for-crate.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ used in the build:

```nix
let
rustToolchain = ...;
rustToolchainForPkgs = p: ...;
rustToolchain = rustToolchainForPkgs pkgs;
in
# Incorrect usage, missing `clippy` override!
#(crane.mkLib pkgs).overrideScope (final: prev: {
Expand All @@ -17,5 +18,5 @@ in
#});
# Correct usage (`overrideToolchain` handles the details for us)
(crane.mkLib pkgs).overrideToolchain rustToolchain
(crane.mkLib pkgs).overrideToolchain rustToolchainForPkgs
```
2 changes: 1 addition & 1 deletion docs/overriding_derivations.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ craneLib.buildPackage {
# Use a different `craneLib` instantiation: one with a nightly compiler
my-crate-nightly = my-crate.override {
craneLib = craneLib.overrideToolchain pkgs.rust-bin.nightly.latest.default;
craneLib = craneLib.overrideToolchain (p: p.rust-bin.nightly.latest.default);
};
};
});
Expand Down
6 changes: 3 additions & 3 deletions examples/build-std/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@
overlays = [ (import rust-overlay) ];
};

rustToolchain = pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override {
rustToolchainFor = p: p.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default.override {
extensions = [ "rust-src" ];
targets = [ "x86_64-unknown-linux-gnu" ];
});
rustToolchain = rustToolchainFor pkgs;

# NB: we don't need to overlay our custom toolchain for the *entire*
# pkgs (which would require rebuidling anything else which uses rust).
# Instead, we just want to update the scope that crane will use by appending
# our specific toolchain there.
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchainFor;

src = craneLib.cleanCargoSource ./.;

Expand Down Expand Up @@ -83,7 +84,6 @@
# Extra inputs can be added here; cargo and rustc are provided by default
# from the toolchain that was specified earlier.
packages = [
# rustToolchain
];
};
});
Expand Down
6 changes: 2 additions & 4 deletions examples/cross-musl/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@
overlays = [ (import rust-overlay) ];
};

rustToolchain = pkgs.rust-bin.stable.latest.default.override {
craneLib = (crane.mkLib pkgs).overrideToolchain (p: p.rust-bin.stable.latest.default.override {
targets = [ "x86_64-unknown-linux-musl" ];
};

craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
});

my-crate = craneLib.buildPackage {
src = craneLib.cleanCargoSource ./.;
Expand Down
6 changes: 1 addition & 5 deletions examples/cross-rust-overlay/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@
overlays = [ (import rust-overlay) ];
};

rustToolchain = pkgs.pkgsBuildHost.rust-bin.stable.latest.default.override {
targets = [ "aarch64-unknown-linux-gnu" ];
};

craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
craneLib = (crane.mkLib pkgs).overrideToolchain (p: p.rust-bin.stable.latest.default);

# Note: we have to use the `callPackage` approach here so that Nix
# can "splice" the packages in such a way that dependencies are
Expand Down
9 changes: 3 additions & 6 deletions examples/custom-toolchain/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@
overlays = [ (import rust-overlay) ];
};

rustWithWasiTarget = pkgs.rust-bin.stable.latest.default.override {
targets = [ "wasm32-wasi" ];
};

# NB: we don't need to overlay our custom toolchain for the *entire*
# pkgs (which would require rebuidling anything else which uses rust).
# Instead, we just want to update the scope that crane will use by appending
# our specific toolchain there.
craneLib = (crane.mkLib pkgs).overrideToolchain rustWithWasiTarget;
craneLib = (crane.mkLib pkgs).overrideToolchain (p: p.rust-bin.stable.latest.default.override {
targets = [ "wasm32-wasi" ];
});

my-crate = craneLib.buildPackage {
src = craneLib.cleanCargoSource ./.;
Expand Down Expand Up @@ -76,7 +74,6 @@
# Extra inputs can be added here; cargo and rustc are provided by default
# from the toolchain that was specified earlier.
packages = [
# rustWithWasiTarget
];
};
});
Expand Down
3 changes: 1 addition & 2 deletions examples/end-to-end-testing/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
};
inherit (pkgs) lib;

rustToolchain = pkgs.rust-bin.stable.latest.default;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
craneLib = (crane.mkLib pkgs).overrideToolchain (p: p.rust-bin.stable.latest.default);
src = craneLib.cleanCargoSource ./.;

workspace = craneLib.buildPackage {
Expand Down
4 changes: 2 additions & 2 deletions examples/trunk-workspace/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@

inherit (pkgs) lib;

rustToolchain = pkgs.rust-bin.stable.latest.default.override {
rustToolchainFor = p: p.rust-bin.stable.latest.default.override {
# Set the build targets supported by the toolchain,
# wasm32-unknown-unknown is required for trunk.
targets = [ "wasm32-unknown-unknown" ];
};
craneLib = ((crane.mkLib pkgs).overrideToolchain rustToolchain).overrideScope (_final: _prev: {
craneLib = ((crane.mkLib pkgs).overrideToolchain rustToolchainFor).overrideScope (_final: _prev: {
# The version of wasm-bindgen-cli needs to match the version in Cargo.lock. You
# can unpin this if your nixpkgs commit contains the appropriate wasm-bindgen-cli version
inherit (import nixpkgs-for-wasm-bindgen { inherit system; }) wasm-bindgen-cli;
Expand Down
4 changes: 2 additions & 2 deletions examples/trunk/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@

inherit (pkgs) lib;

rustToolchain = pkgs.rust-bin.stable.latest.default.override {
rustToolchainFor = p: p.rust-bin.stable.latest.default.override {
# Set the build targets supported by the toolchain,
# wasm32-unknown-unknown is required for trunk
targets = [ "wasm32-unknown-unknown" ];
};
craneLib = ((crane.mkLib pkgs).overrideToolchain rustToolchain).overrideScope (_final: _prev: {
craneLib = ((crane.mkLib pkgs).overrideToolchain rustToolchainFor).overrideScope (_final: _prev: {
# The version of wasm-bindgen-cli needs to match the version in Cargo.lock. You
# can unpin this if your nixpkgs commit contains the appropriate wasm-bindgen-cli version
inherit (import nixpkgs-for-wasm-bindgen { inherit system; }) wasm-bindgen-cli;
Expand Down
Loading