Wxpython needs access to screen

Hi all,

I’m testing Nix with MacOs. I’m using nix-shell with mach-nix to create a python env with wxPython and other python libs. It’s building correctly the env but when I try to run a code using wxpython I see this error:

This program needs access to the screen. Please run with a Framework
build of python, and only when you are logged in on the main display
of your Mac.

Is there a way to fix this problem?

I think this problem is because Python in Nix doesn’t the Framework part. Is there a way to have it in Nix?

What is your script/Os info?


on Linux

the minimal example works:
nix-shell -p nixFlakes --run "nix run github:davhau/mach-nix#with.ipython.wxpython --show-trace "

import wx; a=wx.App(); wx.Frame(None, title="Hello World").Show(); a.MainLoop()

My OS is Mac OS X 10.13.6. If I run your python commands it shows this message:

This program needs access to the screen. Please run with a Framework
build of python, and only when you are logged in on the main display
of your Mac.

It’s because it needs a python compile with Framework enabled. I’m trying with this shell.nix where I’m overlaying python to enable framework:

let
  sources = import ./nix/sources.nix;
  pkgs = import sources.nixpkgs {};
  my_pkgs = import sources.nixpkgs {
    overlays = [
       (
         final: prev: {
           python38 = prev.python38.overrideAttrs (oldAttrs: rec{
             configureFlags = [
               "--enable-framework=${placeholder "out"}/Framework/"
               "--with-threads"
               "--without-ensurepip"
               "--with-system-expat"
               "--with-system-ffi"
               "--with-openssl=${prev.openssl.dev}"
               "--prefix=${placeholder "out"}"
               "--datarootdir=${placeholder "out"}/share"
               "--datadir=${placeholder "out"}/share"
               "--libdir=${placeholder "out"}/lib"
               "--includedir=${placeholder "out"}/include"
               #"--bindir=${placeholder "out"}/bin"
             ];
             nativeBuildInputs = oldAttrs.nativeBuildInputs ++ [
               prev.darwin.DarwinTools
             ];
             buildInputs = oldAttrs.buildInputs ++ [
               pkgs.darwin.apple_sdk.frameworks.Foundation
               pkgs.darwin.apple_sdk.frameworks.Cocoa
             ];
             installPhase = ''
                make install PYTHONAPPSDIR=${placeholder "out"}
                ln -s ${placeholder "out"}/Framework/Python.framework/Versions/3.8/lib/*.dylib ${placeholder "out"}/lib/
                ln -s ${placeholder "out"}/Framework/Python.framework/Versions/3.8/lib/python3.8/* ${placeholder "out"}/lib/python3.8/

             '';
           });
         }
       )
    ];
   };
  mach-nix = import (builtins.fetchGit {
    url = "https://github.com/DavHau/mach-nix/";
    ref = "refs/tags/3.1.1";
  }) {
    pkgs = my_pkgs;
    python = "python38";
  };
  customPython = mach-nix.mkPython {
    requirements = ''
      pillow
      wxpython==4.1.0
      numpy
      scipy
      scikit-image
      scikit-learn
      imageio
      h5py==2.10.0
      pyserial
      psutil
      nibabel
      configparser
      PyPubsub
      vtk
      gdcm
      plaidml
      plaidml-keras
      cython
      ipython
      Theano
    '';

    _.enum34.phases = "installPhase";
    _.enum34.installPhase = "mkdir $out";

    _.plaidml.pipInstallFlags = "--no-deps";
    _.plaidml-keras.pipInstallFlags = "--no-deps";

    _.gdcm.cmakeFlags = [
      "-DGDCM_BUILD_APPLICATIONS=OFF"
      "-DGDCM_BUILD_SHARED_LIBS=ON"
      "-DGDCM_USE_VTK=OFF"
      "-DGDCM_WRAP_PYTHON:BOOL=ON"
      "-DGDCM_INSTALL_PYTHONMODULE_DIR=${placeholder "out"}/lib/python3.8/site-packages/"
    ];
  };
in pkgs.mkShell {
  name = "invesalius";
  buildInputs = with pkgs; [
    customPython
  ];
}

It’s compiling python but it’s not compiling libxml2, maybe I need to change more things yet.

I did some progress, now it’s compiling much more things. Unfortunately, my Macbook is old (last Macbook white released), so it take a lot of time to compile the things.

let
  sources = import ./nix/sources.nix;
  pkgs = import sources.nixpkgs {};
  my_pkgs = import sources.nixpkgs {
    overlays = [
       (
         final: prev: {
           python38 = prev.python38.overrideAttrs (oldAttrs: rec{
             
             configureFlags = prev.lib.remove "--enable-shared"  oldAttrs.configureFlags ++ [
               "--enable-framework=${placeholder "out"}/Framework/"
               "--datarootdir=${placeholder "out"}/share"
               "--datadir=${placeholder "out"}/share"
               "--libdir=${placeholder "out"}/lib"
               "--includedir=${placeholder "out"}/include"
               #"--bindir=${placeholder "out"}/bin"
             ];

             nativeBuildInputs = oldAttrs.nativeBuildInputs ++ [
               prev.darwin.DarwinTools
             ];
             buildInputs = oldAttrs.buildInputs ++ [
               pkgs.darwin.apple_sdk.frameworks.Foundation
               pkgs.darwin.apple_sdk.frameworks.Cocoa
             ];

             prePatch = oldAttrs.prePatch + ''
              substituteInPlace configure --replace '/Applications' '${placeholder "out"}/Applications'
             '';

             postInstall = ''
                ln -s ${placeholder "out"}/Framework/Python.framework/Versions/3.8/lib/*.dylib ${placeholder "out"}/lib/
                ln -s ${placeholder "out"}/Framework/Python.framework/Versions/3.8/lib/python3.8/* ${placeholder "out"}/lib/python3.8/
                ln -s ${placeholder "out"}/include/python3.8/* ${placeholder "out"}/Framework/Python.framework/Versions/3.8/include/python3.8/
                ln -s ${placeholder "out"}/Framework/Python.framework/Versions/3.8/include/python3.8/pyconfig.h ${placeholder "out"}/include/python3.8/ 
             '' + oldAttrs.postInstall;
           });
         }
       )
    ];
   };
  mach-nix = import (builtins.fetchGit {
    url = "https://github.com/DavHau/mach-nix/";
    ref = "refs/tags/3.1.1";
  }) {
    pkgs = my_pkgs;
    python = "python38";
  };
  customPython = mach-nix.mkPython {
    requirements = ''
      pillow
      wxpython==4.1.0
      numpy
      scipy
      scikit-image
      scikit-learn
      imageio
      h5py==2.10.0
      pyserial
      psutil
      nibabel
      configparser
      PyPubsub
      vtk
      gdcm
      plaidml
      plaidml-keras
      cython
      ipython
      Theano
    '';

    _.enum34.phases = "installPhase";
    _.enum34.installPhase = "mkdir $out";

    _.plaidml.pipInstallFlags = "--no-deps";
    _.plaidml-keras.pipInstallFlags = "--no-deps";

    _.gdcm.cmakeFlags = [
      "-DGDCM_BUILD_APPLICATIONS=OFF"
      "-DGDCM_BUILD_SHARED_LIBS=ON"
      "-DGDCM_USE_VTK=OFF"
      "-DGDCM_WRAP_PYTHON:BOOL=ON"
      "-DGDCM_INSTALL_PYTHONMODULE_DIR=${placeholder "out"}/lib/python3.8/site-packages/"
    ];
  };
in pkgs.mkShell {
  name = "invesalius";
  buildInputs = with pkgs; [
    customPython
  ];
}