Michael Maclean

Using Nix to install Go tools for VS Code

I use both Nix on macOS and NixOS on different machines, so most of my projects have acquired shell.nix files at their root so that I can have the same set of tools and dependencies available to me regardless of which machine I’m on. I also use VS Code at least some of the time, and the Go extension there requires a number of command line tools for much of its functionality. It’s quite happy to go off and install these itself, but that I’d prefer to maintain those using Nix.

After some experimentation, here’s a skeleton shell.nix that provides everything VS Code’s Go extension looks for:

{ pkgs ? import <nixpkgs> { } }:

with pkgs;

mkShell {
  buildInputs = [
    go
    gotools
    gopls
    go-outline
    gocode
    gopkgs
    gocode-gomod
    godef
    golint
  ];
}

I find it works best if I switch to the project directory, enter nix-shell (or use direnv if you prefer), then launch Code using code ..

I have tried the nix-env-selector extension in the past, but I’ve found that not every VS Code language extension plays well with that, and previously Go was one of those which didn’t. I will give it another shot later, but for now this works for me.