summary refs log tree commit diff
path: root/pkgs/applications/editors/vscode/generic.nix
blob: e100318005021d932b2d3360eb965d2143bd5d9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
{ stdenv, lib, makeDesktopItem
, unzip, libsecret, libXScrnSaver, libxshmfence, wrapGAppsHook, makeWrapper
, atomEnv, at-spi2-atk, autoPatchelfHook
, systemd, fontconfig, libdbusmenu, glib, buildFHSUserEnvBubblewrap
, writeShellScriptBin

# Populate passthru.tests
, tests

# needed to fix "Save as Root"
, nodePackages, bash

# Attributes inherit from specific versions
, version, src, meta, sourceRoot
, executableName, longName, shortName, pname, updateScript
# sourceExecutableName is the name of the binary in the source archive, over
# which we have no control
, sourceExecutableName ? executableName
}:

let
  inherit (stdenv.hostPlatform) system;
  unwrapped = stdenv.mkDerivation {

    inherit pname version src sourceRoot;

    passthru = {
      inherit executableName longName tests updateScript;
      fhs = fhs {};
      fhsWithPackages = f: fhs { additionalPkgs = f; };
    };

    desktopItem = makeDesktopItem {
      name = executableName;
      desktopName = longName;
      comment = "Code Editing. Redefined.";
      genericName = "Text Editor";
      exec = "${executableName} %F";
      icon = "code";
      startupNotify = true;
      startupWMClass = shortName;
      categories = [ "Utility" "TextEditor" "Development" "IDE" ];
      mimeTypes = [ "text/plain" "inode/directory" ];
      keywords = [ "vscode" ];
      actions.new-empty-window = {
        name = "New Empty Window";
        exec = "${executableName} --new-window %F";
        icon = "code";
      };
    };

    urlHandlerDesktopItem = makeDesktopItem {
      name = executableName + "-url-handler";
      desktopName = longName + " - URL Handler";
      comment = "Code Editing. Redefined.";
      genericName = "Text Editor";
      exec = executableName + " --open-url %U";
      icon = "code";
      startupNotify = true;
      categories = [ "Utility" "TextEditor" "Development" "IDE" ];
      mimeTypes = [ "x-scheme-handler/vscode" ];
      keywords = [ "vscode" ];
      noDisplay = true;
    };

    buildInputs = [ libsecret libXScrnSaver libxshmfence ]
      ++ lib.optionals (!stdenv.isDarwin) ([ at-spi2-atk ] ++ atomEnv.packages);

    runtimeDependencies = lib.optional stdenv.isLinux [ (lib.getLib systemd) fontconfig.lib libdbusmenu ];

    nativeBuildInputs = [ unzip ]
      ++ lib.optionals stdenv.isLinux [
        autoPatchelfHook
        nodePackages.asar
        (wrapGAppsHook.override { makeBinaryWrapper = makeWrapper; })
      ];

    dontBuild = true;
    dontConfigure = true;
    noDumpEnvVars = true;

    installPhase = ''
      runHook preInstall
    '' + (if stdenv.isDarwin then ''
      mkdir -p "$out/Applications/${longName}.app" "$out/bin"
      cp -r ./* "$out/Applications/${longName}.app"
      ln -s "$out/Applications/${longName}.app/Contents/Resources/app/bin/${sourceExecutableName}" "$out/bin/${executableName}"
    '' else ''
      mkdir -p "$out/lib/vscode" "$out/bin"
      cp -r ./* "$out/lib/vscode"

      ln -s "$out/lib/vscode/bin/${sourceExecutableName}" "$out/bin/${executableName}"

      mkdir -p "$out/share/applications"
      ln -s "$desktopItem/share/applications/${executableName}.desktop" "$out/share/applications/${executableName}.desktop"
      ln -s "$urlHandlerDesktopItem/share/applications/${executableName}-url-handler.desktop" "$out/share/applications/${executableName}-url-handler.desktop"

      mkdir -p "$out/share/pixmaps"
      cp "$out/lib/vscode/resources/app/resources/linux/code.png" "$out/share/pixmaps/code.png"

      # Override the previously determined VSCODE_PATH with the one we know to be correct
      sed -i "/ELECTRON=/iVSCODE_PATH='$out/lib/vscode'" "$out/bin/${executableName}"
      grep -q "VSCODE_PATH='$out/lib/vscode'" "$out/bin/${executableName}" # check if sed succeeded
    '') + ''
      runHook postInstall
    '';

    preFixup = ''
      gappsWrapperArgs+=(
        # 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:+--enable-features=UseOzonePlatform --ozone-platform=wayland}}"
      )
    '';

    # See https://github.com/NixOS/nixpkgs/issues/49643#issuecomment-873853897
    # linux only because of https://github.com/NixOS/nixpkgs/issues/138729
    postPatch = lib.optionalString stdenv.isLinux ''
      # this is a fix for "save as root" functionality
      packed="resources/app/node_modules.asar"
      unpacked="resources/app/node_modules"
      asar extract "$packed" "$unpacked"
      substituteInPlace $unpacked/@vscode/sudo-prompt/index.js \
        --replace "/usr/bin/pkexec" "/run/wrappers/bin/pkexec" \
        --replace "/bin/bash" "${bash}/bin/bash"
      rm -rf "$packed"

      # 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 "$unpacked" "$packed"

      # this fixes bundled ripgrep
      chmod +x resources/app/node_modules/@vscode/ripgrep/bin/rg
    '';

    inherit meta;
  };

  # 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
  #
  # buildFHSUserEnv allows for users to use the existing vscode
  # extension tooling without significant pain.
  fhs = { additionalPkgs ? pkgs: [] }: buildFHSUserEnvBubblewrap {
    # 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
      openssl
      zlib

      # mono
      krb5
    ]) ++ additionalPkgs pkgs;

    # symlink shared assets, including icons and desktop entries
    extraInstallCommands = ''
      ln -s "${unwrapped}/share" "$out/"
    '';

    runScript = "${unwrapped}/bin/${executableName}";

    # vscode likes to kill the parent so that the
    # gui application isn't attached to the terminal session
    dieWithParent = false;

    passthru = {
      inherit executableName;
      inherit (unwrapped) pname version; # for home-manager module
    };

    meta = meta // {
      description = ''
        Wrapped variant of ${pname} which launches in a FHS compatible envrionment.
        Should allow for easy usage of extensions without nix-specific modifications.
      '';
    };
  };
in
  unwrapped