Created
June 2, 2023 20:25
-
-
Save 3noch/cf6967e94a7227272bfb5921bcc51f36 to your computer and use it in GitHub Desktop.
nix-shell VSCode Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ 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