about summary refs log tree commit diff
path: root/pkgs/applications/editors/vscode/extensions/vscodeEnv.nix
blob: 37b82075e7518215919fd631a3ff7f8143473d3f (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
#Use vscodeWithConfiguration and vscodeExts2nix to create a vscode executable. When the executable exits, it updates the mutable extension file, which is imported when evaluated by Nix later.
{
  lib,
  buildEnv,
  writeShellScriptBin,
  extensionsFromVscodeMarketplace,
  vscodeDefault,
  jq,
}:
##User input
{
  vscode ? vscodeDefault,
  nixExtensions ? [ ],
  vscodeExtsFolderName ? ".vscode-exts",
  # will add to the command updateSettings (which will run on executing vscode) settings to override in settings.json file
  settings ? { },
  createSettingsIfDoesNotExists ? true,
  launch ? { },
  createLaunchIfDoesNotExists ? true,
  # will add to the command updateKeybindings(which will run on executing vscode) keybindings to override in keybinding.json file
  keybindings ? { },
  createKeybindingsIfDoesNotExists ? true,
  user-data-dir ? ''"''${TMP}''${name}"/vscode-data-dir'',
  # if file exists will use it and import the extensions in it into this derivation else will use empty extensions list
  # this file will be created/updated by vscodeExts2nix when vscode exists
  mutableExtensionsFile,
}:
let
  mutableExtensionsFilePath = toString mutableExtensionsFile;
  mutableExtensions = lib.optionals (builtins.pathExists mutableExtensionsFile) (
    import mutableExtensionsFilePath
  );
  vscodeWithConfiguration =
    import ./vscodeWithConfiguration.nix
      {
        inherit lib writeShellScriptBin extensionsFromVscodeMarketplace;
        vscodeDefault = vscode;
      }
      {
        inherit
          nixExtensions
          mutableExtensions
          vscodeExtsFolderName
          user-data-dir
          ;
      };

  updateSettings = import ./updateSettings.nix { inherit lib writeShellScriptBin jq; };
  userSettingsFolder = "${user-data-dir}/User";

  updateSettingsCmd = updateSettings {
    settings = {
      "extensions.autoCheckUpdates" = false;
      "extensions.autoUpdate" = false;
      "update.mode" = "none";
    } // settings;
    inherit userSettingsFolder;
    createIfDoesNotExists = createSettingsIfDoesNotExists;
    symlinkFromUserSetting = (user-data-dir != "");
  };

  updateLaunchCmd = updateSettings {
    settings = launch;
    createIfDoesNotExists = createLaunchIfDoesNotExists;
    vscodeSettingsFile = ".vscode/launch.json";
  };

  updateKeybindingsCmd = updateSettings {
    settings = keybindings;
    createIfDoesNotExists = createKeybindingsIfDoesNotExists;
    vscodeSettingsFile = ".vscode/keybindings.json";
    inherit userSettingsFolder;
    symlinkFromUserSetting = (user-data-dir != "");
  };

  vscodeExts2nix =
    import ./vscodeExts2nix.nix
      {
        inherit lib writeShellScriptBin;
        vscodeDefault = vscodeWithConfiguration;
      }
      {
        extensionsToIgnore = nixExtensions;
        extensions = mutableExtensions;
      };
  code = writeShellScriptBin "code" ''
    ${updateSettingsCmd}/bin/vscodeNixUpdate-settings
    ${updateLaunchCmd}/bin/vscodeNixUpdate-launch
    ${updateKeybindingsCmd}/bin/vscodeNixUpdate-keybindings
    ${vscodeWithConfiguration}/bin/code --wait "$@"
    echo 'running vscodeExts2nix to update ${mutableExtensionsFilePath}...'
    ${vscodeExts2nix}/bin/vscodeExts2nix > ${mutableExtensionsFilePath}
  '';
in
buildEnv {
  name = "vscodeEnv";
  paths = [
    code
    vscodeExts2nix
    updateSettingsCmd
    updateLaunchCmd
    updateKeybindingsCmd
  ];
}