Build configuration

Every snap project depends on a file called snapcraft.yaml, which is the recipe that instructs Snapcraft on how to build it. This page provides an overview of the major sections of a snapcraft.yaml file.

For practical guidance about how to approach this file and your snap project for the first time, see the Snapcraft checklist. For a reference with all the keys and values available for this file, see the snapcraft.yaml schema.

The snapcraft.yaml file can be organised into three main sections according to function:

  1. Top-level directives and information about the snap, which describe its base properties and publication details.
  2. App directives, which describe the execution, interfaces, and resources available to each app in the snap.
  3. Part directives, which describe how to import and build each required part of the snap.

Since the configuration is written in YAML, these sections are merely recommendations to make the file readable for humans. The order of the directives doesn’t affect the build process of Snapcraft.

Top-level directives

The first block of the configuration typically contains all the base settings for the snap, which can be roughly split into two categories:

  • Directives for the snap’s software base and runtime, such as the silicon architecture, platform, core libraries, packaging, runtime confinement, and development tooling.
  • Information about the snap itself, such as its name, version, contributors, and the publisher’s website, which is made public on its page on the Snap Store.

As an example, here are the first lines in snapcraft.yaml for the Hello World snap:

name: hello
base: core22
version: '2.10'
summary: GNU Hello, the "hello world" snap
description: |
  GNU hello prints a friendly greeting.
grade: stable
confinement: strict

App directives

The apps key declares the applications and services that the snap runs. For each app you can declare settings that control how each app is executed and which system resources it can access. Example settings include the path and parameters to run, the app’s environment variables, and other binaries that the app requires.

Continuing with Hello World as an example, here’s its apps key:

apps:
  hello:
    command: bin/hello

Part directives

The parts key details all of the upstream code components that contribute to the apps inside the snap. Every aspect of the part is managed here, such as its source, dependencies, and how it’s compiled. For a primer on parts, see Adding parts.

Cribbing from Hello World again, this is its parts key:

parts:
  gnu-hello:
    source: http://ftp.gnu.org/gnu/hello/hello-2.10.tar.gz
    plugin: autotools

Further reading

You can pass certain environment variables to Snapcraft during build. See Environment variables that Snapcraft exposes for a list of them.

See Snapcraft advanced grammar to learn how to check for specific conditions for certain keys.

3 Likes

Note that you can, as the last resort, check out the schema in the source tree: snapcraft/snapcraft.json at master · snapcore/snapcraft.

1 Like

That schema is not relevant for Snapcraft 7.

This should have a title change to state snapcraft.yaml format rather than simply snapcraft format. Even better might be to replace format with syntax or schema in addition to adding .yaml. Snapcraft itself is a tool, which does not have a “format”. The configuration file that it consumes, snapcraft.yaml is the thing that has a format!

I haven’t done this myself, as I am wary that changing the title might change the URL and therefore the links from the contents/navigation post, which is rightly locked from my access, so I might break the docs site by doing the change myself without the consequent change to the contents doc.

Thanks so much for this suggestion, and I think you’re absolutely right. I’ll do a quick audit of what it might involve across the docs/links, but I think we should change this page to The snapcraft.yaml schema. I’ll do the same for The snap format.

1 Like

I don’t think SC7 uses that JSON file anymore for schema; AIUI, the new definition for snapcraft.yaml grammar is here, https://github.com/snapcore/snapcraft/blob/main/snapcraft/projects.py. But I’m not absolutely sure.

Snapcraft is still using (and updating) that schema. It’s particularly useful to validate your yaml files before you save them via an editor like VSCode.

The last sentence is not accurate – that JSON file no longer defines the current schema for Snapcraft, it’s defined by a number of Python files.

Thanks. I’ve removed this reference for now.

I revamped this page for language and structure, and included more up-to-date information about the Hello World snap.

1 Like