Enable pip install in jupyter-lab service

Hi all

I’ve successfully configured a JupyterLab service on NixOS, but I’ve run into an issue when trying to install additional pip packages directly from the Jupyter notebook interface using the %pip install command. I encountered the following error:

ERROR: Could not install packages due to an OSError: [Errno 30] Read-only file system: '/nix/store/98kl9npajj0k2ffw00yknm6wdn15kbwf-python3-3.11.9-env/lib/python3.11/site-packages/cymem'

Here is my current Nix configuration:

{ pkgs, ... }:
{

  services.jupyter = {
    enable = true;
    user = "cyrill";
    ip = "0.0.0.0";
    port = 8889;
    package = pkgs.python311Packages.jupyterlab;
    command = "jupyter-lab";
      kernels = {
        python3 = let
          env = pkgs.python3.withPackages (ps:
            with ps; [
              ipykernel
              transformers
              pytorch
	      llama-index-core
              llama-index-readers-file
              llama-index-llms-ollama
              llama-index-embeddings-ollama
              llama-index-embeddings-huggingface
            ]);
        in {
          displayName = "Python 3";
          argv = [
            env.interpreter
            "-m"
            "ipykernel_launcher"
            "-f"
            "{connection_file}"
          ];
          language = "python";
        };
      };
    notebookDir = "~/jupyter-notebook";
  };
}

To resolve the issue, I attempted to extend the configuration by adding a preStart script that activates a virtual environment (venv). Here’s the additional snipped I included:

    # Create a virtual environment in the home directory
     systemd.services.jupyter.preStart = ''
       if [ ! -d ${venvDir} ]; then
         ${env.interpreter} -m venv ${venvDir}
         source ${venvDir}/bin/activate
       else
         source ${venvDir}/bin/activate
       fi
     '';

Am I on the right track with this approach, or is there a better way to allow pip installations directly from the notebook interface in NixOS? Thanks for your help

kind regards
cyrill