1
# Snapcraft: Advanced features
3
Once you have built [your first snap](your-first-snap.md), you will probably
4
want to learn more about snapcraft's more advanced features. Having a look at
5
our selection of examples is a good idea, as we want it to be a good showcase
6
of what is possible and generally relevant.
10
Our showcase can be found in the actual source of `snapcraft` itself. Check
11
it out by simply running:
13
bzr branch lp:snapcraft
16
Inspecting the source locally will make easier to build the examples and
17
play around with them. (You can
18
[view them online](https://bazaar.launchpad.net/~snappy-dev/snapcraft/core/files/head:/examples/)
21
### Playing around with the examples
23
If you just checked out the `snapcraft` source and inspect the examples, you
24
can start off your explorations by reading the accompanying `snapcraft.yaml`
29
This will inform you of all the steps taken during the creation of the snap.
31
## Defining your parts
33
Once you have noted down all the general information about your snap
34
(like description, vendor information and everything else), naming
35
the individual parts will define the stucture of your `snapcraft.yaml` file.
36
Think of parts as individual components of your snap: Where do you pull them
37
from? How are they built?
39
### Prerequisites during the build
41
The example named `downloader-with-wiki-parts` shows how very easily make sure
42
that its build dependencies are installed:
44
build-packages: [libssl-dev]
46
The above will install the `libssl-dev` package from the Ubuntu archive before
47
an attempted build. If you need a specific version of libssl-dev or a custom
48
build, you will need to specify a separate part.
50
Also note that the above will not define which libraries are shipped with the
51
app. It merely makes sure you have all the relevant build tools installed.
54
### Pulling and building
56
If you just intend to pull and build the source, take a look at the `gopaste`
57
example. In its `snapcraft.yaml` file you can find just this one `parts`
63
source: git://github.com/wisnij/gopaste/gopasted
65
It starts off with the name of the specific part (`gopaste` here), the origin
66
of the part (it's a `git` URL) and how to build it (type: `go-project`).
67
Other possible scenarios would be Bazaar or Mercurial branches, or local
70
### Mixing and matching plugins
72
An interesting example is `py2-project` because it defines only one part
73
(`spongeshaker`), but uses plugins of two types (`python2-project` and
74
`make-project) to assemble and build the snap:
79
source: git://github.com/markokr/spongeshaker.git
83
The example above mixes and matches parts of different origin. Locally it
84
provides a binary we intend to ship (the `sha3sum.py` script) and a
85
`Makefile` (to install our script in the right place).
87
`spongeshaker` is a python library we will need to pull from git, build as
88
a python project and bundle along with our script.
90
A possible use-case for the above would be if you just intend to ship a small
91
binary which you maintain, but require a library you need to build from git
92
as opposed to simply including it from the Ubuntu archives.
94
What's happening during the `snapcraft` run is:
96
1. Get `spongeshaker` from `git`.
97
1. Build it as a python project (which will include installing the python
98
library in the right place).
99
1. Running `make` (from the local `Makefile`) and thus installing our
100
`sha3sum.py` script in the right place.
102
### Putting your parts in order
104
If you app comprises of multiple parts, it might be necessary to build and
105
stage parts in a particular order. This can be done by using the `after`
111
source: lp:~mterry/+junk/pipelinetest
115
type: autotools-project
116
source: lp:~mterry/libpipeline/printf
119
In the case of the `libpipeline` example above, the part named `libpipeline`
120
will be built before `pipelinetest`. Especially if you need specific
121
functionality during a build or as part of checks during the `snap` phase,
126
With snapcraft we want to make it easy to learn from other app vendors and
127
re-use parts which have worked well for them.
129
In the `downloader-with-wiki-parts` example, you can see that the `main`
135
As we never define the `curl` part in the above example, `snapcraft` will
136
check the Ubuntu Wiki, which is where we currently host examples of
137
successful snapcraft parts. The build order in this case would be `curl`,
145
If you are planning to provide binaries and servies to the users of your
146
apps, you need to specify them in your definition first. It's just a matter
149
The `godd` example has one binary:
155
The above will take care of making the script executable, adding it to the
156
user's path and install it in the right place.
158
For a simple service we can take a look at `gopaste`:
162
description: "gopaste"
165
You define a name for the service, describe it (so log messages are more
166
descriptive), declare how to run the service and that's it. For more
167
thoughts on services and their security, visit the
168
[snappy policy](https://developer.ubuntu.com/en/snappy/guides/security-policy/).
171
### Limiting the number of installed files
173
To check the list of files included in your snap, you can use `dpkg -c` on
174
the resulting `.snap` file. If you find that certain files should not be
175
shipped to the user (download size being just one factor), you can use
176
`snapcraft`'s feature of defining specific filesets:
179
- usr/lib/x86_64-linux-gnu/libgudev-1.0.so*
180
- usr/lib/x86_64-linux-gnu/libobject-2.0.so*
181
- usr/lib/x86_64-linux-gnu/libglib-2.0.so*
184
Here `godd` further defines the list of files to be placed in the app
185
during the `snap` phase. As you can see above, globs (using asterisks as
186
wildcard characters) are a good way of handling complexities within the