Skip to content

Instantly share code, notes, and snippets.

@cdepillabout
Last active March 17, 2024 11:46
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save cdepillabout/f7dbe65b73e1b5e70b7baa473dafddb3 to your computer and use it in GitHub Desktop.
Save cdepillabout/f7dbe65b73e1b5e70b7baa473dafddb3 to your computer and use it in GitHub Desktop.
shell.nix for Python development using virtualenv and pip
let
nixpkgs-src = builtins.fetchTarball {
# master of 2021-01-05.
url = "https://github.com/NixOS/nixpkgs/archive/1a57d96edd156958b12782e8c8b6a374142a7248.tar.gz";
sha256 = "1qdh457apmw2yxbpi1biwl5x5ygaw158ppff4al8rx7gncgl10rd";
};
pkgs = import nixpkgs-src {
config = {
# allowUnfree may be necessary for some packages, but in general you should not need it.
allowUnfree = false;
};
};
# This is the Python version that will be used.
myPython = pkgs.python37;
pythonWithPkgs = myPython.withPackages (pythonPkgs: with pythonPkgs; [
# This list contains tools for Python development.
# You can also add other tools, like black.
#
# Note that even if you add Python packages here like PyTorch or Tensorflow,
# they will be reinstalled when running `pip -r requirements.txt` because
# virtualenv is used below in the shellHook.
ipython
pip
setuptools
virtualenvwrapper
wheel
]);
lib-path = with pkgs; lib.makeLibraryPath [
libffi
openssl
stdenv.cc.cc
# If you want to use CUDA, you should uncomment this line.
# linuxPackages.nvidia_x11
];
shell = pkgs.mkShell {
buildInputs = [
# my python and packages
pythonWithPkgs
# other packages needed for compiling python libs
pkgs.readline
pkgs.libffi
pkgs.openssl
# unfortunately needed because of messing with LD_LIBRARY_PATH below
pkgs.git
pkgs.openssh
pkgs.rsync
];
shellHook = ''
# Allow the use of wheels.
SOURCE_DATE_EPOCH=$(date +%s)
# Augment the dynamic linker path
export "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${lib-path}"
# Setup the virtual environment if it doesn't already exist.
VENV=.venv
if test ! -d $VENV; then
virtualenv $VENV
fi
source ./$VENV/bin/activate
export PYTHONPATH=`pwd`/$VENV/${myPython.sitePackages}/:$PYTHONPATH
'';
};
in
shell
@cdepillabout
Copy link
Author

Occasionally I do a little Python development. This shell.nix gives a shell with python and pip available for Python development. This can be used to install and run even complicated packages like PyTorch or Tensorflow.

The shellHook in shell.nix automatically puts you in a virtualenv, so you don't have to remember to do this every time.

@TanvirOnGH
Copy link

This is really useful. Thank you :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment