How to set up rustlings in Nixos?
How do you set up rustlings in Nixos?

NixOS + Rust

How to set up rustlings in NixOS?

Learn rust language with rustlings in NixOS.

--

The Rustlings project contains a list of small exercises that help you get used to reading and writing Rust code, including reading and responding to compiler messages.

In short, Rustlings gives a learning path for the Rust language.

Sometimes, developers need clarification about how to run Rustlings in nixos.

In a small tutorial, we learn how to install rustlings and how to run them in nixos. You do not need to install any tools, such as gcc in nixos. You need to follow the steps I have described.

Steps

  1. First clone Rustlings repository
  2. Build Rustlings project in NixOS.
    Config the LSP for rustlings
    Run Watch mode in Rustlings.
  3. Conclusion

First clone Rustlings repository

The first step is to clone the Rustlings repository locally using the git command.

# clone repository
git clone -b 5.6.1 --depth 1 https://github.com/rust-lang/rustlings

# change directory or folder
cd rustlings

The command output looks like this.

➜  tutorial git clone -b 5.6.1 --depth 1 https://github.com/rust-lang/rustlings

Cloning into 'rustlings'...
remote: Enumerating objects: 201, done.
remote: Counting objects: 100% (201/201), done.
remote: Compressing objects: 100% (186/186), done.
remote: Total 201 (delta 7), reused 196 (delta 7), pack-reused 0
Receiving objects: 100% (201/201), 139.97 KiB | 329.00 KiB/s, done.
Resolving deltas: 100% (7/7), done.
Note: switching to '9a743f80c57cc6bf27819589a8ddb5a5579ab1a4'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

git switch -c <new-branch-name>

Or undo this operation with:

git switch -

Turn off this advice by setting config variable advice.detachedHead to false

➜ tutorial cd rustlings
➜ rustlings git:(5.6.1)

Build Rustlings project in NixOS.

To build the Rustlings project in nixos, check your Nix language version using the following command.

➜  rustlings git:(5.6.1) nix --version 
nix (Nix) 2.18.2

If your Nix version > 2.3, run the nix develop command in your Rustlings project. Your nix version < = 2.3; run the nix-shell command in your Rustlings project.

The Nix language version is bigger than the 2.3 version on my laptop, so I use the nix develop command in the Rustlings project folder.

➜  rustlings git:(5.6.1) nix develop  

[officialrajdeepsingh@nixos:~/tutorial/rustlings]$

The nix develop command builds an interactive development environment for you.

The nix develop and nix-shellcommand takes some time, depending on your internet connection.

Now, you run the rustlings command.

[officialrajdeepsingh@nixos:~/tutorial/rustlings]$ rustlings --help

Rustlings is a collection of small exercises to get you used to writing and reading Rust code

Usage: rustlings [OPTIONS] [COMMAND]

Commands:
verify Verify all exercises according to the recommended order
watch Rerun `verify` when files were edited
run Run/Test a single exercise
reset Reset a single exercise using "git stash -- <filename>"
hint Return a hint for the given exercise
list List the exercises available in Rustlings
lsp Enable rust-analyzer for exercises
help Print this message or the help of the given subcommand(s)

Options:
--nocapture Show outputs from the test exercises
-h, --help Print help
-V, --version Print version

[officialrajdeepsingh@nixos:~/tutorial/rustlings]$

If you face this type of error in the Rustlings project.

❯ nix develop
error: experimental Nix feature 'nix-command' is disabled; use '--extra-experimental-features nix-command' to override

To fix this error, copy the following code and paste it into a configuration.nix file.

# configuration.nix

# Enable experimental-features in nixos
nix.settings.experimental-features = [ "nix-command" "flakes" ];

Rebuild your nixos with the nixos-rebuild command.

Config the LSP for rustlings

To configure the LSP for Rustlings, run the following command.

rustlings lsp

The command output looks like this.

[officialrajdeepsingh@nixos:~/tutorial/rustlings]$ rustlings lsp
Successfully generated rust-project.json
rust-analyzer will now parse exercises, restart your language server or editor

Now restart your IDE server or editor (IDE).

Run Watch mode in Rustlings.

To run watch mode in Rustlings, use the following command.

rustlings watch

How do you exit the interactive development environment?

Just type the exit command in the terminal to exit the interactive development environment.

[officialrajdeepsingh@nixos:~/tutorial/rustlings]$ exit
exit
➜ rustlings git:(5.6.1)

How to rerun the rustlings project?

Simple first rerun your interactive development environment using nix develop and nix-shell command.

I used nix develop command to build the environment and then rerun rustlings CLI.

➜  rustlings git:(5.6.1) nix develop          

[officialrajdeepsingh@nixos:~/tutorial/rustlings]$ rustlings watch

Your rustlings CLI is starting work now, and you can use it.

Conclusion

Rustlings are the most important project for learning rust with exercises. You can now easily set up your rustlings in nixos.

To learn more about Nixos and Linux stuff, follow the Linux publication on Medium and other updates. Follow me on Twitter (X) and Medium.

--

--

Rajdeep Singh
The Linux

JavaScript | TypeScript | Reactjs | Nextjs | Rust | Biotechnology | Bioinformatic | Frontend Developer | Author | https://linktr.ee/officialrajdeepsingh