smaret
October 3, 2018, 8:09pm
1
I have setup a Python environnement in nix
that contains the several Python modules. The python
executable is a wrapper around the python
interpreter:
% less /Users/smaret/.nix-profile/bin/python
#! /nix/store/cblfnvb5rmhd2z231mqasn0brzh1hhv4-bash-4.4-p23/bin/bash -e
export PYTHONHOME='/nix/store/avqvqqqsrbrcpja4avvw9sfff2bfwm8c-python-2.7.15-env'
export PYTHONNOUSERSITE='true'
exec "/nix/store/s9mj8zzg7b0zj7s0k4rx3hdsiayr3ckn-python-2.7.15/bin/python" "${extraFlagsArray[@]}" "$@"
I would like to use this wrapper in a script shebang to make sure that this Python environnement is used when running the script:
% cat test.py
#!/Users/smaret/.nix-profile/bin/python
print("hello world")
However it seems that the shebang does not launch Python:
% ./test.py
./test.py: line 2: syntax error near unexpected token `"hello world"'
./test.py: line 2: `print("hello world")'
% /Users/smaret/.nix-profile/bin/python test.py
hello world
Is there a way to make the wrapper work in a shebang?
This looks like the same thing described in this issue:
opened 09:24PM - 18 Nov 15 UTC
0.kind: enhancement
1.severity: mass-rebuild
2.status: stale
While trying to package JRuby as a more ruby like package, I discovered that OSX⊠does not support shebangs at the kernel level.
Running `jruby` (which is a bash script that starts java) works fine on any unix.
Trying to run `gem` which has `jruby` in it's shebang:
```
â /nix/store/5cxwjdd1izi7gk0i3csl1ifb2y6m69p1-jruby-9.0.4.0/bin/gem
zsh: exec format error: /nix/store/5cxwjdd1izi7gk0i3csl1ifb2y6m69p1-jruby-9.0.4.0/bin/gem
```
And apparantly bash has a fallback mode where it interprets the script as a bash script:
```
building path(s) â/nix/store/d64p6aq2hm1f8xz5smd4d7blzvygi2jc-tzinfo-1.2.2.gemâ
unpacking sources
/nix/store/5cxwjdd1izi7gk0i3csl1ifb2y6m69p1-jruby-9.0.4.0/bin/gem: line 4: syntax error near unexpected token `('
/nix/store/5cxwjdd1izi7gk0i3csl1ifb2y6m69p1-jruby-9.0.4.0/bin/gem: line 4: `load File.join(File.dirname(__FILE__), 'jgem')'
builder for â/nix/store/nw9fwklp9jvp4bilvkhwcn45zgg51v1s-tzinfo-1.2.2.gem.drvâ failed with exit code 2
cannot build derivation â/nix/store/x99njf4xb10y21wbchj99667vfvwlyjh-fluentd-0.12.6.drvâ: 1 dependencies couldn't be built
error: build of â/nix/store/x99njf4xb10y21wbchj99667vfvwlyjh-fluentd-0.12.6.drvâ failed
```
### More details:
- http://www.in-ulm.de/~mascheck/various/shebang/
- See: âinterpreter itself as #! scriptâ
### Suggested fix
If jruby were a binary, it would probably work fine. Maybe `wrapProgram` can generate and compile a tiny C program that runs the wrapped script.
1 Like
smaret
October 3, 2018, 8:39pm
3
Indeed, this is the same problem. A workaround (suggested in the issue) is to prefix the wrapper in the sheband with !/usr/bin/env
:
% cat test.py
#!/usr/bin/env /Users/smaret/.nix-profile/bin/python
print("hello world")
% ./test.py
hello world
rkoe
October 3, 2018, 8:42pm
4
Here, a stripped-down version works (on NixOS):
$ cat /home/user/python
#!/bin/sh
exec "/nix/store/2k3108vcfni2ba5asf6yk4iz18v5q7lc-python-2.7.15/bin/python" "${extraFlagsArray[@]}" "$@"
$ cat test.py
#!/home/user/python
print("hello world")
$ ./test.py
hello world
But note that your wrapper may break when you upgrade your packages. I would recommend to create a Nix-package â either for your program or for your wrapper.
You may also be interested in using nix-shell as shebang: Introduction