diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..ee2c5aa0 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1704008649, + "narHash": "sha256-rGPSWjXTXTurQN9beuHdyJhB8O761w1Zc5BqSSmHvoM=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "d44d59d2b5bd694cd9d996fd8c51d03e3e9ba7f7", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "utils": "utils" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1701680307, + "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "4022d587cbbfd70fe950c1e2083a02621806a725", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..ec7ecfc0 --- /dev/null +++ b/flake.nix @@ -0,0 +1,26 @@ +{ + description = "pygls: The Generic Language Server Framework"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, utils }: + + utils.lib.eachDefaultSystem(system: + let + pkgs = import nixpkgs { inherit system; }; + python-wasi = pkgs.callPackage ./nix/python-wasi.nix {}; + # TODO: Perhaps an overlay would allow callPackage to know what python-wasi is. + mkPythonWASIShell = pkgs.callPackage ./nix/python.nix { python-wasi = python-wasi; }; + in { + + devShells.default = mkPythonWASIShell { + pyPackages = with pkgs.python311Packages; [ + lsprotocol + ]; + }; + } + ); +} diff --git a/nix/python-wasi.nix b/nix/python-wasi.nix new file mode 100644 index 00000000..3fdcb838 --- /dev/null +++ b/nix/python-wasi.nix @@ -0,0 +1,17 @@ +{ fetchzip, stdenv }: + +stdenv.mkDerivation { + pname = "python-wasi"; + version = "3.11.4"; + + src = fetchzip { + url = "https://github.com/brettcannon/cpython-wasi-build/releases/download/v3.11.4/python-3.11.4-wasi_sdk-16.zip"; + stripRoot = false; + sha256 = "sha256-AZGdgpRvHcu6FY/a7capldjDhTpkfhGkqYnm127nAN8="; + }; + + buildCommand = '' + mkdir $out + cp -r $src/* $out + ''; +} diff --git a/nix/python.nix b/nix/python.nix new file mode 100644 index 00000000..6cd22dbf --- /dev/null +++ b/nix/python.nix @@ -0,0 +1,32 @@ +{ lib +, python-wasi +, mkShell +, stdenv +, wasmtime +, writeShellScriptBin +}: + +{ pyPackages ? [] +}: + +let + + pyDeps = lib.concatMap (pkg: lib.remove pkg.pythonModule pkg.requiredPythonModules) pyPackages; + allPackages = lib.unique (pyPackages ++ pyDeps); + + pythonPath = lib.concatMapStringsSep ":" (pkg: "${pkg}/lib/python3.11/site-packages") allPackages; + + python = writeShellScriptBin "python-wasi" '' + exec ${wasmtime}/bin/wasmtime run ${python-wasi}/python.wasm \ + --env PYTHONHOME=${python-wasi} \ + --env PYTHONPATH='.:${pythonPath}' \ + --dir ${python-wasi} \ + ${lib.concatMapStringsSep "\\\n " (pkg: "--dir '${pkg}/lib/python3.11/site-packages' ") allPackages} \ + --dir . \ + -- "$@" + ''; + +in mkShell { + name = "python-wasi"; + packages = [ python ]; +}