2022-11-05

rust development environment setup with neovim

What I want:

For Flatpak users (Linux only) To gain the full benefit of customization, it's highly recommend to install either text editor via the system's native package manager supported instead of via flatpak.

install rust toolchains

Before installing Rust toolchain, check with the current operating system to see if gcc package is installed. If not, it's recommended to install development tools:

Arch

There is no prerequisites for Arch system, since all prerequisite packages for Rust toolchain come preinstalled.

Debian / Ubuntu

sudo apt install build-essential

Fedora

dnf offers similar packages comparable to build-essential in a slightly different format. The most convenient way is to install the "Development Tools" as a package collective:

sudo dnf groupinstall "Development Tools"

Please visit the official website of Rust lang to install all the necessary default toolchain components.

Once installed, double check via:

rustup component list --installed

to confirm if the following additional components are installed.

If not, you can install them via:

rustup component add <toolchain_component>

neovim

nvim
    - .config/nvim configs
        - plugin manager
            - mason.nvim
            - lsconfig
            - nvim-dap
            - nvim-dap-ui
            - plenary
    - .config/nvim/lua/user
        - astrocommunity.pack.rust

clipboard configurations

On Linux, depending on the display server protocol installed on system, either X11 or Wayland. Neovim or Vim may not have access to the system's clipboard. That means, if not configured, copying and pasting between system and Neovim or Vim is inaccessible.

Use native package manager to install:

wayland clipboard

For Debian / Ubuntu:

sudo apt install wl-clipboard

For Fedora:

sudo dnf install wl-clipboard

For Arch:

sudo pacman -S wl-clipboard

x11 clipboard

For Debian / Ubuntu:

sudo apt install xclip

For Fedora:

sudo dnf install xclip

For Arch:

sudo pacman -Syu xclip

Once installed, check to see if Neovim or Vim has access to system's clipboard by running the following command inside of a Neovim:

:checkhealth

Since there is no :checkhealth command support from Vim, simply perform copying & pasting should surfice as a good-enough check.

There should be a green checkmark appeared on the corresponding health status.

nerd font

Many of Neovim's plugins requires "Nerd font" for displaying icons to represent file types or emoji on its UI. If current operating system doesn't have any types of "Nerd font" installed. Visit official nerd-fonts github release page to download and install one.

On Linux, once installed, configure Terminal app to use the newly installed Nerd font and then restart the Terminal app itself for it to take into effect. Many Linux distros nowadays come with Nerd font by default, so some Linux users may skip this step.

On macOS, operating system may need to be restarted first, and then configure iTerm2 to use the Nerd font. And then restart iTerm2 to make it take into effect.

For example, to install terminus nerd font:

on arch:

sudo pacman -S ttf-terminus-nerd

on fedora, ubuntu / debian:

Unfortunately, official repositories of these operating systems do not have nerd fonts available. To install nerd fonts, download it directly from nerdfonts.com.

Alternatively, use the convenient download script below to perform download:

curl -OL https://github.com/ryanoasis/nerd-fonts/releases/latest/download/terminus.tar.xz

Then extract the font files and install them onto system manually.

on macos:

brew tap homebrew/cask-fonts
brew install --cask font-terminess-ttf-nerd-font

editor

on fedora:

sudo dnf install neovim

on ubuntu:

Ubuntu repository contain an older version of Neovim, which is below the compatible version 0.9 of one of the plugins Lazy. To get Neovim work, activate and use the unstable channel of Neovim's Official PPA.

sudo add-apt-repository ppa:neovim-ppa/unstable
sudo apt update && sudo apt install neovim

on arch:

sudo pacman -S neovim

on macos:

macOS's Terminal app lacks support for more than 256 colors, meaning the Terminal app is unable to accurately render themes, icons, and colors for today's development needs.

In order to have a fully functional neovim, it's recommended to use a modern terminal app supported by the open source community, such as iTerm2, Kitty, or Alacritty, etc.

For example:

brew install --cask alacritty
brew install neovim

astronvim

To simplify the installation and maintenance process, use AstroNvim configuration framework for quickly setting everything up in 1 command:

To install AstroNvim:

git clone --depth 1 https://github.com/AstroNvim/AstroNvim.git ~/.config/nvim

configurations

After finished cloning AstroNvim, it's time to setup the file directories for user specific configurations.

To begin setting up for configurations that is specific to a personal level. It's highly recommended to use the community template to save time. Here is all the available community plugins from AstronNvim community here. Sometimes, large projects often requires a combination of a few plugins or even across several languages. For the scope of this guide, only the rust language pack is needed.

repository setup

First, clone the template repository below:

git clone https://github.com/AstroNvim/user_example ~/.config/nvim/lua/user

Then delete the .github directories from the cloned example:

rm -rf ~/.config/nvim/lua/user/.github

defining plugins

Next, add the following community plugin in ~/.config/nvim/lua/user/plugins/community.lua:

return {
    "AstroNvim/astrocommunity",
    { import = "astrocommunity.pack.rust" },
}

disabling default behaviors

AstroNvim comes with a lot of default. Depending on personal preferences, some default behavior can be disbled to improve ergonomics. For example, to disable better-escape.nvim, go to ~/.config/nvim/lua/user/core.lua:

return {
    -- ...etc...
    -- You can disable default plugins as follows:
    { "max397574/better-escape.nvim", enabled = false },
    -- ...etc...
}

customizing editor behaviors

Next, try override some of the default values to impose user preferences.

For example, add the following options to provide some visual guides in ~/.config/nvim/lua/user/options.lua:

return {
    opt = {
        -- etc.
        list = true, -- show dash on whilespaces
        colorcolumn = "80", -- show vertical bar at column 80
    },
    g = {
        -- etc.
    }
}

Next, open nvim will trigger lazy to download all the plugins by calling:

nvim

If this is the first time nvim being opened, it will take some time to download all the necessary plugins. Be patient and do not exit while lazy is working on the download process.

Once neovim completes downloading all of its plugins, run:

:checkhealth

debugging

AstroNvim comes with all the tools needed for debugging, such as nvim-dap and nvim-dap-ui. The only other dependency on the operating system side is a debugger, which almost all operating system has come with gdm preinstalled.

Unfortunately macOS is incompliant to GPL license, gdm will need to be installed manually. An alternative is to use Apple's lldb debugger.

More commands can be found by using a fuzzy search via <Leader> fk on specific keywords, such as debug or breakpoint. etc.

optional: maintaining personal configurations

When editor is configured, it's highly recommended to save the configuration folder onto a remote git server so that it becomes available for cloning by other machines / operating systems.

Simply commit the changes in ~/.config/nvim/lua/user, and then push it to remote.

For reference, here is my own user configuration as example:

User configuration

That should be it. Happy coding.