Last active
July 12, 2024 07:24
-
-
Save blakat360/90494cb321accd09fb6afbd58aa6e80d to your computer and use it in GitHub Desktop.
Incremental python shell.nix (assumes a local install of nix-ld if using NixOS)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with import <nixpkgs> { }; | |
let | |
venvDir = "./.venv"; | |
pythonPackages = python39Packages; | |
NIX_LD_LIBRARY_PATH = lib.makeLibraryPath [ | |
stdenv.cc.cc | |
openssl | |
]; | |
NIX_LD = lib.fileContents "${stdenv.cc}/nix-support/dynamic-linker"; | |
# potentially dodgy, it's way safer to make wrapper scripts which set this for all the commands that need it | |
LD_LIBRARY_PATH=NIX_LD_LIBRARY_PATH; | |
in pkgs.mkShell { | |
name = "impurePythonEnv"; | |
buildInputs = [ | |
stdenv.cc.cc.lib | |
sqlitebrowser | |
pam | |
pythonPackages.python | |
# Needed when using python 2.7 | |
# pythonPackages.virtualenv | |
# ... | |
]; | |
inherit NIX_LD_LIBRARY_PATH NIX_LD LD_LIBRARY_PATH; | |
# This is very close to how venvShellHook is implemented, but | |
# adapted to use 'virtualenv' | |
shellHook = '' | |
SOURCE_DATE_EPOCH=$(date +%s) | |
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" | |
''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment