Rust: Difference between revisions

imported>TenTypekMatus
m (Add some fixes, notably with devenv.sh)
imported>AnyTimeTraveler
m (Integrate rust-toolchain pinning mechanism with existing rust conventions)
Line 30: Line 30:
<syntaxHighlight lang="nix">
<syntaxHighlight lang="nix">
{ pkgs ? import <nixpkgs> {} }:
{ pkgs ? import <nixpkgs> {} }:
  let
    overrides = (builtins.fromTOML (builtins.readFile ./rust-toolchain.toml));
    libPath = with pkgs; lib.makeLibraryPath [
      # load external libraries that you need in your rust project here
    ];
in
   pkgs.mkShell rec {
   pkgs.mkShell rec {
     buildInputs = with pkgs; [
     buildInputs = with pkgs; [
Line 37: Line 43:
       rustup
       rustup
     ];
     ];
     RUSTC_VERSION = pkgs.lib.readFile ./rust-toolchain;
     RUSTC_VERSION = overrides.toolchain.channel;
     # https://github.com/rust-lang/rust-bindgen#environment-variables
     # https://github.com/rust-lang/rust-bindgen#environment-variables
     LIBCLANG_PATH = pkgs.lib.makeLibraryPath [ pkgs.llvmPackages_latest.libclang.lib ];
     LIBCLANG_PATH = pkgs.lib.makeLibraryPath [ pkgs.llvmPackages_latest.libclang.lib ];
Line 48: Line 54:
       # add libraries here (e.g. pkgs.libvmi)
       # add libraries here (e.g. pkgs.libvmi)
     ]);
     ]);
    LD_LIBRARY_PATH = libPath;
     # Add glibc, clang, glib, and other headers to bindgen search path
     # Add glibc, clang, glib, and other headers to bindgen search path
     BINDGEN_EXTRA_CLANG_ARGS =  
     BINDGEN_EXTRA_CLANG_ARGS =
     # Includes normal include path
     # Includes normal include path
     (builtins.map (a: ''-I"${a}/include"'') [
     (builtins.map (a: ''-I"${a}/include"'') [
       # add dev libraries here (e.g. pkgs.libvmi.dev)
       # add dev libraries here (e.g. pkgs.libvmi.dev)
       pkgs.glibc.dev  
       pkgs.glibc.dev
     ])
     ])
     # Includes with special directory paths
     # Includes with special directory paths
Line 61: Line 68:
       ''-I${pkgs.glib.out}/lib/glib-2.0/include/''
       ''-I${pkgs.glib.out}/lib/glib-2.0/include/''
     ];
     ];
   }
   }
</syntaxHighlight>
</syntaxHighlight>


It's important to have a file named <code>rust-toolchain</code> lying in the same directory as the shell.nix.
It's important to have a file named <code>rust-toolchain.toml</code> lying in the same directory as the shell.nix.
Its purpose is to pin the version of the used Rust compiler.
Rust already has a standardized way of pinning a toolchain version for a workspace or a project.
<syntaxHighlight lang="shell-session">
See [https://rust-lang.github.io/rustup/overrides.html#the-toolchain-file the Rustup book] for its syntax.
$ cat rust-toolchain
 
nightly-2021-09-19
A minimal example of the <code>rust-toolchain.toml</code>:
<syntaxHighlight lang="toml">
[toolchain]
channel = "stable"
</syntaxHighlight>
</syntaxHighlight>