This content originally appeared on Level Up Coding - Medium and was authored by vvs
A simple guide to getting started Nix and Rust for learning setup with the Nix shell on macOS
Introduction
So, I use multiple adjectives to define my professional version, and one of the earliest titles is Software Programmer. Over the years I have done various roles, but the programmer in me wakes up ever so often intending to learn something new and interesting.
This morning I read an article from another software professional about the Rust language. I have known about Rust language for years but never could bring myself to invest time in learning it. The article in question itself is opinionated, but it triggered in me the interest to take learning Rust seriously.
Rust Has A Dark Side. Why Is It Hated So Much?
No! Not one more Development Environment
Every programmer out there who has learned more than one programming at a home or work computer deals with the mess of having multiple different software and sometimes multiple versions of the same software. Are there any Python developers using Mac out there? You know what I mean.
Learning a new programming language means experimenting and possibly installing a plethora of different packages. If one is lucky enough to use this language for professional or personal projects then great. However, there is a good likelihood that one will forget about it in a few weeks and then the installed software just lies there waiting for the opportune moment to cause dependency conflicts.
Ideally, I would like to clean up my learning environment after I am done and start fresh when I have real work to do. My usual solution is to use Virtual Machines using something like Virtual Box.
VirtualBox - Oracle VM VirtualBox
Want to hear more about Virtual Box? Do offer your thoughts by clapping and commenting.
Virtual Box in itself is an interesting topic and perhaps best left for another discussion. For now suffice to say, that it is a somewhat overly complicated solution for quick and ephemeral development environments.
Enter Nix
Someone had mentioned Nix in an online community of Techie Founders that I hang out with. They also shared this link about Nix and it was indeed very interesting. I have added the links at the end in References.
Nix is many things but the main thing that got me interested then was the fact that it had a “package manager” that allowed me to create ephemeral development environments without messing with the overall configuration of my system.
Now that I am interested in learning a new language, I wanted to make it simple for me to delete that environment if say I want to clean up my learning environment later. Nix seems like the perfect tool for this.
Like my writing? consider subscribing to my blog by email or following me.
Let's get Rusty with Nix
My aim here is to clear the “Getting Started” chapter of the “The Rust Programming Language” book. So here are the steps in creating a simple Rust environment using Nix
- Install Nix
- Set up a Shell environment
- Install essential packages
- Test the new environment
- Confirm that my System OS is unaffected.
Install Nix
As the download page of Nix suggests it is as simple as running the below command on a Mac. For other OS this command may be a little different. Consult the manual for details.
$ sh <(curl -L https://nixos.org/nix/install)
Of note here is the fact that part of the installation requires “root” user access and will ask for sudo access. However it clearly states what command(s) it is about to fire so if you are nervous, consult the documentation before offering the permission to run the command.
Setup a shell environment
Once nix is installed one can easily enter the nix-shell using the following command.
$ nix-shell -p
Install essential packages
The above shell is great but that does not do anything interesting. I can start a shell with rustc and rustup pre-installed using the below command.
$ nix-shell -p rustc rustup
However, this is too long a command to remember. If I had to add a few more Rust or related packages it would become more complicated. So instead it is better to go with a saved environment configuration.
So I now create a simple shell environment configuration in a file called rust.nix. I can call this file anything else and it will still work so long as it conforms to the nix syntax.
{
pkgs ? import <nixpkgs> { },
}:
pkgs.mkShellNoCC {
packages = with pkgs; [
rustc
rustup
];
}
I won’t dive too much into the details of this file. It is sufficient to say that this allows me to declaratively list the packages that I want to be installed by default in my environment. In this case, those are rustc and rustup.
Test the new environment
Now that I have a good environment configuration time to test it. I go to the directory where I had put rust.nix and run the below command.
$ nix-shell rust.nix
If this is the first install Nix will download and install a bunch of dependencies and packages and this may take a while. After everything though you should be inside the nix-shell. Time to check if it worked. Run a few more commands
$ rustc --version
rustc 1.79.0 (129f3b996 2024-06-10) (built from a source tarball)
$ cargo --version
error: rustup could not choose a version of cargo to run, because one wasn't specified explicitly, and no default is configured.
help: run 'rustup default stable' to download the latest stable release of Rust and set it as your default toolchain.
Looks like the cargo package manager wasn’t installed by default using this environment file. However, the error message also provides a hint. So I ran that command within the nix shell.
$ rustup default stable
This downloads a bunch of other important Rust language tools including cargo. Then I check the cargo version again.
$ cargo - version
cargo 1.80.0 (376290515 2024–07–16)
This time it is present and we are ready. Only one more step left to check.
Confirm that my System OS is unaffected
It wouldn’t exactly be unchanged since I did install nix-shell and its dependencies. nix-shell in turn did some more package installations but I don’t expect them to be installed and available on my main OS when I am not inside my nix-shell.
Let's check this. So I open a new Mac terminal and run these commands.
$ rustc --version
-bash: rustc: command not found
$ cargo --version
-bash: cargo: command not found
$ rustup
-bash: rustup: command not found
Great. My macOS does not recognize any of the 3 commands that my Rust installation has made available via the rust.nix configuration file. So when I am done and I want to get rid of the environment, I can run the appropriate Nix shell commands for clean up and get rid of it easily.
Conclusion
We have examined how we can set up a simple Nix shell configuration that allows us to create a basic ephemeral environment to learn the Rust programming language. Time to get Rusty.
Get an email whenever vvs publishes.
A humble request. If you like my writing and want to keep reading more about Software, Technology and AI please consider subscribing to me by email or following me.
I also offer services as a freelance Product / Technical Mentor and, If you are looking to develop something for your business you can book a consultation with me.
References
- Nix & NixOS | Declarative builds and deployments
- Declarative shell environments with shell.nix
- The Rust Programming Language
More interesting Mac / Linux articles from me.
- How to Dual Boot Mac And Linux
- How to remove the “to get future Google Chrome updates” warning
- Journey of my Tux
Getting started with Nix and Rust on a Mac was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by vvs
vvs | Sciencx (2024-08-04T17:28:54+00:00) Getting started with Nix and Rust on a Mac. Retrieved from https://www.scien.cx/2024/08/04/getting-started-with-nix-and-rust-on-a-mac/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.