Why use a shell.nix File?
Using a shell.nix
file will allow you to setup a development environment that is self-contained and does not pollute your regular environment. When you run nix-shell
and you have the shell.nix
file in the same folder, it will automatically get your development tools installed for you. You can also interact with this same environment via an extension in VS Code, without needing to install the development tools in your regular environment.
Create a shell.nix File
Depending on what languages you are working on will impact what your shell.nix
file looks like. It could be something as simple as this if you just need ruby:
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
nativeBuildInputs = [ pkgs.buildPackages.ruby_2_7 ];
}
For Elixir or Erlang development, it may look like this:
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
# nativeBuildInputs is usually what you want -- tools you need to run
nativeBuildInputs = [
pkgs.buildPackages.elixir
pkgs.buildPackages.nodejs
pkgs.buildPackages.erlang
pkgs.buildPackages.rebar3
];
}
Install the Extension in Visual Studio Code
Install the Nix Environment Selector extension in Visual Studio Code and select shell.nix
when you open your workspace/folder. If you need access to the same files in the integrated terminal, you just have to run nix-shell in the terminal in the folder that has the shell.nix
file.
Other Things to Note
You could also launch nix-shell
in command line and then run code .
to launch it in the same environment without needing to install the extension at all.
Useful References
Nix Environment Selector Visual Studio Code Extension
Exploring Nix & Haskell Part 2: Dev Tools & IDE Integration, has some advanced discussion of targetting older channels in NixOS.