Skip to content

Instantly share code, notes, and snippets.

@stefanDeveloper
Created March 21, 2023 09:27
Show Gist options
  • Save stefanDeveloper/0df5e674a449fe11942b52544c8e6cdf to your computer and use it in GitHub Desktop.
Save stefanDeveloper/0df5e674a449fe11942b52544c8e6cdf to your computer and use it in GitHub Desktop.
Jupyter Notebook Nix Shell
with import <nixpkgs> { };
pkgs.mkShell rec {
venvDir = "./.venv";
buildInputs = [
# Defines a python + set of packages.
(python3.withPackages (ps: with python3Packages; [
jupyter
ipython
ipykernel
setuptools
virtualenv # run virtualenv .
pip
pyqt5 # avoid installing via pip
pandas
numpy
matplotlib
scipy
]))
];
# Automatically run jupyter when entering the shell.
shellHook = ''
# fixes libstdc++ issues and libgl.so issues
LD_LIBRARY_PATH=${stdenv.cc.cc.lib}/lib/:/run/opengl-driver/lib/
# fixes xcb issues :
QT_PLUGIN_PATH=${qt5.qtbase}/${qt5.qtbase.qtPluginPrefix}
SOURCE_DATE_EPOCH=$(date +%s)
QT_XCB_GL_INTEGRATION="none"
if [ -d "${venvDir}" ]; then
echo "Skipping venv creation, '${venvDir}' already exists"
else
echo "Creating new venv environment in path: '${venvDir}'"
# Note that the module venv was only introduced in python 3, so for 2.7
# this needs to be replaced with a call to virtualenv
${pythonPackages.python.interpreter} -m venv "${venvDir}"
fi
# Under some circumstances it might be necessary to add your virtual
# environment to PYTHONPATH, which you can do here too;
PYTHONPATH=$PWD/${venvDir}/${pythonPackages.python.sitePackages}/:$PYTHONPATH
source "${venvDir}/bin/activate"
# Updating pip
python -m pip install --upgrade pip
python -m ipykernel install --user --name=${venvDir}
jupyter notebook --ip 127.0.0.1 --port 8888 --no-browser --allow-root
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment