DavHau / mach-nix

Create highly reproducible python environments
MIT License
858 stars 105 forks source link

How to replace old nixpkgs with mach-nix.mkPython #493

Open sandangel opened 2 years ago

sandangel commented 2 years ago

Hi, some python nixpkgs is old. I'm trying to use mach-nix to install that package from official registries (like pip) instead.

what I tried so far:

    (python3.withPackages (ps: with ps; [
      (mach-nix.mkPython {
        requirements = ''
          pynvim
          python-lsp-black
          python-lsp-server
          pylsp-mypy
        '';
      })
    ]))

but I don't get the pylsp binary installed. This code is working:

    (python3.withPackages (ps: with ps; [
          pynvim
          python-lsp-black
          python-lsp-server
          pylsp-mypy
    ]))

but of course the version is older.

bjornfor commented 2 years ago

Try

let
  mach-nix = import (builtins.fetchGit {
    url = "https://github.com/DavHau/mach-nix/";
    ref = "refs/heads/master";
    #rev = "..."; # recommended to pin a commit
  }) { };

  customPython = mach-nix.mkPython {
    requirements = ''
      pynvim
      python-lsp-black
      python-lsp-server
      pylsp-mypy
    '';
  };
in
  customPython

That got me a 'pylsp' binary..

sandangel commented 2 years ago

I tried that already. pysql is executable, but that won't work for neovim because python runtime need to be aware of those python packages:

https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/python.section.md#ad-hoc-temporary-python-environment-with-nix-shell-ad-hoc-temporary-python-environment-with-nix-shell

bjornfor commented 2 years ago

I'm not exactly sure what you're aiming for, but perhaps you get what you want by changing mkPython to mkPythonShell and then use nix-shell?

sandangel commented 2 years ago

@bjornfor hi, thanks for your help. I'm trying to set up the neovim lsp for python. I want to install the lsp server packages using nix, but they are old. so I thought I could use mach-nix to install newer version. but apperently it doesn't work with mach-nix because neovim python need to reference them as its site packages, basically the nix venv version of python.

bjornfor commented 2 years ago

So you want neovim from nixpkgs to be built with a custom python from mach-nix?

sandangel commented 2 years ago

not neccessary, just the pkgs.python3 from nixpkgs need to be aware of python packages from mach-nix. or if mach-nix also installing python, I need to configure neovim to point to that python.

bjornfor commented 2 years ago

I looked a bit into it and found the nixpkgs expressions for neovim to be written in such a way that providing a mach-nix customPython to it wasn't that straight forward.

I'm not sure whether this interface mismatch is an issue in mach-nix or nixpkgs :-)

sandangel commented 2 years ago

you can always config neovim python with vim.g.python3_host_prog = vim.env.HOME .. '/.nix-profile/bin/python' . It's just that python need to be aware of site packages installed by mach-nix. I still can not figure out.

bjornfor commented 2 years ago

Then point to a customPython from mach-nix instead of $HOME/.nix-profile/bin/python?

sandangel commented 2 years ago

@bjornfor where is that python though . I need the path

bjornfor commented 2 years ago

For a quick hack, run command -v python3 when you're in your working mach-nix python env. That'll give you the path.

bjornfor commented 2 years ago

For a quick hack, run command -v python3 when you're in your working mach-nix python env. That'll give you the path.

Better: Use something like https://github.com/DavHau/mach-nix/issues/493#issuecomment-1188151209 as starting point and run nix-build to get the store path.

sandangel commented 2 years ago

I got /nix/store/10nbcgpi5vgbadacrd1b8aw0gfgpk293-python3-3.10.5/bin/python, but I want to have something like "${customPython.pkgs}/bin/python" in my nvim config instead of hard coding.

bjornfor commented 2 years ago

I got /nix/store/10nbcgpi5vgbadacrd1b8aw0gfgpk293-python3-3.10.5/bin/python, but I want to have something like "${customPython.pkgs}/bin/python" in my nvim config instead of hard coding.

"${customPython.pkgs}/bin/python" -> "${import ./your-mach-nix-python-file.nix}/bin/python"?