Rust: Difference between revisions

Artturin (talk | contribs)
Make everything more cross-compatible
Artturin (talk | contribs)
Installation via rustup: use bindgenHook and remove now uneccesary attrs
Line 94: Line 94:


If you want the most "normal" Rust experience I recommend using rustup with the following example shell.nix:
If you want the most "normal" Rust experience I recommend using rustup with the following example shell.nix:
<syntaxHighlight lang="nix">
<syntaxhighlight lang="nix">
{ pkgs ? import <nixpkgs> {} }:
{
  let
  pkgs ? import <nixpkgs> { },
    overrides = (builtins.fromTOML (builtins.readFile ./rust-toolchain.toml));
}:
    libPath = with pkgs; lib.makeLibraryPath [
let
      # load external libraries that you need in your rust project here
  overrides = (builtins.fromTOML (builtins.readFile ./rust-toolchain.toml));
    ];
in
in
  pkgs.mkShell rec {
pkgs.callPackage (
     buildInputs = with pkgs; [
  {
      clang
     stdenv,
      # Replace llvmPackages with llvmPackages_X, where X is the latest LLVM version (at the time of writing, 16)
    mkShell,
      llvmPackages.bintools
    rustup,
    rustPlatform,
  }:
  mkShell {
    nativeBuildInputs = [
       rustup
       rustup
      rustPlatform.bindgenHook
     ];
     ];
    # libraries here
    buildInputs =
      [
      ];
     RUSTC_VERSION = overrides.toolchain.channel;
     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 ];
     shellHook = ''
     shellHook = ''
       export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin
       export PATH="''${CARGO_HOME:-~/.cargo}/bin":"$PATH"
       export PATH=$PATH:''${RUSTUP_HOME:-~/.rustup}/toolchains/$RUSTC_VERSION-x86_64-unknown-linux-gnu/bin/
       export PATH="''${RUSTUP_HOME:-~/.rustup}/toolchains/$RUSTC_VERSION-${stdenv.hostPlatform.rust.rustcTarget}/bin":"$PATH"
      '';
    '';
    # Add precompiled library to rustc search path
    RUSTFLAGS = (builtins.map (a: ''-L ${a}/lib'') [
      # add libraries here (e.g. pkgs.libvmi)
    ]);
    LD_LIBRARY_PATH = libPath;
    # Add glibc, clang, glib, and other headers to bindgen search path
    BINDGEN_EXTRA_CLANG_ARGS =
    # Includes normal include path
    (builtins.map (a: ''-I"${a}/include"'') [
      # add dev libraries here (e.g. pkgs.libvmi.dev)
      pkgs.glibc.dev
    ])
    # Includes with special directory paths
    ++ [
      ''-I"${pkgs.llvmPackages_latest.libclang.lib}/lib/clang/${pkgs.llvmPackages_latest.libclang.version}/include"''
      ''-I"${pkgs.glib.dev}/include/glib-2.0"''
      ''-I${pkgs.glib.out}/lib/glib-2.0/include/''
    ];
   }
   }
</syntaxHighlight>
) { }
 
</syntaxhighlight>


It's important to have a file named <code>rust-toolchain.toml</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.
Line 147: Line 138:
                   # or nightly-20XX-XX-XX for a specific nightly.
                   # or nightly-20XX-XX-XX for a specific nightly.
</syntaxhighlight>
</syntaxhighlight>
The important part is that this also works with complex setups using bindgen and precompiled C libraries. To add a new C library in the search path of bindgen and rustc edit the variables <code>BINDGEN_EXTRA_CLANG_ARGS</code> and <code>RUSTFLAGS</code>


== Cross-compiling ==
== Cross-compiling ==