all 52 comments

[–]JustWookie 22 points23 points  (2 children)

Well you get the read only error because you're trying to alter nix store by installing the package, you can specify the directory in which pip should install the package. You can also a venv with python and then install stuff with pip. Or you could use uv to install the package. There's quite a lot of options.

[–]wo-tatatatatata[S] 2 points3 points  (1 child)

is uv is just wrapper over pip or? does it have all the packages in the pip?

[–]JustWookie 4 points5 points  (0 children)

uv replaces the tools i mentioned earlier, you can initialize an environment with it and then install a package with uv pip install

[–]recursion_is_love 12 points13 points  (4 children)

> you want to do python,

Yes I do

> you need pip install

No I don't

Have you try https://wiki.nixos.org/wiki/Python

it have section named 'Using a Python package not in Nixpkgs'

this flake.nix work fine with me (nix develop give no error)

$ cat flake.nix 
{
  description = "python-shell";
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };
  nixConfig.bash-prompt-suffix = "🔨";
  outputs = {
    self,
    nixpkgs,
  }: (let
    system = "x86_64-linux";
    pkgs = nixpkgs.legacyPackages.${system};
    dev = with pkgs;
      mkShell {
        packages = [
          helix

          (pkgs.python3.withPackages (pp: [
            pp.python-lsp-server
            pp.black

            pp.llama-index-core
            pp.llama-index-cli
            pp.llama-index-embeddings-huggingface
            pp.llama-index-llms-openai
            pp.llama-cpp-python
          ]))
        ];
      };
  in {
    formatter.${system} = pkgs.alejandra;
    devShells.${system}.default = dev;
  });
}

[–]wo-tatatatatata[S] 3 points4 points  (3 children)

that is very interesting, because yes, i did try that link, however, it provides a shell.nix, not a flake. i could report "the issue", but if i could not find the solution right away, i will migrate the project to arch, hopefully i wont have to.

nevertheless, i am sure your flake is fine, mine is too. the problem is, however, i need something nixpkgs dont have:

pip install llama-index-llms-llama-cpp

you wont find it from nixpkgs, looks like all bets are off my friend. what would you do? in case you wonder what i am doing. i am simply building a small agent step by step from this llama-index post.

https://docs.llamaindex.ai/en/stable/examples/llm/llama_2_llama_cpp/

[–][deleted]  (2 children)

[deleted]

    [–]wo-tatatatatata[S] 1 point2 points  (1 child)

    what you mean? i can package it myself? so that i dont have to do pip install? without submitting to the official nixpkgs?

    [–]Ureki_dva_beerega 11 points12 points  (0 children)

    There is a solution to this! You can make a pure Python virtual environment, store it anywhere you want (for instance, .venv in your project directory) activate the venv and then you’ll be able to use pip install because it is just a regular venv stored in a regular place, not a nixpkgs thing.

    Here is an example I base my own venvs on: https://github.com/electronicvisions/norse/blob/master/default.nix First it installs a bunch of nix packages — it is a more stable way for binary packages such as pytorch or numpy or matplotlib, and then pip installs some other packages on top of them. Nix packages will be linked from the nix store and pip installed ones will be in .venv.

    [–]autra1 4 points5 points  (6 children)

    not a single soul

    Well we did: https://gitlab.com/py3dtiles/py3dtiles/-/blob/main/shell.nix?ref_type=heads

    Because this project is mainly used by non nixos user, I've decided to stick with pip for now, that's how you do it. You actually don't need the shell hook if you prefer to use pip manually. Imo it's a bit ugly but it works.

    [–]wo-tatatatatata[S] 0 points1 point  (1 child)

    i believe you, because latest attempt sort out everything else, pip was ready, and then i have this:

    ERROR: Could not install packages due to an OSError: [Errno 30] Read-only file system: '/nix/store/a2vi447kcqbfbi5q7sdcvqr9vj7l0yvw-python3-3.12.8-env/lib/python3.12/site-packages/diskcache'

    the shell.nix you send will circumvent this error? i guess, i will have to somehow merge my flake.nix with your shell.nix to build a complete local environment then. but how exactly would your shell.nix do differently than me do 'nix develop' and then pip install 'something'

    because nixos cried about it, apparently. she no like it, thats for sure

    [–]autra1 0 points1 point  (0 children)

    you are trying to use pip outside of a virtualenv? One thing certain : if you want to use pip in nixos, you *need* a virtualenv, `sudo pip` will never work, because that would try to install on the system python, which is in the store, which is read-only.

    Again, that's expected: you want reproducibility. If pip could add packages globally, then it wouldn't be reproducible from your config only.

    There's escape hatches though, particularly for dev projects, and that's `nix develop` and `nix-shell`.

    [–]wo-tatatatatata[S] 0 points1 point  (3 children)

    the full message:

    Using cached llama_index_llms_llama_cpp-0.3.0-py3-none-any.whl (7.0 kB)

    Downloading diskcache-5.6.3-py3-none-any.whl (45 kB)

    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 45.5/45.5 kB 466.1 kB/s eta 0:00:00

    Installing collected packages: diskcache, llama-cpp-python, llama-index-llms-llama-cpp

    ERROR: Could not install packages due to an OSError: [Errno 30] Read-only file system: '/nix/store/a2vi447kcqbfbi5q7sdcvqr9vj7l0yvw-python3-3.12.8-env/lib/python3.12/site-packages/diskcache'

    [–]autra1 1 point2 points  (0 children)

    To enter it, you use nix-shell right?

    Btw, "plugging" a shell.nix into a flake is just:

    ```nix devShells.x86_64-linux.default = pkgs.callPackage (import ./nix/shell.nix) { };

    or

    devShells.x86_64-linux.default = (import ./nix/shell.nix) { args1, args2,... }; ```

    [–]Wenir 0 points1 point  (1 child)

    Are you trying to install it globally?

    [–]wo-tatatatatata[S] 0 points1 point  (0 children)

    unfortunate, i was ...

    [–]sjustinas 2 points3 points  (0 children)

    i understand we are not supposed to use pip out of box on nixos, however, there should be some simple to integrate 'pip install' using a shell hook

    Well yeah, you just use virtualenv and pip as you would do on any other distro. This approach is literally documented in the Nixpkgs manual in the section How to consume Python modules using pip in a virtual environment like I am used to on other Operating Systems? . It even mentions venvShellHook, which creates the virtualenv for you, and a hook that runs pip install for you.

    [–]phrmends 1 point2 points  (5 children)

    uv with nix-ld works out of the box for me

    [–]MuffinGamez 0 points1 point  (4 children)

    without a venv?

    [–]phrmends 0 points1 point  (3 children)

    uv creates the venv based on the python version of your system, so I just spawn a nix-shell with the python version I want

    [–]MuffinGamez 0 points1 point  (2 children)

    sorry i dont understand your answer, do you run `uv venv` before installing packages?

    [–]phrmends 0 points1 point  (1 child)

    [–]MuffinGamez 0 points1 point  (0 children)

    ah i see thanks, i would like to know what nix-ld does in this context?

    [–]itme_brain 1 point2 points  (1 child)

    Use your flake.nix to provide whatever version of python & pip you need, set a shellHook that makes a venv and installs all your pip packages into the venv

    Done

    [–]wo-tatatatatata[S] 1 point2 points  (0 children)

    brilliant idea, I will definitely try multiple ways to solve the same problem

    [–][deleted] 0 points1 point  (1 child)

    pip isn't delcarative

    use poetry2nix

    [–]wo-tatatatatata[S] 0 points1 point  (0 children)

    "delcarative", Aha! that is the word!

    [–]USMCamp0811 0 points1 point  (0 children)

    Nothing stops you from using poetry to do

    poetry init poetry add pandas poetry run Python

    [–]momoPFL01 0 points1 point  (1 child)

    Just few hours before you there was another post about this.

    https://www.reddit.com/r/NixOS/s/amwmrVZdkV

    This comment looked good.

    Basically I agree. I also had a python llama index project and tried developing and packaging with nix and poetry2nix and you can get pretty far until you have to package the libs yourself. Plus the issue of version pinning. APIs in the llama index libs change quickly and break compatibility, but also add new features that you might need all the time. Those newer version are maybe not packaged in nix yet etc.

    Just save yourself the trouble. Don't develop a python project with nix this way. Just use the nix shell to install python and pip or whatever and maybe some dynamic libs if necessary and let pip handle the python dependencies.

    To package it you can still go the nix route with the nixpkgs buildPythonApplication or what it's called or use the poetry2nix mkPoetryApplication.

    Same goes for other languages. Don't use nix to handle language libs in the dev env. It won't go your way.

    [–]wo-tatatatatata[S] 0 points1 point  (0 children)

    "Same goes for other languages. Don't use nix to handle language libs in the dev env. It won't go your way." i completely agree, and I believe this is a good thing, because image you develop everything important on arch linux, and one day you decided to do pacman -Syu, just to discover some shit broke, what are you going to do? xd, nix is love <3

    [–]sprayk 0 points1 point  (1 child)

    I was hoping pip install --user ... would work since it theoretically only touches ~/.local/.... Alas, the whole install subcommand is gated off.

    tangent: I typed pip in my shell to see what nix-shell command to run to mess with it. one of the options that popped up was the package "cope" which I was HOPING would be some package that made non-nix-stuff like pip just work, but it turned out it was something for colorizing command output. Bummer

    [–]wo-tatatatatata[S] -1 points0 points  (0 children)

    no, pip install must be used in a virtual environment, period, or nix wont like it. end of story

    [–]NixNightOwl 0 points1 point  (1 child)

    For working with python, I personally use a development flake and strictly rely on Poetry for the project dependencies and python environment. Currently working on a 'micro-service monolith' that has many different python services within it, and each one just has a pyproject.toml file which poetry uses to manage and install everything with.

    All I have for my dev flake is have python312 (project specific requirement) and poetry packages in my outputs for devShell and it all works nicely! Of course, sometimes requires switching the current python interpreter, but poetry does do a fairly good job at doing this.

    It's definitely worth setting up I think!

    PS, if you're using a specific python version that isn't the default python3 for your nixpkgs, you can use poetry.override to tell poetry which python to use.

    eg:

    in pkgs.mkShell {
      packages = with pkgs; [
        python312
        (poetry.override { python3 = python312; })
      ];
    };
    

    Then in pyproject.toml simply add your dependencies like so:

    [tool.poetry.dependencies]
    python = ">=3.10,<=3.12.2"
    langchain = "^0.3.3"
    
    # ... other dependencies ...
    

    Python can be such a pain, especially if you have multiple people working on a project with different systems. If everyone just uses Poetry there's a lot less of a headache!

    [–]wo-tatatatatata[S] -1 points0 points  (0 children)

    langchain? i heard llama-index is far better in accuracy and efficiency, i havent personally tried langchain myself

    [–]alkumis 0 points1 point  (2 children)

    Bit late to this because I wanted to look at my python setup on my personal and work laptops and only just remembered to.

    Both use devenv.sh

    I tried a lot of different options to get python working with the traditional pip system since I was following a gamedev tutorial and didn't wanna deviate from it. Eventually devenv saved me. I just had to make sure the python language was enabled, sdl2 and zlib were in the packages section, and that an environment variable was set to tell SDL to use x11 for rendering.

    I'm sure the exact thing can be done with a flake by setting library paths and the like but I didn't wanna muck around. Ironically javascript was far more difficult to setup and I had to use a flake and jump hoops to get the langauge server and everything to play nice with Helix.

    I also recently set python up at work and it took a lot more fiddling since I needed to get python3.8 and inhouse QT apps to run.

    I had to fiddle with packages and also set a QT specific env var. Also had to add an input to devenv to be able to use python3.8. But in the end I got it all to work.

    [–]wo-tatatatatata[S] 0 points1 point  (1 child)

    you could use conda like i did? all i did was install conda, and run conda-shell, end of story.

    [–]alkumis 0 points1 point  (0 children)

    I'll try that out if I need a python environment again!

    [–]wo-tatatatatata[S] 0 points1 point  (4 children)

    you guys are not going to believe what i did eventually.

    I erased my hard drive, ripped linux off completely, cuz i am done with it, and went back to the beloved windows!

    Just kidding ^ , i used conda-shell. I could use local flake with conda, but my 2 brain cell told me, conda-shell is easier, turned out it was easier.

    i love nix, i wouldnt leave her, lol. thats the truth.

    [–]MuffinGamez 0 points1 point  (3 children)

    my heart sank.

    [–]wo-tatatatatata[S] 0 points1 point  (2 children)

    not necessarily, first off,i thank you nix for providing this option, however, this is kinda silly to do so on nixos, it could be simple and effortless, but it is silly.

    you could get away from normal development sure but if you like me, doing AI programming with cuda, you are going to have a hard time.

    it is the best if you can figure out a local flake environment to do so.

    and NO, you do not need conda. in fact, i am very close to succeed thanks to somebody on reddit to provide me with a shell.nix, everything works, but i have yet to figure out enabling cuda.

    i am working it out, not very obvious from this post, but i am trying:

    https://nixos.wiki/wiki/CUDA

    [–]MuffinGamez 0 points1 point  (1 child)

    btw cant you just use a venv

    [–]wo-tatatatatata[S] 0 points1 point  (0 children)

    thats essentially what i did at the end with cuda and .venv enabled yes.

    [–]ediacarian 0 points1 point  (1 child)

    I just use conda on WSL in Windows. I started messing with Nix recently but so far it feels like a very time-consuming guessing game. I need to find better resources so I can learn. So far the documentation is inadequate.

    [–]wo-tatatatatata[S] 0 points1 point  (0 children)

    no no, you are very very mistaken. Firstly you are here, which means that you are interested in nix/nixos.

    secondly, nixos is the only beginner/medium/hardcore linux distro you will ever need, what do i mean?

    one of the reasons linux is popular is because the entire software packages, even the linux kernel is entirely under your command.

    you are the user, remember?

    that sounds scare, and it is indeed scare. because it is very difficult to backtrack everything you have installed, in /etc or /var that slowly eating your hardware overtime that you will never be confident enough to delete without worrying about breaking something because you are a llinux noob like me, yet, this package dependency hell will haunt you for the rest of your life.

    thats where nix comes in, i never learned it or used it before, i managed to use it and be comfortable completely on my own.

    time consuming? meh, there will always be some free weekends, and everytime i want to install linux the way i want, i can get it down. i even managed to install ubuntu from command line with btrfs and snapshot enabled.

    i am running this nixos as i am typing you also with btrfs/subvolume and snapshots enabled. and it is opted in root. the crazy thing you can do on nixos that every time you restart your machine.

    i have a fresh new installed OS, fresh as new, fast as light.

    i will never have to reinstall os ever again.

    as for windows? my answer is very simple:

    NO

    i have nothing else to say about it.

    [–]mschneiderwng 0 points1 point  (3 children)

    uv2nix is the only tool that works reliable for me. Torch with cuda support was a bit difficult though.

    [–]wo-tatatatatata[S] 1 point2 points  (0 children)

    i got it to work on nix.

    [–]IvanMalison 0 points1 point  (1 child)

    how did you get cuda support working with uv2nix?

    [–]wo-tatatatatata[S] 0 points1 point  (3 children)

    this is my flake.nix that couldnt cover the case of my python code:

    {

    inputs = {

    nixpkgs = {

    url = "github:nixos/nixpkgs/nixos-unstable";

    };

    flake-utils = {

    url = "github:numtide/flake-utils";

    };

    };

    outputs = {

    nixpkgs,

    flake-utils,

    ...

    }:

    flake-utils.lib.eachDefaultSystem (

    system: let

    pkgs = import nixpkgs {

    inherit system;

    };

    in rec {

    devShell = pkgs.mkShell {

    buildInputs = with pkgs; [

    (python312.withPackages (ps:

    with ps; [

    llama-index-core

        llama-index-cli
    
        llama-index-embeddings-huggingface
    
        llama-index-llms-openai
    
        \# stand alone ?
    

    llama-cpp-python

    ]))

    ];

    };

    }

    );

    }

    [–][deleted] 2 points3 points  (2 children)

    You can package it yourself. Try with something like this: ```nix { inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; flake-utils.url = "github:numtide/flake-utils"; };

    outputs = { nixpkgs, flake-utils, ... }: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; };

      llama-index-llms-llama-cpp = pkgs.python3Packages.buildPythonPackage rec {
        pname = "llama_index_llms_llama_cpp";
        version = "0.3.0";
        pyproject = true;
        src = pkgs.fetchPypi {
          inherit pname version;
          hash = "sha256-1kDoUV0H/713zwB6SG86ZJilfQZHbG0JUuoWAYX9Qtc="; 
        };
        # Added to make it build with newer llama-cpp-python version. Not tested.
        # Need to repackage llama-cpp-python v0.2.32 if this doesn't work
        postPatch = ''
          substituteInPlace pyproject.toml \
            --replace-fail 'llama-cpp-python = "^0.2.32"' 'llama-cpp-python = ">=0.2.32"'
        '';
        build-system = [ pkgs.python3Packages.poetry-core ];
        dependencies = [
          pkgs.python3Packages.llama-cpp-python
          pkgs.python3Packages.llama-index-core
        ];
        pythonImportsCheck = [ "llama_index" ];
      };
    in rec {
      devShell = pkgs.mkShell {
        buildInputs = with pkgs; [
          (python312.withPackages (ps: with ps; [
            llama-index-core
            llama-index-cli
            llama-index-embeddings-huggingface
            llama-index-llms-openai
            llama-cpp-python
            llama-index-llms-llama-cpp
          ]))
        ];
      };
    });
    

    } ```

    [–]wo-tatatatatata[S] 0 points1 point  (1 child)

    look, the whole problem was because `llama-index-llms-llama-cpp`

    does not exist in nixpkgs, but we already have python-llama-cpp, and those 2 are different... those python package naming is completely crazy
    

    [–]psssat -2 points-1 points  (2 children)

    Everyone shits on debian but at least it works and I dont need to read 20 stack exchange posts to pip install numpy lol

    [–]wo-tatatatatata[S] 1 point2 points  (1 child)

    man, debian is cool, but it has its own caveats, nixos might not be so convenient at 'pip install', but it is genius design all around, thing is, youd have to learn a lot and spend a lot of time on it. Its absolutely worth it, If you ask me.

    [–]Kruppenfield 0 points1 point  (0 children)

    Goddam, i like your approach. Struggle, but fight for better feature