|
| 1 | +{ |
| 2 | + nix, |
| 3 | + nix-pyenv-directory, |
| 4 | + pyenv, |
| 5 | + usingPython, |
| 6 | + persist, |
| 7 | + inputDerivation ? "", |
| 8 | + ... |
| 9 | +}: |
| 10 | +let |
| 11 | + optionalCommentOut = if persist then "" else "#"; |
| 12 | + sitePackages = usingPython.sitePackages; |
| 13 | +in |
| 14 | +'' |
| 15 | + if [[ $name == nix-shell ]]; then |
| 16 | + cd ${builtins.toString ./.} |
| 17 | + fi |
| 18 | +
|
| 19 | + # ensure the nix-pyenv directory exists |
| 20 | + if [[ ! -d ${nix-pyenv-directory} ]]; then mkdir ${nix-pyenv-directory}; fi |
| 21 | + if [[ ! -d ${nix-pyenv-directory}/lib ]]; then mkdir ${nix-pyenv-directory}/lib; fi |
| 22 | + if [[ ! -d ${nix-pyenv-directory}/bin ]]; then mkdir ${nix-pyenv-directory}/bin; fi |
| 23 | +
|
| 24 | + ensure_symlink() { |
| 25 | + local link_path="$1" |
| 26 | + local target_path="$2" |
| 27 | + if [[ -L "$link_path" ]] && [[ "$(readlink "$link_path")" = "$target_path" ]]; then |
| 28 | + return 0 |
| 29 | + fi |
| 30 | + rm -f "$link_path" > /dev/null 2>&1 |
| 31 | + ln -s "$target_path" "$link_path" |
| 32 | + } |
| 33 | +
|
| 34 | + # creating python library symlinks |
| 35 | + for file in ${pyenv}/${sitePackages}/*; do |
| 36 | + basefile=$(basename $file) |
| 37 | + if [ -d "$file" ]; then |
| 38 | + if [[ "$basefile" != *dist-info && "$basefile" != __pycache__ ]]; then |
| 39 | + ensure_symlink "${nix-pyenv-directory}/lib/$basefile" $file |
| 40 | + fi |
| 41 | + else |
| 42 | + # the typing_extensions.py will make the vscode type checker not working! |
| 43 | + if [[ $basefile == *.so ]] || ([[ $basefile == *.py ]] && [[ $basefile != typing_extensions.py ]]); then |
| 44 | + ensure_symlink "${nix-pyenv-directory}/lib/$basefile" $file |
| 45 | + fi |
| 46 | + fi |
| 47 | + done |
| 48 | + for file in ${nix-pyenv-directory}/lib/*; do |
| 49 | + if [[ -L "$file" ]] && [[ "$(dirname $(readlink "$file"))" != "${pyenv}/${sitePackages}" ]]; then |
| 50 | + rm -f "$file" |
| 51 | + fi |
| 52 | + done |
| 53 | +
|
| 54 | + # ensure the typing_extensions.py is not in the lib directory |
| 55 | + rm ${nix-pyenv-directory}/lib/typing_extensions.py > /dev/null 2>&1 |
| 56 | +
|
| 57 | + # add python executable to the bin directory |
| 58 | + ensure_symlink "${nix-pyenv-directory}/bin/python" ${pyenv}/bin/python |
| 59 | + export PATH=${nix-pyenv-directory}/bin:$PATH |
| 60 | +
|
| 61 | + ${optionalCommentOut} ${nix}/bin/nix-store --add-root ${nix-pyenv-directory}/.nix-shell-inputs --realise ${inputDerivation} |
| 62 | +'' |
0 commit comments