about summary refs log tree commit diff
path: root/pkgs/applications/editors/vscode/extensions/ms-python.python/default.nix
blob: 25091b95efc4b037052892250681f8027ad0407b (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
{ lib
, vscode-utils
, icu
, python3
  # When `true`, the python default setting will be fixed to specified.
  # Use version from `PATH` for default setting otherwise.
  # Defaults to `false` as we expect it to be project specific most of the time.
, pythonUseFixed ? false
  # For updateScript
, writeScript
, bash
, curl
, coreutils
, gnused
, jq
, nix
}:

vscode-utils.buildVscodeMarketplaceExtension rec {
  mktplcRef = {
    name = "python";
    publisher = "ms-python";
    version = "2023.1.10091012";
    hash = "sha256-JosFv6ngJmw1XRILwTZMVxlGIdWFLFQjj4olfnVwAIM=";
  };

  buildInputs = [ icu ];

  nativeBuildInputs = [ python3.pkgs.wrapPython ];

  propagatedBuildInputs = with python3.pkgs; [
    debugpy
    jedi-language-server
  ];

  postPatch = ''
    # remove bundled python deps and use libs from nixpkgs
    rm -r pythonFiles/lib
    mkdir -p pythonFiles/lib/python/
    ln -s ${python3.pkgs.debugpy}/lib/*/site-packages/debugpy pythonFiles/lib/python/
    buildPythonPath "$propagatedBuildInputs"
    for i in pythonFiles/*.py; do
      patchPythonScript "$i"
    done
  '' + lib.optionalString pythonUseFixed ''
    # Patch `packages.json` so that nix's *python* is used as default value for `python.pythonPath`.
    substituteInPlace "./package.json" \
      --replace "\"default\": \"python\"" "\"default\": \"${python3.interpreter}\""
  '';

  passthru.updateScript = writeScript "update" ''
    #! ${bash}/bin/bash

    set -eu -o pipefail

    export PATH=${lib.makeBinPath [
      curl
      coreutils
      gnused
      jq
      nix
    ]}

    api=$(curl -s 'https://marketplace.visualstudio.com/_apis/public/gallery/extensionquery' \
      -H 'accept: application/json;api-version=3.0-preview.1' \
      -H 'content-type: application/json' \
      --data-raw '{"filters":[{"criteria":[{"filterType":7,"value":"${mktplcRef.publisher}.${mktplcRef.name}"}]}],"flags":16}')
    # Find the latest version compatible with stable vscode version
    version=$(echo $api | jq -r '.results[0].extensions[0].versions | map(select(has("properties"))) | map(select(.properties | map(select(.key == "Microsoft.VisualStudio.Code.Engine")) | .[0].value | test("\\^[0-9.]+$"))) | .[0].version')

    if [[ $version != ${mktplcRef.version} ]]; then
      tmp=$(mktemp)
      curl -sLo $tmp $(echo ${(import ../mktplcExtRefToFetchArgs.nix mktplcRef).url} | sed "s|${mktplcRef.version}|$version|")
      hash=$(nix hash file --type sha256 --base32 --sri $tmp)
      sed -i -e "s|${mktplcRef.hash}|$hash|" -e "s|${mktplcRef.version}|$version|" pkgs/applications/editors/vscode/extensions/ms-python.python/default.nix
    fi
  '';

  meta = {
    description = "A Visual Studio Code extension with rich support for the Python language";
    downloadPage = "https://marketplace.visualstudio.com/items?itemName=ms-python.python";
    homepage = "https://github.com/Microsoft/vscode-python";
    changelog = "https://github.com/microsoft/vscode-python/releases";
    license = lib.licenses.mit;
    platforms = [ "x86_64-linux" "aarch64-darwin" "x86_64-darwin" ];
    maintainers = [ lib.maintainers.jraygauthier lib.maintainers.jfchevrette ];
  };
}