Jupyter notebooks for Python in VSCode in the nixy way

Hello, I’m trying to setup my workspace to run Jupyter notebooks in VSCode.

VSCode is enabled in home-manager configuration with ms-python.python and ms-toolsai.jupyter extensions.

My shell.nix (took from stuzenz answer here):

with import <nixpkgs> { };

let
  pythonPackages = python312Packages;
  pythonEnv = python312.withPackages (ps: with ps; [
    # python 
    ipykernel
    jupyter
    jupyterlab
    notebook
    pyzmq
    venvShellHook
    pip
    numpy
    pandas
    requests
    scipy
    matplotlib
  ]);
in pkgs.mkShell rec {
  name = "lmptest1";
  venvDir = "./.venv";
  buildInputs = [

    pkgs.stdenv.cc.cc.lib
    stdenv.cc.cc
    git-crypt    

    pythonEnv
  ];

  postVenvCreation = ''
    unset SOURCE_DATE_EPOCH

    python -m ipykernel install --user --name=myenv4 --display-name="myenv4"
  '';

  postShellHook = ''
    unset SOURCE_DATE_EPOCH
  '';
}

Then I run

kein@jeta  ~/Documents/nucleation/lmptest$ nix-shell ./shell.nix
[nix-shell:~/Documents/nucleation/lmptest]$ code .

And open one of my notebooks, and Jupyter extension gives a lot of errors (summary below):

Visual Studio Code (1.98.2, undefined, desktop)
Jupyter Extension Version: 2025.2.2025021801.
Python Extension Version: 2025.4.0.
Pylance Extension Version: 2025.3.2.
Platform: linux (x64).
Home = /home/kein
Temp Storage folder ~/.config/Code/User/globalStorage/ms-toolsai.jupyter/version-2025.2.2025021801
Workspace folder ~/Documents/nucleation/lmptest
19:24:40.854 [warn] Exception while attempting zmq : libstdc++.so.6: cannot open shared object file: No such file or directory
19:24:40.854 [warn] Exception while attempting zmq (fallback) : libstdc++.so.6: cannot open shared object file: No such file or directory
19:24:40.895 [info] Attempting to start a server because of preload conditions ...
19:24:41.830 [warn] Failed to get activated env vars for /nix/store/s9szrmi6w1f7sq5p4qa9rsgiiwvba2s3-python3-3.12.9-env/bin/python3 in 885ms
19:24:41.879 [info] Process Execution: /nix/store/s9szrmi6w1f7sq5p4qa9rsgiiwvba2s3-python3-3.12.9-env/bin/python3 -c "import site;print("USER_BASE_VALUE");print(site.USER_BASE);print("USER_BASE_VALUE");"
19:24:41.942 [info] Process Execution: /nix/store/s9szrmi6w1f7sq5p4qa9rsgiiwvba2s3-python3-3.12.9-env/bin/python3 -c "import jupyter;print('6af208d0-cb9c-427f-b937-ff563e17efdf')"
19:24:41.967 [info] Process Execution: /nix/store/s9szrmi6w1f7sq5p4qa9rsgiiwvba2s3-python3-3.12.9-env/bin/python3 -c "import notebook;print('6af208d0-cb9c-427f-b937-ff563e17efdf')"
19:24:41.980 [warn] Failed to get activated env vars for /nix/store/s9szrmi6w1f7sq5p4qa9rsgiiwvba2s3-python3-3.12.9-env/bin/python in 200ms
19:24:42.004 [info] Process Execution: /nix/store/s9szrmi6w1f7sq5p4qa9rsgiiwvba2s3-python3-3.12.9-env/bin/python -c "import site;print("USER_BASE_VALUE");print(site.USER_BASE);print("USER_BASE_VALUE");"
19:24:42.064 [info] Process Execution: /nix/store/s9szrmi6w1f7sq5p4qa9rsgiiwvba2s3-python3-3.12.9-env/bin/python -m pip list
19:24:42.103 [warn] Failed to get activated env vars for /nix/store/s9szrmi6w1f7sq5p4qa9rsgiiwvba2s3-python3-3.12.9-env/bin/python3 in 38ms
19:24:42.126 [info] Process Execution: /nix/store/s9szrmi6w1f7sq5p4qa9rsgiiwvba2s3-python3-3.12.9-env/bin/python3 -m jupyter kernelspec --version
19:24:42.549 [warn] Failed to get activated env vars for /nix/store/s9szrmi6w1f7sq5p4qa9rsgiiwvba2s3-python3-3.12.9-env/bin/python3 in 11ms
19:40:28.457 [warn] Disposing old controller startUsingPythonInterpreter:'.jvsc74a57bd00b5466e621e9f486033b421612aa6a6d12d4ddaa1f0121f959a4bd7d8011cce2./nix/store/l5dza37ayzb2v82rr4mmb9jb00si1zbg-python3-3.12.9-env/python./nix/store/l5dza37ayzb2v82rr4mmb9jb00si1zbg-python3-3.12.9-env/python.-m#ipykernel_launcher' for view = 'jupyter-notebook'
19:40:43.515 [info] http://localhost:8888: Kernel started: cee41633-fa69-449d-b6e2-d7c95ef2cf96
19:40:43.820 [info] Started session for kernel startUsingPythonInterpreter:.jvsc74a57bd0999ccc97854861602c7e6bc15b3efc1e64adca51cc7e049ee4acc4cb8f0dfe76./nix/store/s9szrmi6w1f7sq5p4qa9rsgiiwvba2s3-python3-3.12.9-env/python./nix/store/s9szrmi6w1f7sq5p4qa9rsgiiwvba2s3-python3-3.12.9-env/python.-m#ipykernel_launcher
19:40:43.963 [info] Kernel successfully started
19:40:43.990 [info] Process Execution: /nix/store/s9szrmi6w1f7sq5p4qa9rsgiiwvba2s3-python3-3.12.9-env/bin/python /home/~/.vscode/extensions/ms-toolsai.jupyter-2025.2.2025021801-linux-x64/pythonFiles/printJupyterDataDir.py
19:40:44.007 [error] Failed to get baseUrl [Error: EINVAL: invalid argument, readlink '/home/~/.vscode/extensions/ms-toolsai.jupyter-2025.2.2025021801-linux-x64/temp/scripts/c92ae8434af0fb1d9ce4ed622ffb63975fcd2d438c0e258f2650fcc671695bda/jupyter/nbextensions'] {
  errno: -22,
  code: 'EINVAL',
  syscall: 'readlink',
  path: '/home/~/.vscode/extensions/ms-toolsai.jupyter-2025.2.2025021801-linux-x64/temp/scripts/c92ae8434af0fb1d9ce4ed622ffb63975fcd2d438c0e258f2650fcc671695bda/jupyter/nbextensions'
}
19:40:44.088 [warn] Cell completed with errors su [Error]: No module named 'matplotlib'
    at n.execute (/home/~/.vscode/extensions/ms-toolsai.jupyter-2025.2.2025021801-linux-x64/dist/extension.node.js:294:4958)
    at processTicksAndRejections (node:internal/process/task_queues:95:5) {
  ename: 'ModuleNotFoundError',
  evalue: "No module named 'matplotlib'",
  traceback: [
    '\x1B[0;31m---------------------------------------------------------------------------\x1B[0m',
    '\x1B[0;31mModuleNotFoundError\x1B[0m                       Traceback (most recent call last)',
    'Cell \x1B[0;32mIn[1], line 1\x1B[0m\n' +
      '\x1B[0;32m----> 1\x1B[0m \x1B[38;5;28;01mfrom\x1B[39;00m\x1B[38;5;250m \x1B[39m\x1B[38;5;21;01mmatplotlib\x1B[39;00m\x1B[38;5;250m \x1B[39m\x1B[38;5;28;01mimport\x1B[39;00m pyplot \x1B[38;5;28;01mas\x1B[39;00m plt\n' +
      '\x1B[1;32m      2\x1B[0m \x1B[38;5;28;01mimport\x1B[39;00m\x1B[38;5;250m \x1B[39m\x1B[38;5;21;01mnumpy\x1B[39;00m\x1B[38;5;250m \x1B[39m\x1B[38;5;28;01mas\x1B[39;00m\x1B[38;5;250m \x1B[39m\x1B[38;5;21;01mnp\x1B[39;00m\n' +
      '\x1B[1;32m      3\x1B[0m \x1B[38;5;28;01mimport\x1B[39;00m\x1B[38;5;250m \x1B[39m\x1B[38;5;21;01mpandas\x1B[39;00m\x1B[38;5;250m \x1B[39m\x1B[38;5;28;01mas\x1B[39;00m\x1B[38;5;250m \x1B[39m\x1B[38;5;21;01mpd\x1B[39;00m\n',
    "\x1B[0;31mModuleNotFoundError\x1B[0m: No module named 'matplotlib'"
  ]
}

Summary:

  • It cannot load libstdc++.so.6
  • Fails to get activated env vars for /nix/store/..../python3 for several times
  • Tries to read /home/~/.vscode/extensions/ms-toolsai.jupyter-2025.2.2025021801-linux-x64/temp/scripts/c92ae8434af0fb1d9ce4ed622ffb63975fcd2d438c0e258f2650fcc671695bda/jupyter/nbextensions, appending /home before ~/. Note: the path without leading /home exists and there is some stuff
  • Cannot find matplotlib, however I mentioned it in my shell.nix