Skip to content

Instantly share code, notes, and snippets.

@GeorgesAlkhouri
Created August 19, 2022 06:59
Show Gist options
  • Select an option

  • Save GeorgesAlkhouri/d75f5256188483602b96a7aff20c02c3 to your computer and use it in GitHub Desktop.

Select an option

Save GeorgesAlkhouri/d75f5256188483602b96a7aff20c02c3 to your computer and use it in GitHub Desktop.
Python development environment via nix-shell and pip
{pkgs ? import <nixpkgs> {}}: let
myPython = pkgs.python37;
pythonWithPkgs = myPython.withPackages (pythonPkgs:
with pythonPkgs; [
pip
setuptools
wheel
]);
lib-path = with pkgs;
lib.makeLibraryPath [
libffi
openssl
stdenv.cc.cc
zlib
];
venvDir = "./.venv";
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}"
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
${myPython.interpreter} -m venv "${venvDir}"
fi
PYTHONPATH=$PWD/${venvDir}/${myPython.sitePackages}/:$PYTHONPATH
source "${venvDir}/bin/activate"
'';
};
in
shell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment