Skip to content

Instantly share code, notes, and snippets.

@3noch
Created June 2, 2023 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3noch/cf6967e94a7227272bfb5921bcc51f36 to your computer and use it in GitHub Desktop.
Save 3noch/cf6967e94a7227272bfb5921bcc51f36 to your computer and use it in GitHub Desktop.
nix-shell VSCode Golang
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell rec {
packages = with pkgs; [ delve go gopls go-outline go-tools libkrb5.dev ];
WORKSPACE_ROOT = toString ./.;
# VSCode's Go plugin doesn't play well with nix shells, so we create a custom directory for all our tools and tell the plugin to use it.
shellHook = ''
initNixShell() {
echo Initializing Nix shell
mkdir -p "$WORKSPACE_ROOT/.nix"
ln --symbolic --force ${pkgs.symlinkJoin { name = "go-tools"; paths = map (x: "${x}/bin") packages; }}/* "$WORKSPACE_ROOT/.nix"
local settings_file="$WORKSPACE_ROOT/.vscode/settings.json"
if [ -f "$settings_file" ]; then
echo Setting .nix as gopath in VSCode settings
local new_json
new_json=$(${pkgs.jq}/bin/jq '."go.toolsGopath" = $gopath' --arg gopath ''\'''${workspaceRoot}/.nix' < "$settings_file") && echo "$new_json" > "$settings_file"
fi
}
initNixShell
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment