The magic-launch launcher: Fix file type detection based on libmagic in the snap runtime

This launcher that fixes libmagic file type autodetection compatibility in snaps.

How to use

  1. Merge the following part definition to your Snapcraft recipe:

    parts:
      # Launcher for fixing libmagic applications
      # https://forum.snapcraft.io/t/the-magic-launch-launcher-fix-file-type-detection-based-on-libmagic-in-the-snap-runtime/10442
      magic-launch:
        source: https://github.com/Lin-Buo-Ren/magic-launch.git
        source-tag: v2.0.0
        plugin: dump
        stage:
          - bin/*
    
  2. In the apps stanza, insert bin/magic-launch into the command chain:

    apps:
      _app_name_:
        # The command to run the application, the value should be a
        # *relative path* to an executable file rooted from the `prime` directory
        command: bin/magic-launch "${SNAP}"/bin/_app_command_
    

    if you’re using the full adapter:

    apps:
      _app_name_:
        # The environment adapter style to use, `command-chain` is only supported
        # by the `full` adapter
        adapter: full
    
        # The command to run the application, the value should be a
        # *relative path* to an executable file rooted from the `prime` directory
        command: bin/_app_command_
        command-chain:
          - bin/magic-launch
    

The implementation

Snaps that use this stage snap

References

file(1) manual page


Happy snapcrafting!

1 Like

Interesting. In a python3 snap (the review-tools) I use:

apps:
  foo:
    environment:
      MAGIC: $SNAP/usr/share/misc/magic
...
parts:
  bar:
    plugin: nil
    ...
    install: ./snap-fixup.sh
    ...
    stage-packages:
    - file
    - libmagic1
...

Where snap-fixup.sh has:

#!/bin/sh
...
SNAPDIR="$(pwd)/../install"
echo "Symlinking libmagic.so"
libmagic=$(find "$SNAPDIR"/usr/lib -name libmagic.so.1.0.0)
cd $(dirname "$libmagic")
ln -s libmagic.so.1.0.0 libmagic.so
cd -
...

This ‘works’ but isn’t exactly beautiful :slight_smile: Curious if you tried your part with python3 (import magic)? Either way, I’ll be taking a look at this. Thanks!

1 Like

It works:

$ test-magic 
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import magic
>>> 
name: test-magic
base: core18
summary: ' '
description: ' ' 
version: '0.1'

grade: stable
confinement: strict

parts:
  magic-launch:
    plugin: nil
    stage-snaps:
    - magic-launch

  main:
    source: .
    plugin: dump
    stage-packages:
    - python3-magic
    - libmagic1
    organize:
      'test-magic.bash': bin/

apps:
  test-magic:
    adapter: full
    command: bin/test-magic.bash
    command-chain:
    - bin/magic-launch

#!/usr/bin/env bash
python3