Rust

From NixOS Wiki
Revision as of 19:45, 16 June 2020 by imported>Jtojnar (do not use pkgconfig alias)


This article is about the rust programming language.

Rust Nightlies

Either use rust overlay or rustup to get install rust nightlies.

Neovim Completion

Racer completion can be configured using the following snippet:

(neovim.override {
  configure = {
    customRC = ''
      if filereadable($HOME . "/.vimrc")
        source ~/.vimrc
      endif
      let $RUST_SRC_PATH = '${stdenv.mkDerivation {
        inherit (rustc) src;
        inherit (rustc.src) name;
        phases = ["unpackPhase" "installPhase"];
        installPhase = "cp -r src $out";
      }}'
    '';
    packages.nixbundle.start = with vimPlugins; [
      nvim-completion-manager
      nvim-cm-racer
    ];
  };
})

Emacs integration

Making Emacs use certain version of libraries can be tricky, in particular for example OpenSSL might not be found even if it is in your Nix store. Two solutions exists :

  • a "global" one
  • a per-project one

Both solution relies on the usage of a library called direnv and its package integration in Emacs emacs-direnv.

Global solution

If you happen to have all of your Rust projects in one folder "/path/to/your/rust" then placing a file /path/to/your/rust/.envrc with this content :

use_nix

and another file /path/to/your/rust/default.nix :

let
  moz_overlay = import (builtins.fetchTarball https://github.com/mozilla/nixpkgs-mozilla/archive/master.tar.gz);
  nixpkgs = import <nixpkgs> { overlays = [ moz_overlay ]; };
  ruststable = (nixpkgs.latest.rustChannels.stable.rust.override { extensions = [ "rust-src" "rls-preview" "rust-analysis" "rustfmt-preview" ];});
in
  with nixpkgs;
  stdenv.mkDerivation {
    name = "rust";
    buildInputs = [ openssl pkg-config nasm rustup ruststable cmake zlib ];
  }

will, for example, grant you a free acces to OpenSSL ! Feel free to override this to your liking !

Per-project solution

For a more granular solution to this problem a more in-depth review of the direnv wiki will be poorly be needed !