# How can I set up my Rust programming environment?

**URL:** https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501
**Category:** Help
**Created:** [October 23, 2019, 4:00pm UTC](https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501 "2019-10-23T16:00:21Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![anon70844979](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@anon70844979](https://discourse.nixos.org/u/anon70844979)
#### Post date: [October 23, 2019, 4:00pm UTC](https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501/1 "2019-10-23T16:00:21Z")

</div>

I am quite new to NixOS, and if you’re asking, I did read the manual.  
I want a nice guide on how to use **nix-shell** with **Rust** , managing Cargo packages, building my programs and other stuff.  
Also, share your own workflow if you are a Rust programmer, would be helpful for me to take as an example.

---

<div class="post-metadata">

### Author: ![FRidh](https://discourse.nixos.org/user_avatar/discourse.nixos.org/fridh/32/7643_2.png) [@FRidh](https://discourse.nixos.org/u/FRidh)
#### Post date: [October 23, 2019, 4:13pm UTC](https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501/2 "2019-10-23T16:13:27Z")

</div>

> [@anon70844979](#):
>
> and if you’re asking, I did read the manual.

Which manual? Also the Nixpkgs manual with the section on Rust with the section on `nix-shell` usage?  
[https://nixos.org/nixpkgs/manual/#users-guide-to-the-rust-infrastructure](https://nixos.org/nixpkgs/manual/#users-guide-to-the-rust-infrastructure)

---

<div class="post-metadata">

### Author: ![anon70844979](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/a/3ec8ea/32.png) [@anon70844979](https://discourse.nixos.org/u/anon70844979)
#### Post date: [October 23, 2019, 4:33pm UTC](https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501/3 "2019-10-23T16:33:08Z")

</div>

Oh, it looks like I only read [NixOS 23.11 manual | Nix & NixOS](https://nixos.org/nixos/manual/)  
Guess that’s another manual for me to read.  
I followed the guide on the page you linked, worked for building a simple hello world program.  
What I want to know now is how can I create a .nix file that when ran using nix-shell will install some packages like carnix, cargo and rustc  
Also that particular .nix file ran using nix-shell, what is it called? I’d like to call it by it’s name the next time I mention it.

---

<div class="post-metadata">

### Author: ![nyarly](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nyarly/32/17_2.png) [@nyarly](https://discourse.nixos.org/u/nyarly)
#### Post date: [October 24, 2019, 10:54pm UTC](https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501/4 "2019-10-24T22:54:35Z")

</div>

The simplest `shell.nix` for rust would look like

```nix
{ pkgs: import <nixpkgs> }:
pkgs.mkShell {
  buildInputs = with pkgs; [cargo carnix];
}

```

I think that’ll bring in all the dependencies you’d need. When you start needing outside dependencies (sqlite, openssl, etc) you can add them to that.

---

<div class="post-metadata">

### Author: ![danieldk](https://discourse.nixos.org/user_avatar/discourse.nixos.org/danieldk/32/31405_2.png) [@danieldk](https://discourse.nixos.org/u/danieldk)
#### Post date: [October 25, 2019, 6:13am UTC](https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501/5 "2019-10-25T06:13:45Z")

</div>

> [@anon70844979](#):
>
> Also, share your own workflow if you are a Rust programmer, would be helpful for me to take as an example.

For development I mostly use a `shell.nix` with the Rust toolchain. I use Mozilla’s [Rust overlay](https://github.com/NixOS/nixpkgs/blob/0109d6587a587170469cb11afc18a3afe71859a3/doc/languages-frameworks/rust.section.md#using-the-rust-nightlies-overlay) because it makes it so easy to get the current stable versions, nightlies, or even specific nightlies (for `pyo3`). E.g.

```nix
with import <nixpkgs> {};

stdenv.mkDerivation rec {
  name = "finalfusion-utils-env";
  env = buildEnv { name = name; paths = buildInputs; };

  buildInputs = [
    latest.rustChannels.stable.rust
}

```

To make something properly buildable, I add `default.nix` using `crate2nix` + `buildRustCrate` or `buildRustPackage`.

---

<div class="post-metadata">

### Author: ![samuela](https://discourse.nixos.org/user_avatar/discourse.nixos.org/samuela/32/2486_2.png) [@samuela](https://discourse.nixos.org/u/samuela)
#### Post date: [March 3, 2021, 6:35am UTC](https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501/6 "2021-03-03T06:35:16Z")

</div>

FWIW here’s my shell.nix for rust stuff:

```auto
let
  # Last updated: 2/26/21. Update as necessary from https://status.nixos.org/...
  pkgs = import (fetchTarball("https://github.com/NixOS/nixpkgs/archive/04ac9dcd311956d1756d77f4baf9258392ee7bdd.tar.gz")) {};

  # Rolling updates, not deterministic.
  # pkgs = import (fetchTarball("channel:nixpkgs-unstable")) {};
in pkgs.mkShell {
  buildInputs = [
    pkgs.cargo
    pkgs.rustc
    pkgs.rustfmt

    # Necessary for the openssl-sys crate:
    pkgs.openssl
    pkgs.pkg-config
  ];

  # See https://discourse.nixos.org/t/rust-src-not-found-and-other-misadventures-of-developing-rust-on-nixos/11570/3?u=samuela.
  RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
}

```

---

<div class="post-metadata">

### Author: ![efx](https://discourse.nixos.org/user_avatar/discourse.nixos.org/efx/32/7925_2.png) [@efx](https://discourse.nixos.org/u/efx)
#### Post date: [March 3, 2021, 1:18pm UTC](https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501/7 "2021-03-03T13:18:31Z")

</div>

Howdy!  
My comments are slightly off-topic since they do not constitute a full how-to guide.

I have followed examples on the [wiki](https://nixos.wiki/wiki/Rust). These examples demonstrate a few options. But they could also be out-of-date or inaccurate.

Yesterday I learned about another Rust overlay from @oxalica: [GitHub - oxalica/rust-overlay: Pure and reproducible nix overlay of binary distributed rust toolchains](https://github.com/oxalica/rust-overlay#use-as-a-classic-nix-overlay)  
This overlay looks incredibly promising because it has _precompiled_ toolchains. I have seen the Mozilla overlay recompile a full toolchain before.

I hope to write a `shell.nix` using oxalica’s rust-overlay for another OSS project. I’ll post a link here once I get around to it!

---

<div class="post-metadata">

### Author: ![zzantares](https://discourse.nixos.org/user_avatar/discourse.nixos.org/zzantares/32/3994_2.png) [@zzantares](https://discourse.nixos.org/u/zzantares)
#### Post date: [May 23, 2021, 5:17pm UTC](https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501/8 "2021-05-23T17:17:52Z")

</div>

For others looking, it turns out [Oxalica’s Rust overlay](https://github.com/oxalica/rust-overlay) is very nice, here is a `shell.nix` file I use:

```nix
{ nixpkgs ? import <nixpkgs> { }}:

let
  rustOverlay = builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz";
  pinnedPkgs = nixpkgs.fetchFromGitHub {
    owner = "NixOS";
    repo = "nixpkgs";
    rev = "1fe6ed37fd9beb92afe90671c0c2a662a03463dd";
    sha256 = "1daa0y3p17shn9gibr321vx8vija6bfsb5zd7h4pxdbbwjkfq8n2";
  };
  pkgs = import pinnedPkgs {
    overlays = [(import rustOverlay)];
  };
in
  pkgs.mkShell {
    buildInputs = with pkgs; [
      rust-bin.stable.latest.default
      rust-analyzer
    ];

    RUST_BACKTRACE = 1;
  }

```

I blogged about a more classic approach too [here](https://gutier.io/post/development-using-rust-with-nix/) if anyone is interested.

---

<div class="post-metadata">

### Author: ![savannidgerinel](https://discourse.nixos.org/user_avatar/discourse.nixos.org/savannidgerinel/32/1119_2.png) [@savannidgerinel](https://discourse.nixos.org/u/savannidgerinel)
#### Post date: [September 9, 2021, 1:26pm UTC](https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501/9 "2021-09-09T13:26:33Z")

</div>

I’ve been using the Oxalica overlay for my shell for a while.

```auto
let
    rust_overlay = import (builtins.fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz");
    cargo2nix = import (builtins.fetchTarball "https://github.com/cargo2nix/cargo2nix/tarball/master") {};
    pkgs = import <pkgs-21.05> { overlays = [rust_overlay]; };
    unstable = import <unstable> {};
    rust = pkgs.rust-bin.stable."1.54.0".default.override {
      extensions = ["rust-src"];
    };

in pkgs.mkShell {
    name = "luminescent-dreams-core";

    buildInputs = [
      pkgs.exiftool
      pkgs.file
      pkgs.pkgconfig
      pkgs.glib
      pkgs.gtk3
      pkgs.wrapGAppsHook
      rust
      cargo2nix.package
      unstable.rust-analyzer
    ];

    shellHook = ''
      if [-e ~/.nixpkgs/shellhook.sh]; then . ~/.nixpkgs/shellhook.sh; fi

      export MAGIC_DB=${pkgs.file}/share/misc/magic.mgc;
    '';
}

```

I was particularly happy when I discovered the `extensions = ["rust-src"]` statement, which finally got rust-analyzer working reliably.

So, any of you know how to use Oxalica to specify the Rust and Cargo packages for `buildRustPackage`? I know that as part of a nixpkgs package, I would just use the default that nixpkgs uses, but my work is internal and I really need to specify a known version in order to stay with my co-workers.

---

<div class="post-metadata">

### Author: ![efx](https://discourse.nixos.org/user_avatar/discourse.nixos.org/efx/32/7925_2.png) [@efx](https://discourse.nixos.org/u/efx)
#### Post date: [October 11, 2021, 1:20pm UTC](https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501/10 "2021-10-11T13:20:56Z")

</div>

> [@savannidgerinel](#):
>
> So, any of you know how to use Oxalica to specify the Rust and Cargo packages for `buildRustPackage` ?

Great question. I do not know off the top of my head.  
@savannidgerinel would you be up for posting this as a separate topic? I’m curious if anyone has done this. It also seems like a good question to have for reference on this discourse.

---

<div class="post-metadata">

### Author: ![ilkecan](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/i/3be4f8/32.png) [@ilkecan](https://discourse.nixos.org/u/ilkecan)
#### Post date: [October 11, 2021, 1:29pm UTC](https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501/11 "2021-10-11T13:29:48Z")

</div>

> [@savannidgerinel](#):
>
> So, any of you know how to use Oxalica to specify the Rust and Cargo packages for `buildRustPackage` ?

From memory, something like this should work:

```auto
let
  rust = pkgs.rust-bin.stable."1.54.0".default.override {
    extensions = ["rust-src"];
  };
  rustPlatform = pkgs.makeRustPlatform {
    cargo = rust;
    rustc = rust;
  };
in
rustPlatform.buildRustPackage {
  ...
}

```

---

<div class="post-metadata">

### Author: ![milahu](https://discourse.nixos.org/user_avatar/discourse.nixos.org/milahu/32/4006_2.png) [@milahu](https://discourse.nixos.org/u/milahu)
#### Post date: [October 4, 2022, 7:57pm UTC](https://discourse.nixos.org/t/how-can-i-set-up-my-rust-programming-environment/4501/12 "2022-10-04T19:57:22Z")

</div>

see also the nixpkgs manual: [Controlling Rust Version Inside `nix-shell`](https://ryantm.github.io/nixpkgs/languages-frameworks/rust/#controlling-rust-version-inside-nix-shell)
