How to install pip library in idx from requirements.txt

i try to make a Django app, when i try to install a library via pip, its show error:


× This environment is externally managed
╰─> This command has been disabled as it tries to modify the immutable
    `/nix/store` filesystem.
    
    To use Python with Nix and nixpkgs, have a look at the online documentation:
    <https://nixos.org/manual/nixpkgs/stable/#python>.

Does you project have a python virtual env configured??

If not then configure a virtual env then activate the env and run the pip install command

If it already has a virtual env then activate the env and run the pip install command

Can you provide dev.nix file for Django Project. I am using custom dev.nix file but I don’t know how to make changes in dev.nix file. I also tried to read documentation but still not able to run comment to activate virtual environment on onStart.

Out of curiosity, which template did you use? Looks like you’re not in a virtual environment. Can you create one and then try to install?

python -m venv .venv
source .venv/bin/activate
pip install django

you are right, i adjust file and error is gone. but i have another error while preview.
edit: i open project again and this error also fixed. but in terminal:

apitest-3xxxx39:~/api$ /nix/store/4cb33zcc0i7wrnmwwfp9q4y2p4fas9gd-active-venv
source /home/user/api/.venv/bin/activate
ERROR: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
apitest-3xxxx39:~/api$ source /home/user/api/.venv/bin/activate
(.venv) apitest-3xxxx39:~/api$ 

dev.nix:

{ pkgs, ... }: {
  channel = "stable-24.05";
  packages = [
    pkgs.python3
    pkgs.git
    pkgs.zsh
  ];
  env = {};
    idx.extensions = [
      "ms-python.python"
      "dbaeumer.vscode-eslint"
      "esbenp.prettier-vscode"
    ];
    idx.workspace = {
      onCreate = {
        create-venv = ''
          python -m venv .venv
          source .venv/bin/activate
          pip install -r mysite/requirements.txt
        '';
        default.openFiles = ["mysite/requirements.txt" "mysite/mysite/urls.py"];
      };
      onStart = {
        active-venv = ''
        source .venv/bin/activate
        pip install -r requirements.txt
        '';
      };
    };
    idx.previews = {
      enable = true;
      previews = {
        web = {
          command = ["./devserver.sh"];
          env = {
            PORT = "$PORT";
          };
          manager = "web";
        };
      };
    };
  services.postgres = {
    enable = true;
    extensions = ["pgvector"];
  };
  services.redis = {
    enable = true;
    port = 5535;
  };
}

and devserver.sh:

#!/bin/sh
source .venv/bin/activate
python mysite/manage.py runserver $PORT

sorry was busy with something else:

You can checkout this repo that for a fastapi and try doing something similar: fastapi-idx/dev.nix at main · srivats22/fastapi-idx · GitHub