Skip to content

Python script packaged with (python3.withPackages ...) as build input not working on Darwin (shebang issue?) #65351

@anderslundstedt

Description

@anderslundstedt

Describe the bug
If I package a Python script with python3.withPackages ... as build input then the script will build but not run on Darwin.

To Reproduce
Steps to reproduce the behavior:

  1. Use MacOS.
  2. Create default.nix:
let
  pkgs = import <nixpkgs> { };
  python = pkgs.python3.withPackages (_: []);
in
  pkgs.stdenv.mkDerivation rec {
    name = "py-test";
    buildInputs = [ python ];
    unpackPhase = "true";
    installPhase = ''
      mkdir -p $out/bin
      touch $out/bin/py-test
      chmod +x $out/bin/py-test
      echo "#!${python}/bin/python" >> $out/bin/py-test
      echo "print(0)" >> $out/bin/py-test
    '';
  }
  1. Build and run:
nix-build
./result/bin/py-test
  1. Observe unexpected behaviour.
  • In bash:
./result/bin/py-test: line 2: syntax error near unexpected token `0'
./result/bin/py-test: line 2: `print(0)'
  • In zsh:
zsh: exec format error: ./result/bin/py-test

Expected behavior
Output:

0

Additional context
Everything works as expected if we replace pkgs.python3.withPackages ... with pkgs.python3. The only difference in the result is the shebang.

  • ./result/bin/py-test with pkgs.python3.withPackages ...:
#!/nix/store/jhbikg6nir868g3wfypi7vppn5mbjhp5-python3-3.7.4-env/bin/python
print(0)
  • ./result/bin/py-test with pkgs.python3:
#!/nix/store/8r52cdbn9g3sl9pz8vm4xcf7rhrqjqin-python3-3.7.4/bin/python
print(0)

Executing the python executable in the shebang works, as does executing py-test with the same executable, that is

/nix/store/jhbikg6nir868g3wfypi7vppn5mbjhp5-python3-3.7.4-env/bin/python result/bin/py-test

gives the expected output.

Existing Darwin shebang-related issues:

However, it does not seem like a problem with the character limit in this case?

Metadata
Please run nix run nixpkgs.nix-info -c nix-info -m and paste the result.

 - system: `"x86_64-darwin"`
 - host os: `Darwin 16.7.0, macOS 10.12.6`
 - multi-user?: `no`
 - sandbox: `no`
 - version: `nix-env (Nix) 2.2.2`
 - channels(anders): `""`
 - nixpkgs: `/Users/anders/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.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions