Skip to content

MacOS: Python shebang issue #123067

@bergkvist

Description

@bergkvist

Describe the bug
On MacOS, nested shebangs/shebangs pointing at scripts are not allowed. When using pkgs.python39.withPackages(...) and installing packages with pip into a sandbox environment, binary executables get a shebang that makes them unable to execute.

To Reproduce

  1. Create shell.nix, and run nix-shell:
let
  pkgs = import <nixpkgs> {};
  python = pkgs.python39.withPackages(ps: [ ps.pip ]);
in pkgs.mkShell {
  buildInputs = [ python ];
  shellHook = ''
    export PIP_DISABLE_PIP_VERSION_CHECK=1
    export PIP_PREFIX="$(pwd)/_build/pip"
    export PYTHONPATH="$PIP_PREFIX/lib/python3.9/site-packages:$PYTHONPATH"
    export PATH="$PIP_PREFIX/bin:$PATH"
  '';
}
  1. Install ipython with pip, and try to execute it.
[nix-shell]$ pip install ipython
[nix-shell]$ ipython

/Users/tobias/nix-python-issue/_build/pip/bin/ipython: line 3: import: command not found
/Users/tobias/nix-python-issue/_build/pip/bin/ipython: line 4: import: command not found
from: can't read /var/mail/IPython
/Users/tobias//nix-python-issue/_build/pip/bin/ipython: line 7: syntax error near unexpected token `('
/Users/tobias//nix-python-issue/_build/pip/bin/ipython: line 7: `    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])'

Expected behavior
It should be possible to do $ pip install ipython, followed by $ ipython - and have ipython actually execute successfully.

Observations

The shebang in _build/pip/bin/ipython points to a script, which is not allowed on MacOS (only Linux):

[nix-shell]$ cat $PIP_PREFIX/bin/ipython
#!/nix/store/i46k148mi830riq4wxh49ki8qmq0731k-python3-3.9.2-env/bin/python3.9
# -*- coding: utf-8 -*-
import re
import sys
from IPython import start_ipython
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(start_ipython())

If /nix/store/i46k148mi830riq4wxh49ki8qmq0731k-python3-3.9.2-env/bin/python3.9 was an executable, everything would be fine. But since this is a script, with its own shebang, this causes issues:

[nix-shell]$ cat /nix/store/i46k148mi830riq4wxh49ki8qmq0731k-python3-3.9.2-env/bin/python3.9
#! /nix/store/ra8yvijdfjcs5f66b99gdjn86gparrbz-bash-4.4-p23/bin/bash -e
export NIX_PYTHONPREFIX='/nix/store/i46k148mi830riq4wxh49ki8qmq0731k-python3-3.9.2-env'
export NIX_PYTHONEXECUTABLE='/nix/store/i46k148mi830riq4wxh49ki8qmq0731k-python3-3.9.2-env/bin/python3.9'
export NIX_PYTHONPATH='/nix/store/i46k148mi830riq4wxh49ki8qmq0731k-python3-3.9.2-env/lib/python3.9/site-packages'
export PYTHONNOUSERSITE='true'
exec "/nix/store/7pjbbmnrch7frgyp7gz19ay0z1173c7y-python3-3.9.2/bin/python3.9"  "$@"

Metadata

  • system: "x86_64-darwin"
  • host os: Darwin 20.3.0, macOS 10.16
  • multi-user?: no
  • sandbox: no
  • version: nix-env (Nix) 2.3.10
  • channels(tobias): "darwin, nixpkgs-21.05pre284563.ab6943a7450"
  • nixpkgs: /Users/tobias/.nix-defexpr/channels/nixpkgs

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: bugSomething is broken2.status: stalehttps://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md6.topic: darwinRunning or building packages on Darwin6.topic: pythonPython is a high-level, general-purpose programming language.
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions