Build-packages behavior when compiling through pip

To learn more about snap and container packaging in general, I’ve tried to package a Python application with C++ dependencies that would otherwise be tricky to redistribute in a portable way. One of the dependencies is dlib, which builds some binaries with cmake when you pip install it.

In my first attempt, I put all the build dependencies under build-packages. However, when installing dlib itself, compilation would fail because Python.h wasn’t found, while python3-dev and python-dev were installed as part of build-packages. When I used the --debug flag to run the build with the cleanbuild command and inspected the container when it failed, the headers were present in the appropriate /usr/include directory and I was also able to pip install dlib directly.

When I put the build dependencies under stage-packages instead of build-packages, the snapcraft build succeeded. But that feels like a hack, since I suppose most of those *-dev packages won’t be needed at run-time.

It looks like I might be able to filter those extra files out using a prime section but I’m wondering if there’s a better way to do it. Why aren’t the packages installed with build-packages visible during the installation of Python packages?

name: thug-memes # you probably want to 'snapcraft register <name>'
version: '0.1.0' # just for humans, typically '1.2+git' or '1.3.2'
summary: Command line Thug Meme generator written in Python # 79 char long summary
description: |
  Command line Thug Meme generator written in Python

grade: stable # must be 'stable' to release into candidate/stable channels
confinement: strict # use 'strict' once you have the right plugs and slots

apps:
  thug:
    command: thug
    plugs: [home, x11]

parts:
  thug-memes:
    plugin: python
    source: https://github.com/jerry-git/thug-memes
    source-type: git
    source-tag: '0.1.0'
    stage-packages:
      - libsm6
      - libxrender1
      - libxext6
  dlib:
    plugin: python
    stage-packages:
      - build-essential
      - cmake
      - libopenblas-dev
      - liblapack-dev
      - libx11-dev
      - libgtk-3-dev
      - python-dev
      - python3-dev
    python-packages:
      - numpy
      - dlib

environment:
  LC_ALL: C.UTF-8
  LANG: C.UTF-8