Rust: Difference between revisions

From NixOS Wiki
imported>Nyarly
No edit summary
imported>TheSirC
Adding Emacs support
Line 32: Line 32:
})
})
</syntaxHighlight>
</syntaxHighlight>
== 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 [https://github.com/direnv/direnv/wiki/Nix direnv] and its package integration in Emacs [https://github.com/wbolster/emacs-direnv emacs-direnv].
=== Global solution ===
If you happen to have all of your Rust projects in one folder "<code>/path/to/your/rust</code>" then placing a file <code>/path/to/your/rust/.envrc</code> with this content :
<syntaxHighlight lang="shell">
use_nix
</syntaxHighlight>
and  another file <code>/path/to/your/rust/default.nix</code> :
<syntaxHighlight lang="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 nasm rustup ruststable cmake zlib ];
    shellHook = ''
        export OPENSSL_DIR="${openssl.dev}"
        export OPENSSL_LIB_DIR="${openssl.out}/lib"
    '';
  }
</syntaxHighlight>
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 [https://github.com/direnv/direnv/wiki/Nix#persistent-shell-for-speeding-things-up the direnv wiki] will be poorly be needed !

Revision as of 10:37, 20 June 2019


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 nasm rustup ruststable cmake zlib ];

    shellHook = ''
        export OPENSSL_DIR="${openssl.dev}"
        export OPENSSL_LIB_DIR="${openssl.out}/lib"
    '';
  }

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 !