YAML code sharing and reuse


#1

At the moment it is difficult to find and use the sometimes arcane recipes that people use to build their software. What we need is a way to share code and solutions between projects. To that end I created sc-jsonnet which is based on the jsonnet project to generate your snapcraft.yaml. Jsonnet is a superset of JSON that allows for some program-ability when generating your yaml.

Let’s take an example of gnome-twitch, which is a snap I maintain. Here is the old yaml (173 lines):

name: gnome-twitch
adopt-info: gnome-twitch
version: '0.4.2'
summary: Enjoy Twitch on your GNU/Linux desktop
description: |
  Enjoy Twitch on your GNU/Linux desktop
  
  Easily browse and search channels and games. Keep track of your follows
  whether you have a Twitch account or not.
  A super fast, hardware-accelerated player plus a fully integrated native
  chat makes for a fantastic viewing experience.
  GNOME Twitch is packed with cool features and development is constantly
  on-going, bringing heaps of new stuff straight to you.

grade: stable
confinement: strict
base: core18

architectures:
- build-on: amd64
- build-on: i386

slots:
  dbus-gnome-twitch:
    interface: dbus
    bus: session
    name: com.vinszent.GnomeTwitch

plugs:
  gtk-3-themes:
    interface: content
    target: $SNAP/data-dir/themes
    default-provider: gtk-common-themes:gtk-3-themes
  icon-themes:
    interface: content
    target: $SNAP/data-dir/icons
    default-provider: gtk-common-themes:icon-themes
  sound-themes:
    interface: content
    target: $SNAP/data-dir/sounds
    default-provider: gtk-common-themes:sounds-themes
  gnome-3-28-1804:
    interface: content
    target: $SNAP/gnome-platform
    default-provider: gnome-3-28-1804:gnome-3-28-1804

layout:
  /usr/lib/$SNAPCRAFT_ARCH_TRIPLET/webkit2gtk-4.0:
    symlink: $SNAP/gnome-platform/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/webkit2gtk-4.0
  /usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib:
    symlink: $SNAP/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/alsa-lib
  /etc/asound.conf:
    symlink: $SNAP/etc/asound.conf

apps:
  gnome-twitch:
    command: desktop-launch samba-launch snapcraft-preload $SNAP/usr/bin/gnome-twitch
    desktop: usr/share/applications/com.vinszent.GnomeTwitch.desktop
    common-id: com.vinszent.GnomeTwitch.desktop
    plugs:
    - browser-support
    - desktop
    - gsettings
    - network
    - opengl
    - pulseaudio
    - screen-inhibit-control
    - unity7
    - wayland
    - x11

parts:
  desktop-gnome-platform:
    source: https://github.com/ubuntu/snapcraft-desktop-helpers.git
    source-subdir: gtk
    plugin: make
    make-parameters: ["FLAVOR=gtk3"]
    build-packages:
      - gcc
    override-build: |
      snapcraftctl build
      mkdir -pv $SNAPCRAFT_PART_INSTALL/gnome-platform

  alsa:
    plugin: nil
    source: https://github.com/diddledan/snapcraft-alsa.git
    override-pull: |
      cat > asound.conf <<EOF
      pcm.!default {
        type pulse
        fallback "sysdefault"
        hint {
          show on
          description "Default ALSA Output (currently PulseAudio Sound Server)"
        }
      }
      ctl.!default {
        type pulse
        fallback "sysdefault"
      }
      EOF
    override-build: |
      install -m644 -D -t $SNAPCRAFT_PART_INSTALL/etc asound.conf
    build-packages:
    - libasound2-dev
    stage-packages:
    - libasound2
    - libasound2-plugins
    
  samba-launch:
    plugin: nil
    override-pull: |
      cat > samba-launch <<EOF
      #!/bin/bash
      export LD_LIBRARY_PATH="\$SNAP/usr/lib/\$SNAP_LAUNCHER_ARCH_TRIPLET/samba:\$LD_LIBRARY_PATH"
      exec "\$@"
      EOF
    override-build:
      install -m755 -D -t $SNAPCRAFT_PART_INSTALL/bin samba-launch

  snapcraft-preload:
    source: https://github.com/sergiusens/snapcraft-preload.git
    plugin: cmake
    build-packages:
    - gcc-multilib
    - g++-multilib

  gnome-twitch:
    plugin: meson
    source: https://github.com/vinszent/gnome-twitch.git
    source-tag: 'v0.4.2'
    parse-info: [usr/share/appdata/com.vinszent.GnomeTwitch.appdata.xml]
    override-pull: |
      pip3 install meson
      snapcraftctl pull
      sed -i 's|^Icon=.*|Icon=/usr/share/icons/hicolor/512x512/apps/com.vinszent.GnomeTwitch.png|'  data/com.vinszent.GnomeTwitch.desktop.in
    meson-parameters:
    - --buildtype=release
    - --prefix=/usr
    - -Dbuild-player-backends=gstreamer-cairo,gstreamer-opengl,gstreamer-clutter,mpv-opengl
    - -Ddo-post-install=false
    build-packages:
    - gettext
    - gobject-introspection
    - libclutter-gst-3.0-dev
    - libclutter-gtk-1.0-dev
    - libdb5.3-dev
    - libglib2.0-dev
    - libglu1-mesa-dev
    - libjson-glib-dev
    - libmpv-dev
    - libpeas-dev
    - libpulse-dev
    - libsoup2.4-dev
    - libwebkit2gtk-4.0-dev
    - python3-pip
    - python3-setuptools
    - python3-wheel
    stage-packages:
    - gstreamer1.0-clutter-3.0
    - gstreamer1.0-gtk3
    - gstreamer1.0-libav
    - gstreamer1.0-plugins-base
    - gstreamer1.0-plugins-good
    - gstreamer1.0-plugins-bad
    - libglu1-mesa
    - libgpm2
    - libjson-glib-1.0-0
    - libmpv1
    - libslang2
    - samba-libs
    after:
    - desktop-gnome-platform

And here is the equivalent in jsonnet (122 lines ~30% smaller):

local snapcraft = import 'snapcraft.libsonnet';
local alsa = import 'https://raw.githubusercontent.com/diddlesnaps/snapcraft-alsa/cfbd336/alsa.libsonnet';
local preload = import 'https://raw.githubusercontent.com/diddlesnaps/snapcraft-preload/jsonnet/snapcraft-preload.libsonnet';
local desktop = import 'https://raw.githubusercontent.com/diddlesnaps/snapcraft-desktop-helpers/jsonnet/desktop-helpers.libsonnet';

snapcraft {
    name: "gnome-twitch",
    "adopt-info": "gnome-twitch",
    version: "0.4.2",
    summary: "Enjoy Twitch on your GNU/Linux desktop",
    description: "Enjoy Twitch on your GNU/Linux desktop
Easily browse and search channels and games. Keep track of your follows whether you have a Twitch account or not. A super fast, hardware-accelerated player plus a fully integrated native chat makes for a fantastic viewing experience. GNOME Twitch is packed with cool features and development is constantly on-going, bringing heaps of new stuff straight to you.
",

    grade: "stable",
    confinement: "strict",
    base: "core18",

    architectures:[
        { "build-on": "amd64" },
        { "build-on": "i386" },
    ],

    slots: {
        "dbus-gnome-twitch": {
            interface: "dbus",
            bus: "session",
            name: "com.vinszent.GnomeTwitch",
        },
    },

    layout: {
        "/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/webkit2gtk-4.0": {
            symlink: "$SNAP/gnome-platform/usr/lib/$SNAPCRAFT_ARCH_TRIPLET/webkit2gtk-4.0",
        },
    },

    apps: {
        "gnome-twitch": {
            command: "bin/samba-launch $SNAP/usr/bin/gnome-twitch",
            desktop: "usr/share/applications/com.vinszent.GnomeTwitch.desktop",
            "common-id": "com.vinszent.GnomeTwitch.desktop",
            plugs: [
                "browser-support",
                "desktop",
                "gsettings",
                "network",
                "opengl",
                "pulseaudio",
                "screen-inhibit-control",
                "unity7",
                "wayland",
                "x11",
            ],
        },
    },

    parts: {
        "samba-launch": {
            plugin: "nil",
            "override-pull": "
cat > samba-launch <<EOF
#!/bin/bash
export LD_LIBRARY_PATH=\"\\$SNAP/usr/lib/\\$SNAP_LAUNCHER_ARCH_TRIPLET/samba:\\$LD_LIBRARY_PATH\"
exec \"\\$@\"
EOF
",
                "override-build": "install -m755 -D -t $SNAPCRAFT_PART_INSTALL/bin samba-launch",
        },

        "gnome-twitch": {
            plugin: "meson",
            source: "https://github.com/vinszent/gnome-twitch.git",
            "source-tag": "v0.4.2",
            "parse-info": ["usr/share/appdata/com.vinszent.GnomeTwitch.appdata.xml"],
            "override-pull": "
pip3 install meson
snapcraftctl pull
sed -i 's|^Icon=.*|Icon=/usr/share/icons/hicolor/512x512/apps/com.vinszent.GnomeTwitch.png|'  data/com.vinszent.GnomeTwitch.desktop.in
",
            "meson-parameters": [
                "--buildtype=release",
                "--prefix=/usr",
                "-Dbuild-player-backends=gstreamer-cairo,gstreamer-opengl,gstreamer-clutter,mpv-opengl",
                "-Ddo-post-install=false",
            ],
            "build-packages": [
                "gettext",
                "gobject-introspection",
                "libclutter-gst-3.0-dev",
                "libclutter-gtk-1.0-dev",
                "libdb5.3-dev",
                "libglib2.0-dev",
                "libglu1-mesa-dev",
                "libjson-glib-dev",
                "libmpv-dev",
                "libpeas-dev",
                "libpulse-dev",
                "libsoup2.4-dev",
                "libwebkit2gtk-4.0-dev",
                "python3-pip",
                "python3-setuptools",
                "python3-wheel",
            ],
            "stage-packages": [
                "gstreamer1.0-clutter-3.0",
                "gstreamer1.0-gtk3",
                "gstreamer1.0-libav",
                "gstreamer1.0-plugins-base",
                "gstreamer1.0-plugins-good",
                "gstreamer1.0-plugins-bad",
                "libglu1-mesa",
                "libgpm2",
                "libjson-glib-1.0-0",
                "libmpv1",
                "libslang2",
                "samba-libs",
            ],
        },
    },
} + desktop.apply("gnome") + preload.apply() + alsa.apply()

The resultant snapcraft.yaml is in the following Gist: