Skip to content

Instantly share code, notes, and snippets.

@abhillman
Last active November 3, 2023 04:53
Show Gist options
  • Save abhillman/1efa1d0498147b4dd0a9572e11abd373 to your computer and use it in GitHub Desktop.
Save abhillman/1efa1d0498147b4dd0a9572e11abd373 to your computer and use it in GitHub Desktop.
Wrapped version of Python 3.11 for allowing nix-managed python with IntelliJ/PyCharm
#!/bin/bash
# Use of python311 with dependencies, binaries, and other aspects of the environment with nix under IntelliJ.
# Instruct IntelliJ to use this script by specifying a path to this file as the "system" python interpreter.
# Make sure this file is named `python311` (IntelliJ may otherwise reject it) executable by running `chmod +x ...``.
#
# If a `shell.nix` file is located in the same path as this wrapper script, its contents will be used in addition to
# the dependencies listed below via `-p`.
#
# Copyright (C) 2032 Aryeh Hillman ([email protected])
# Permission to copy and modify is granted under the AGPLv3 license
# Last revised 11/2/2023
# Exit upon any failure.
set -euo pipefail
# This is a fix for Mac OS X when using the Python debugger in IntelliJ, which executes a script that is located
# in a path that has spaces in its name without the addition of quotes. Hypothesis as to why this merely happens
# here and not when using the cpython interpreter: arguments are passed via the `execve` syscall, which are unpacked
# in C by traversing argv, which is an array of strings. When working with `$@` in bash (the equivalent of `ARGV`),
# it is easier to just delegate to system python than to work with `ARGV` as an array, as arrays are finicky in bash.
CLEAN_ARGS_SCRIPT="import sys; print(' '.join(sys.argv[1:]).replace('Application Support', 'Application\\ Support'))"
CLEAN_ARGS="$(python3 -c "${CLEAN_ARGS_SCRIPT}" "${@}")"
# Fire up the nix shell
nix-shell \
-p 'python311' \
-p 'python311Packages.lxml' \
-p 'python311Packages.pip' \
-p 'python311Packages.setuptools' \
--run "python3.11 ${CLEAN_ARGS}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment