2023-10-19 13:55:26 +00:00
{ stdenv
, lib
2024-09-19 14:19:46 +00:00
, copyDesktopItems
2023-10-19 13:55:26 +00:00
, makeDesktopItem
, unzip
, libsecret
, libXScrnSaver
, libxshmfence
, buildPackages
, at-spi2-atk
, autoPatchelfHook
, alsa-lib
, mesa
, nss
, nspr
, xorg
, systemd
, fontconfig
, libdbusmenu
, glib
, buildFHSEnv
, wayland
, libglvnd
, libkrb5
# Populate passthru.tests
2021-02-13 14:23:35 +00:00
, tests
2023-10-19 13:55:26 +00:00
# needed to fix "Save as Root"
, asar
, bash
# Attributes inherit from specific versions
, version
, src
, meta
, sourceRoot
, commandLineArgs
, executableName
, longName
, shortName
, pname
, updateScript
2022-10-30 15:09:59 +00:00
, dontFixup ? false
2023-10-19 13:55:26 +00:00
, rev ? null
, vscodeServer ? null
2021-07-03 03:11:41 +00:00
, sourceExecutableName ? executableName
2023-05-24 13:37:59 +00:00
, useVSCodeRipgrep ? false
, ripgrep
2020-04-24 23:36:52 +00:00
} :
2023-10-19 13:55:26 +00:00
stdenv . mkDerivation ( finalAttrs :
2020-04-24 23:36:52 +00:00
let
2021-05-03 20:48:10 +00:00
# Vscode and variants allow for users to download and use extensions
# which often include the usage of pre-built binaries.
# This has been an on-going painpoint for many users, as
# a full extension update cycle has to be done through nixpkgs
# in order to create or update extensions.
# See: #83288 #91179 #73810 #41189
#
2023-04-29 16:46:19 +00:00
# buildFHSEnv allows for users to use the existing vscode
2021-05-03 20:48:10 +00:00
# extension tooling without significant pain.
2023-10-19 13:55:26 +00:00
fhs = { additionalPkgs ? pkgs : [ ] }: buildFHSEnv {
2021-05-03 20:48:10 +00:00
# also determines the name of the wrapped command
name = executableName ;
# additional libraries which are commonly needed for extensions
targetPkgs = pkgs : ( with pkgs ; [
# ld-linux-x86-64-linux.so.2 and others
glibc
# dotnet
curl
icu
libunwind
libuuid
2023-01-11 07:51:40 +00:00
lttng-ust
2021-05-03 20:48:10 +00:00
openssl
zlib
# mono
krb5
] ) ++ additionalPkgs pkgs ;
2022-12-28 21:21:41 +00:00
extraBwrapArgs = [
" - - b i n d - t r y / e t c / n i x o s / / e t c / n i x o s / "
] ;
2022-02-10 20:34:41 +00:00
# symlink shared assets, including icons and desktop entries
2021-05-03 20:48:10 +00:00
extraInstallCommands = ''
2023-10-19 13:55:26 +00:00
ln - s " ${ finalAttrs . finalPackage } / s h a r e " " $ o u t / "
2021-05-03 20:48:10 +00:00
'' ;
2023-10-19 13:55:26 +00:00
runScript = " ${ finalAttrs . finalPackage } / b i n / ${ executableName } " ;
2021-05-03 20:48:10 +00:00
# vscode likes to kill the parent so that the
# gui application isn't attached to the terminal session
dieWithParent = false ;
passthru = {
inherit executableName ;
2023-10-19 13:55:26 +00:00
inherit ( finalAttrs . finalPackage ) pname version ; # for home-manager module
2021-05-03 20:48:10 +00:00
} ;
meta = meta // {
description = ''
2023-05-24 13:37:59 +00:00
Wrapped variant of $ { pname } which launches in a FHS compatible environment .
2021-05-03 20:48:10 +00:00
Should allow for easy usage of extensions without nix-specific modifications .
'' ;
} ;
} ;
in
2023-10-19 13:55:26 +00:00
{
inherit pname version src sourceRoot dontFixup ;
passthru = {
inherit executableName longName tests updateScript ;
fhs = fhs { } ;
fhsWithPackages = f : fhs { additionalPkgs = f ; } ;
} // lib . optionalAttrs ( vscodeServer != null ) {
inherit rev vscodeServer ;
} ;
2024-09-19 14:19:46 +00:00
desktopItems = [
( makeDesktopItem {
name = executableName ;
desktopName = longName ;
comment = " C o d e E d i t i n g . R e d e f i n e d . " ;
genericName = " T e x t E d i t o r " ;
exec = " ${ executableName } % F " ;
2023-10-19 13:55:26 +00:00
icon = " v s ${ executableName } " ;
2024-09-19 14:19:46 +00:00
startupNotify = true ;
startupWMClass = shortName ;
categories = [ " U t i l i t y " " T e x t E d i t o r " " D e v e l o p m e n t " " I D E " ] ;
keywords = [ " v s c o d e " ] ;
actions . new-empty-window = {
name = " N e w E m p t y W i n d o w " ;
exec = " ${ executableName } - - n e w - w i n d o w % F " ;
icon = " v s ${ executableName } " ;
} ;
} )
( makeDesktopItem {
name = executableName + " - u r l - h a n d l e r " ;
desktopName = longName + " - U R L H a n d l e r " ;
comment = " C o d e E d i t i n g . R e d e f i n e d . " ;
genericName = " T e x t E d i t o r " ;
exec = executableName + " - - o p e n - u r l % U " ;
icon = " v s ${ executableName } " ;
startupNotify = true ;
categories = [ " U t i l i t y " " T e x t E d i t o r " " D e v e l o p m e n t " " I D E " ] ;
mimeTypes = [ " x - s c h e m e - h a n d l e r / v s ${ executableName } " ] ;
keywords = [ " v s c o d e " ] ;
noDisplay = true ;
} )
] ;
2023-10-19 13:55:26 +00:00
buildInputs = [ libsecret libXScrnSaver libxshmfence ]
2024-09-26 11:04:55 +00:00
++ lib . optionals ( ! stdenv . hostPlatform . isDarwin ) [ alsa-lib at-spi2-atk libkrb5 mesa nss nspr systemd xorg . libxkbfile ] ;
2023-10-19 13:55:26 +00:00
2024-09-26 11:04:55 +00:00
runtimeDependencies = lib . optionals stdenv . hostPlatform . isLinux [ ( lib . getLib systemd ) fontconfig . lib libdbusmenu wayland libsecret ] ;
2023-10-19 13:55:26 +00:00
nativeBuildInputs = [ unzip ]
2024-09-26 11:04:55 +00:00
++ lib . optionals stdenv . hostPlatform . isLinux [
2023-10-19 13:55:26 +00:00
autoPatchelfHook
asar
2024-09-19 14:19:46 +00:00
copyDesktopItems
2023-10-19 13:55:26 +00:00
# override doesn't preserve splicing https://github.com/NixOS/nixpkgs/issues/132651
2024-10-04 16:56:33 +00:00
# Has to use `makeShellWrapper` from `buildPackages` even though `makeShellWrapper` from the inputs is spliced because `propagatedBuildInputs` would pick the wrong one because of a different offset.
( buildPackages . wrapGAppsHook3 . override { makeWrapper = buildPackages . makeShellWrapper ; } )
2023-10-19 13:55:26 +00:00
] ;
dontBuild = true ;
dontConfigure = true ;
noDumpEnvVars = true ;
installPhase = ''
runHook preInstall
2024-09-26 11:04:55 +00:00
'' + ( i f s t d e n v . h o s t P l a t f o r m . i s D a r w i n t h e n ''
2023-10-19 13:55:26 +00:00
mkdir - p " $ o u t / A p p l i c a t i o n s / ${ longName } . a p p " " $ o u t / b i n "
cp - r . /* " $ o u t / A p p l i c a t i o n s / $ { l o n g N a m e } . a p p "
ln - s " $ o u t / A p p l i c a t i o n s / ${ longName } . a p p / C o n t e n t s / R e s o u r c e s / a p p / b i n / ${ sourceExecutableName } " " $ o u t / b i n / ${ executableName } "
'' e l s e ''
mkdir - p " $ o u t / l i b / v s c o d e " " $ o u t / b i n "
cp - r . /* " $ o u t / l i b / v s c o d e "
ln - s " $ o u t / l i b / v s c o d e / b i n / ${ sourceExecutableName } " " $ o u t / b i n / ${ executableName } "
# These are named vscode.png, vscode-insiders.png, etc to match the name in upstream *.deb packages.
mkdir - p " $ o u t / s h a r e / p i x m a p s "
cp " $ o u t / l i b / v s c o d e / r e s o u r c e s / a p p / r e s o u r c e s / l i n u x / c o d e . p n g " " $ o u t / s h a r e / p i x m a p s / v s ${ executableName } . p n g "
# Override the previously determined VSCODE_PATH with the one we know to be correct
sed - i " / E L E C T R O N = / i V S C O D E _ P A T H = ' $ o u t / l i b / v s c o d e ' " " $ o u t / b i n / ${ executableName } "
grep - q " V S C O D E _ P A T H = ' $ o u t / l i b / v s c o d e ' " " $ o u t / b i n / ${ executableName } " # check if sed succeeded
# Remove native encryption code, as it derives the key from the executable path which does not work for us.
# The credentials should be stored in a secure keychain already, so the benefit of this is questionable
# in the first place.
rm - rf $ out/lib/vscode/resources/app/node_modules/vscode-encrypt
'' ) + ''
runHook postInstall
'' ;
preFixup = ''
gappsWrapperArgs + = (
2024-09-19 14:19:46 +00:00
$ { # we cannot use runtimeDependencies otherwise libdbusmenu do not work on kde
2024-09-26 11:04:55 +00:00
lib . optionalString stdenv . hostPlatform . isLinux
2024-09-19 14:19:46 +00:00
" - - p r e f i x L D _ L I B R A R Y _ P A T H : ${ lib . makeLibraryPath [ libdbusmenu ] } " }
2023-10-19 13:55:26 +00:00
# Add gio to PATH so that moving files to the trash works when not using a desktop environment
- - prefix PATH : $ { glib . bin } /bin
- - add-flags " \' ' ${ NIXOS_OZONE_WL : + \ '' ${ WAYLAND_DISPLAY : + - - ozone-platform-hint = auto - - enable-features = WaylandWindowDecorations } } "
- - add-flags $ { lib . escapeShellArg commandLineArgs }
)
'' ;
# See https://github.com/NixOS/nixpkgs/issues/49643#issuecomment-873853897
# linux only because of https://github.com/NixOS/nixpkgs/issues/138729
2024-09-26 11:04:55 +00:00
postPatch = lib . optionalString stdenv . hostPlatform . isLinux ''
2023-10-19 13:55:26 +00:00
# this is a fix for "save as root" functionality
packed = " r e s o u r c e s / a p p / n o d e _ m o d u l e s . a s a r "
unpacked = " r e s o u r c e s / a p p / n o d e _ m o d u l e s "
asar extract " $ p a c k e d " " $ u n p a c k e d "
substituteInPlace $ unpacked / @ vscode/sudo-prompt/index.js \
- - replace " / u s r / b i n / p k e x e c " " / r u n / w r a p p e r s / b i n / p k e x e c " \
- - replace " / b i n / b a s h " " ${ bash } / b i n / b a s h "
rm - rf " $ p a c k e d "
# without this symlink loading JsChardet, the library that is used for auto encoding detection when files.autoGuessEncoding is true,
# fails to load with: electron/js2c/renderer_init: Error: Cannot find module 'jschardet'
# and the window immediately closes which renders VSCode unusable
# see https://github.com/NixOS/nixpkgs/issues/152939 for full log
ln - rs " $ u n p a c k e d " " $ p a c k e d "
'' + (
let
vscodeRipgrep =
2024-09-26 11:04:55 +00:00
if stdenv . hostPlatform . isDarwin then
2023-10-19 13:55:26 +00:00
" C o n t e n t s / R e s o u r c e s / a p p / n o d e _ m o d u l e s . a s a r . u n p a c k e d / @ v s c o d e / r i p g r e p / b i n / r g "
else
" r e s o u r c e s / a p p / n o d e _ m o d u l e s / @ v s c o d e / r i p g r e p / b i n / r g " ;
in
if ! useVSCodeRipgrep then ''
rm $ { vscodeRipgrep }
ln - s $ { ripgrep } /bin/rg $ { vscodeRipgrep }
'' e l s e ''
chmod + x $ { vscodeRipgrep }
''
) ;
2024-09-26 11:04:55 +00:00
postFixup = lib . optionalString stdenv . hostPlatform . isLinux ''
2024-01-02 11:29:13 +00:00
patchelf \
- - add-needed $ { libglvnd } /lib/libGLESv2.so.2 \
- - add-needed $ { libglvnd } /lib/libGL.so.1 \
- - add-needed $ { libglvnd } /lib/libEGL.so.1 \
$ out/lib/vscode / $ { executableName }
2023-10-19 13:55:26 +00:00
'' ;
inherit meta ;
} )