Visual Studio Code: Difference between revisions
imported>SuperSamus No edit summary |
m Category:IDE added |
||
(26 intermediate revisions by 15 users not shown) | |||
Line 17: | Line 17: | ||
<syntaxHighlight lang=nix> | <syntaxHighlight lang=nix> | ||
environment.systemPackages = with pkgs; [ | environment.systemPackages = with pkgs; [ | ||
vscode-with-extensions.override { | (vscode-with-extensions.override { | ||
vscodeExtensions = with vscode-extensions; [ | vscodeExtensions = with vscode-extensions; [ | ||
bbenoist.nix | bbenoist.nix | ||
Line 31: | Line 31: | ||
} | } | ||
]; | ]; | ||
} | }) | ||
]; | ]; | ||
</syntaxHighlight> | </syntaxHighlight> | ||
Some examples here: [https://github.com/search?q=extensionFromVscodeMarketplace&type=code GitHub search for "extensionFromVscodeMarketplace"] | Some examples here: [https://github.com/search?q=extensionFromVscodeMarketplace&type=code GitHub search for "extensionFromVscodeMarketplace"] | ||
Note: For fetching the sha256 string you can use the following command. Make sure to replace the author, the package name and version!<syntaxhighlight lang="bash"> | |||
nix-prefetch-url https://marketplace.visualstudio.com/_apis/public/gallery/publishers/ms-vscode-remote/vsextensions/remote-ssh-edit/0.47.2/vspackage | |||
</syntaxhighlight>{{ic|extensionsFromVscodeMarketplace}} is a manual way to fetch extensions. However, to keep updated from upstream, [https://github.com/nix-community/nix-vscode-extensions nix-community/nix-vscode-extensions] provides the Nix expressions for the majority of available extensions from Open VSX and VSCode Marketplace. A GitHub Action updates the extensions daily. | |||
It's also possible to install VS Code via [[Home Manager]]: | It's also possible to install VS Code via [[Home Manager]]: | ||
Line 50: | Line 54: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
* See for more options: [https://nix-community.github.io/home-manager/options. | * See for more options: [https://nix-community.github.io/home-manager/options.xhtml#opt-programs.vscode.enable Home Manager Manual: Options - programs.vscode] | ||
* Search for extensions with configurations: [https://search.nixos.org/packages?type=packages&query=vscode-extensions NixOS Search: vscode-extensions] | * Search for extensions with configurations: [https://search.nixos.org/packages?type=packages&query=vscode-extensions NixOS Search: vscode-extensions] | ||
Line 80: | Line 84: | ||
Adding extension-specific dependencies, these will be added to the FHS environment: | Adding extension-specific dependencies, these will be added to the FHS environment: | ||
<syntaxHighlight lang=nix> | <syntaxHighlight lang=nix> | ||
# needed for rust lang server extension | # needed for rust lang server and rust-analyzer extension | ||
programs.vscode.package = pkgs.vscode.fhsWithPackages (ps: with ps; [ rustup zlib ]); | programs.vscode.package = pkgs.vscode.fhsWithPackages (ps: with ps; [ rustup zlib openssl.dev pkg-config ]); | ||
</syntaxHighlight> | </syntaxHighlight> | ||
Line 93: | Line 97: | ||
(pkgs.vscode.override { isInsiders = true; }).overrideAttrs (oldAttrs: rec { | (pkgs.vscode.override { isInsiders = true; }).overrideAttrs (oldAttrs: rec { | ||
src = (builtins.fetchTarball { | src = (builtins.fetchTarball { | ||
url = "https:// | url = "https://code.visualstudio.com/sha/download?build=insider&os=linux-x64"; | ||
sha256 = " | sha256 = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; | ||
}); | }); | ||
version = "latest"; | version = "latest"; | ||
buildInputs = oldAttrs.buildInputs ++ [ pkgs.krb5 ]; | |||
}); | }); | ||
</syntaxHighlight> | </syntaxHighlight> | ||
You will need to update the <code>sha256</code> value for each new Insiders build. The new value will appear in a validation error when you try to build. | |||
==== Updating insiders placeholder <code>sha256</code>: ==== | |||
You will need to update the placeholder <code>sha256</code> value for each new Insiders build. | |||
The new value will appear in a validation error when you try to build. | |||
Put an arbitrary placeholder value in the <code>sha256</code> field, try to build and you'll get an error message regarding the sha256 value. | |||
===== If insiders error contains <code>sha256:</code>, follow these instructions: ===== | |||
<pre> | |||
//-- ... | |||
error: hash mismatch in file downloaded from 'https://code.visualstudio.com/sha/download?build=insider&os=linux-x64': | |||
specified: sha256:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |||
got: sha256:16fzxqs6ql4p2apq9aw7l10h4ag1r7jwlfvknk5rd2zmkscwhn6z | |||
//-- ... | |||
</pre> | |||
Take that last line and input it where your placeholder was, 'sha256:' in the beginning should be removed. | |||
===== If insiders error contains <code>sha256-</code>, follow these instructions: ===== | |||
<pre> | |||
//-- ... | |||
error: hash mismatch in fixed-output derivation '/nix/store/path': | |||
specified: sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= | |||
got: sha256-aQvTtZdPU2F1UjkFxiLs4A+60A4qc9bXKwKriNsCDPg= | |||
//-- ... | |||
</pre> | |||
Take that last line and run the following python script (you can search for an online python interpreter if it's not installed), the output will give you the correct value: | |||
<syntaxHighlight lang=python> | |||
import base64 | |||
# The 'sha256-' in the beginning should be removed | |||
text = b'aQvTtZdPU2F1UjkFxiLs4A+60A4qc9bXKwKriNsCDPg=' | |||
print(base64.decodebytes(text).hex()) | |||
</syntaxHighlight> | |||
Take the output from this command and input it where your placeholder was. | |||
== Creating development environments using nix-shell == | |||
Instead of using configuration.nix to add packages (e.g. Python or NodeJS) for developing code on VSCode, you can instead use nix-shell. This will allow you to seamlessly create development environments with the correct packages for your project, without rebuilding and restarting NixOS. See [[Development_environment_with_nix-shell | this page]] for further instructions in building nix-shell development environments. | |||
=== Automatically switch nix shells when switching projects === | |||
You can do this by using [https://github.com/nix-community/nix-direnv nix-direnv] and [https://marketplace.visualstudio.com/items?itemName=mkhl.direnv the VSCode extension direnv] for integration. View the nix-direnv github page linked for a guide on setting it up. | |||
=== Alternative for manually switching shells === | |||
The extension [https://marketplace.visualstudio.com/items?itemName=arrterian.nix-env-selector nix-env-selector] will make switching between different nix-shell environments within VSCode so you can switch between different coding projects easily and manually. It has a guide for setting up nix-shell environments for VSCode. | |||
== Wayland == | == Wayland == | ||
Line 145: | Line 202: | ||
=== Any client to NixOS host === | === Any client to NixOS host === | ||
'''tl;dr Use [https://github.com/msteen/nixos-vscode-server nix-vscode-server] on host machines.''' | '''tl;dr Use [https://github.com/msteen/nixos-vscode-server nix-vscode-server] or [https://github.com/Mic92/nix-ld nix-ld] on host machines.''' | ||
==== nix-vscode-server ==== | |||
Note that nix-vscode-server works as of 8/21/21 but is occasionally broken (See https://github.com/msteen/nixos-vscode-server/pull/3, https://github.com/msteen/nixos-vscode-server/pull/4, https://github.com/msteen/nixos-vscode-server/pull/5). Here's a workaround: Install the <code>nodejs-16_x</code> package on the NixOS host, and then run the following nix-shell script: | Note that nix-vscode-server works as of 8/21/21 but is occasionally broken (See https://github.com/msteen/nixos-vscode-server/pull/3, https://github.com/msteen/nixos-vscode-server/pull/4, https://github.com/msteen/nixos-vscode-server/pull/5). Here's a workaround: Install the <code>nodejs-16_x</code> package on the NixOS host, and then run the following nix-shell script: | ||
Line 170: | Line 229: | ||
If instead you'd prefer to fix the binaries manually and have to do so every time that you upgrade your VS Code version, then you can install the <code>nodejs-16_x</code> package on the NixOS host and replace the VS Code provided version. This workaround is described here: https://github.com/microsoft/vscode-remote-release/issues/648#issuecomment-503148523. Note that NodeJS needs to be updated according to VS Code upstream requirements (NodeJS 16 required from 4/2022). | If instead you'd prefer to fix the binaries manually and have to do so every time that you upgrade your VS Code version, then you can install the <code>nodejs-16_x</code> package on the NixOS host and replace the VS Code provided version. This workaround is described here: https://github.com/microsoft/vscode-remote-release/issues/648#issuecomment-503148523. Note that NodeJS needs to be updated according to VS Code upstream requirements (NodeJS 16 required from 4/2022). | ||
==== nix-ld ==== | |||
Add the following settings to <code>configuration.nix</code> on the NixOS host | |||
{{file|/etc/nixos/configuration.nix|nix|<nowiki> | |||
programs.nix-ld.enable = true; | |||
</nowiki>}} | |||
Then run <code>nixos-rebuild switch</code> to enable <code>nix-ld</code>. Unlike the <code>nix-vscode-server</code> solution, the <code>nix-ld</code> solution also enables VSCode extensions even if they include non-Nix binaries. | |||
=== Nix-sourced VS Code to NixOS host === | === Nix-sourced VS Code to NixOS host === | ||
Line 175: | Line 244: | ||
If vscode-remote is installed from nix (vscode-extensions.ms-vscode-remote as above) on the client machine, everything should "just work". | If vscode-remote is installed from nix (vscode-extensions.ms-vscode-remote as above) on the client machine, everything should "just work". | ||
== | == Remote WSL == | ||
Similar to SSH hosts, both <code>nix-vscode-server</code> and <code>nix-ld</code> solution allows a VSCode Windows client to connect a [https://github.com/nix-community/NixOS-WSL NixOS-WSL] host. However, by default the VSCode Windows client uses <code>wsl.exe --exec</code> to start the code server, which bypasses NixOS environment variables required by <code>nix-ld</code>, resulting in failures. | |||
As a workaround, search for the following text in all files under the directory <code>$HOME\.vscode\extensions\</code> | |||
{{file|wslDaemon.js|js|<nowiki> | |||
.push("sh","-c" | |||
</nowiki>}} | |||
Replace it with | |||
{{file|wslDaemon.js|js|<nowiki> | |||
.push("sh","-l","-c" | |||
</nowiki>}} | |||
Then restart VS Code and your VS Code client should be able to connect to NixOS host | |||
See https://github.com/nix-community/NixOS-WSL/issues/222 for the discussion about <code>wsl --exec</code> issue on NixOS-WSL. | |||
See https://github.com/microsoft/vscode-remote-release/issues/8305#issuecomment-1661396267 about the workaround. | |||
== Troubleshooting == | == Troubleshooting == | ||
Line 198: | Line 287: | ||
Don't forget to perform <code>nixos-rebuild switch</code> and reboot the system. | Don't forget to perform <code>nixos-rebuild switch</code> and reboot the system. | ||
=== Server did not start successfully === | |||
<blockquote> | |||
Server did not start successfully. Full server log at /home/user/.vscode-server/.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.log<br> | |||
<br> | |||
server log:<br> | |||
/home/user/.vscode-server/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/bin/code-server: line 12: /home/user/.vscode-server/bin/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/node: No such file or directory | |||
</blockquote> | |||
"No such file or directory" means that libc is not found, see | |||
<pre> | |||
ldd ~/.vscode-server/bin/*/node | |||
</pre> | |||
try to run the node binary on the server | |||
<pre> | |||
~/.vscode-server/bin/*/node | |||
</pre> | |||
if this fails, install node version 16, and try to patch the node binary | |||
<pre> | |||
nix-env -iA nixos.nodejs-16_x | |||
</pre> | |||
<syntaxHighlight lang=bash> | |||
#! /bin/sh | |||
# fix-vscode-server-node.sh | |||
# https://github.com/microsoft/vscode-remote-release/issues/648#issuecomment-503148523 | |||
cd ~/.vscode-server/bin/* | |||
if ! ./node -e "require('process').exit(0)" | |||
then | |||
echo patching node binary $(readlink -f node) | |||
rm node | |||
ln -s $(which node) | |||
else | |||
echo node is working $(readlink -f node) | |||
fi | |||
</syntaxHighlight> | |||
[[Category:Applications]] | |||
[[Category:IDE]] |