Skip to content

Commit

Permalink
Nix flake and rustup toolchain format update (linera-io#1162)
Browse files Browse the repository at this point in the history
* Add newer-format rust-toolchain with WASM support

The newer TOML `rust-toolchain.toml` format allows specifying more
things about the compiler.  Importantly, it allows us to require
support for particular targets, like `wasm32-unknown-unknown`.

We keep the legacy `rust-toolchain` file, in the legacy format, as it
is depended upon by CI.

rust-toolchain documentation:
https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file

* Add Nix develop environment with Rust and WASM support

Reads the Rust toolchain specification from the rustup
`rust-toolchain.toml` file, and uses it to provide a development
environment for `linera-protocol` with required non-Rust dependencies
and standard development tools (`rustfmt`).
  • Loading branch information
Twey authored Oct 27, 2023
1 parent 7d8562d commit 3ac3bee
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 3 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@

# VSCode
.vscode
/.direnv
184 changes: 184 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 69 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
systems.url = "github:nix-systems/default";
rust-overlay.url = "github:oxalica/rust-overlay";

# Dev tools
treefmt-nix.url = "github:numtide/treefmt-nix";
};

outputs = inputs: inputs.flake-parts.lib.mkFlake { inherit inputs; } {
systems = import inputs.systems;
imports = [
inputs.treefmt-nix.flakeModule
];
perSystem = { config, self', pkgs, lib, system, rust-overlay, ... }:
let
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
nonRustDeps = with pkgs; [
libclang.lib
libiconv
openssl
protobuf
pkg-config
nodejs
];
rustToolchain = (pkgs.rust-bin.fromRustupToolchainFile
./rust-toolchain.toml);
rustPlatform = pkgs.makeRustPlatform {
rustc = rustToolchain;
cargo = rustToolchain;
};
in {
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
overlays = [ (import inputs.rust-overlay) ];
};

# Rust dev environment
devShells.default = pkgs.mkShell {
inputsFrom = [
config.treefmt.build.devShell
];
shellHook = ''
# For rust-analyzer 'hover' tooltips to work.
export RUST_SRC_PATH=${rustToolchain.availableComponents.rust-src}
export LIBCLANG_PATH=${pkgs.libclang.lib}/lib
export PATH=$PWD/target/release:~/.cargo/bin:$PATH
'';
buildInputs = nonRustDeps;
nativeBuildInputs = with pkgs; [
rustToolchain
rust-analyzer
];
};

# Add your auto-formatters here.
# cf. https://numtide.github.io/treefmt/
treefmt.config = {
projectRootFile = "flake.nix";
programs = {
nixpkgs-fmt.enable = true;
rustfmt.enable = true;
};
};
};
};
}
10 changes: 7 additions & 3 deletions linera-service/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,15 @@ impl Project {
}

fn create_rust_toolchain(project_root: &Path) -> Result<()> {
let toolchain_path = project_root.join("rust-toolchain");
Self::write_string_to_file(
&toolchain_path,
&project_root.join("rust-toolchain"),
include_str!("../template/rust-toolchain.template"),
)
)?;
Self::write_string_to_file(
&project_root.join("rust-toolchain.toml"),
include_str!("../template/rust-toolchain.toml.template"),
)?;
Ok(())
}

fn create_state_file(source_directory: &Path) -> Result<()> {
Expand Down
1 change: 1 addition & 0 deletions linera-service/template/rust-toolchain.toml.template
4 changes: 4 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[toolchain]
channel = "1.72.0"
components = [ "rustfmt", "rustc-dev", "rust-src" ]
targets = [ "wasm32-unknown-unknown" ]
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(builtins.getFlake ("git+file://" + toString ./.)).devShells.${builtins.currentSystem}.default

0 comments on commit 3ac3bee

Please sign in to comment.