-
-
Notifications
You must be signed in to change notification settings - Fork 14.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
writers.writePython3: created script fails to run on MacOS #93609
Comments
that seems quite sad. Sadly I'm not able to reproduce it since I have to working MacOS installation. Did this work in the past for you? Or is it generally broken? |
thanks @veprbl @Lassulus as described in the issues liked above this is generally broken since I believe a good way to fix it is to use "${self.bash}/bin/bash ${py}/bin/python" as interpreter (but only if I'm happy to submit a PR |
additionally when |
this is the fix I'd go for: {
interpreter =
if libraries == []
then "${super.python3}/bin/python"
else if super.stdenv.isDarwin
then "${self.bash}/bin/bash ${py}/bin/python"
else "${py}/bin/python"
;
} |
On Darwin a script cannot be used as an interpreter in a shebang line, which causes scripts produced with makeScriptWriter (and its derivatives) to fail at run time if the used interpreter was wrapped with makeWrapper (as in the case of python3.withPackages). This commit fixes the problem by detecting if the interpreter is a script and prepending its shebang to the final interpreter line. For example if used interpreter is; ``` /nix/store/ynwv137n2650qy39swcflxbcygk5jwv1-python3-3.8.3-env/bin/python ``` which is a script with following shebang: ``` #! /nix/store/knd85yc7iwli8344ghav3zli8d9gril0-bash-4.4-p23/bin/bash -e ``` then the shebang line in the produced script will be ``` #! /nix/store/knd85yc7iwli8344ghav3zli8d9gril0-bash-4.4-p23/bin/bash -e /nix/store/ynwv137n2650qy39swcflxbcygk5jwv1-python3-3.8.3-env/bin/python ``` This works on Darwin since there does not seem to be a limit to the length of the shabang line and the shebang lines support multiple arguments to the interpreters (as opposed to linux where the kernel imposes a strict limit on shebang lengh and everything following the interpreter is passed to it as a single string). fixes; NixOS#93609 related to: NixOS#65351 NixOS#11133 (and probably a bunch of others) NOTE: scripts produced on platforms other tha Darwin will remain unmodified by this PR. However it might worth considering extending this fix to BSD systems in general. I didn't do it since I have no way of testing it on systems other than MacOS and linux.
…re needed `python.withPackages` has a runtime dependecy on bash since it's wrapped with `makeWrapper`. This fix avoids bash as runtime dependency when it's not needed. as discussed here: NixOS#93609 (comment)
* writers.makeScriptWriter: fix on Darwin\MacOS On Darwin a script cannot be used as an interpreter in a shebang line, which causes scripts produced with makeScriptWriter (and its derivatives) to fail at run time if the used interpreter was wrapped with makeWrapper (as in the case of python3.withPackages). This commit fixes the problem by detecting if the interpreter is a script and prepending its shebang to the final interpreter line. For example if used interpreter is; ``` /nix/store/ynwv137n2650qy39swcflxbcygk5jwv1-python3-3.8.3-env/bin/python ``` which is a script with following shebang: ``` #! /nix/store/knd85yc7iwli8344ghav3zli8d9gril0-bash-4.4-p23/bin/bash -e ``` then the shebang line in the produced script will be ``` #! /nix/store/knd85yc7iwli8344ghav3zli8d9gril0-bash-4.4-p23/bin/bash -e /nix/store/ynwv137n2650qy39swcflxbcygk5jwv1-python3-3.8.3-env/bin/python ``` This works on Darwin since there does not seem to be a limit to the length of the shabang line and the shebang lines support multiple arguments to the interpreters (as opposed to linux where the kernel imposes a strict limit on shebang lengh and everything following the interpreter is passed to it as a single string). fixes; #93609 related to: #65351 #11133 (and probably a bunch of others) NOTE: scripts produced on platforms other than Darwin will remain unmodified by this PR. However it might worth considering extending this fix to BSD systems in general. I didn't do it since I have no way of testing it on systems other than MacOS and linux. * writers.makeScriptWriter: fix typo in comment * writers.makeScriptWriter: fail build if interpreter of interpreter is a script
* writers.makeScriptWriter: fix on Darwin\MacOS On Darwin a script cannot be used as an interpreter in a shebang line, which causes scripts produced with makeScriptWriter (and its derivatives) to fail at run time if the used interpreter was wrapped with makeWrapper (as in the case of python3.withPackages). This commit fixes the problem by detecting if the interpreter is a script and prepending its shebang to the final interpreter line. For example if used interpreter is; ``` /nix/store/ynwv137n2650qy39swcflxbcygk5jwv1-python3-3.8.3-env/bin/python ``` which is a script with following shebang: ``` #! /nix/store/knd85yc7iwli8344ghav3zli8d9gril0-bash-4.4-p23/bin/bash -e ``` then the shebang line in the produced script will be ``` #! /nix/store/knd85yc7iwli8344ghav3zli8d9gril0-bash-4.4-p23/bin/bash -e /nix/store/ynwv137n2650qy39swcflxbcygk5jwv1-python3-3.8.3-env/bin/python ``` This works on Darwin since there does not seem to be a limit to the length of the shabang line and the shebang lines support multiple arguments to the interpreters (as opposed to linux where the kernel imposes a strict limit on shebang lengh and everything following the interpreter is passed to it as a single string). fixes; NixOS#93609 related to: NixOS#65351 NixOS#11133 (and probably a bunch of others) NOTE: scripts produced on platforms other than Darwin will remain unmodified by this PR. However it might worth considering extending this fix to BSD systems in general. I didn't do it since I have no way of testing it on systems other than MacOS and linux. * writers.makeScriptWriter: fix typo in comment * writers.makeScriptWriter: fail build if interpreter of interpreter is a script
This is still an issue for me. How to reproduce:
let
pkgs = import <nixpkgs> {};
python39 = pkgs.python39.withPackages(ps: [ ps.pip ]);
in pkgs.mkShell {
buildInputs = [ python39 ];
shellHook = ''
export PIP_PREFIX="$(pwd)/.build/pip"
export PYTHONPATH="$(pwd):$(pwd)/.build/pip/lib/python3.9/site-packages:$PYTHONPATH"
export PATH="$(pwd)/.build/pip/bin:$PATH"
'';
}
[nix-shell:~/my-project]$ pip install ipython
# Now, after installing ipython, let's try to execute it
[nix-shell:~/my-project]$ ipython
# /Users/tobias/my-project/.build/pip/bin/ipython: line 3: import: command not found
# /Users/tobias/my-project/.build/pip/bin/ipython: line 4: import: command not found
# from: can't read /var/mail/IPython
# /Users/tobias/my-project/.build/pip/bin/ipython: line 7: syntax error near unexpected token `('
# /Users/tobias/my-project/.build/pip/bin/ipython: line 7: ` sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])' Looking inside #!/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()) |
@bergkvist Please file a new issue. |
Describe the bug
Executables produced by
writers.writePython3
(and probably also writePython2 etc) fail to run on MacOSTo Reproduce
Steps to reproduce the behavior:
$(nix-build)
from top level the repo (assuming POSIX shell)Expected behavior
The command from step 2 should print
success
Additional context
This seems to be caused by the fact that
${python3.withPackages}/bin/python
which is used for shebang linein the produced script is itself a "script text executable" which does not seem to work on MacOS.
The repo mentioned above contains a tentative fix, which prepend
${pkgs.bash}/bin/bash
to the shebang line in the produced script.NOTE: the repo above uses my own fork of nixpkgs since dash is currently broken on darwin (and
writePython3
depends on dash)Notify maintainers
@Lassulus
Metadata
"x86_64-darwin"
Darwin 18.7.0, macOS 10.14.6
no
yes
nix-env (Nix) 2.3.6pre7027_6f162d8
""
/nix/store/ciqd3l5bznq5ms389b4w7yjv7vk9vpm0-source
Maintainer information:
The text was updated successfully, but these errors were encountered: