Skip to content

Commit f0d119b

Browse files
committed
cargo: Improve the documentation
1 parent 8568d78 commit f0d119b

File tree

1 file changed

+106
-20
lines changed

1 file changed

+106
-20
lines changed

cargo/README.md

Lines changed: 106 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,125 @@ Generated manifests are supported by flatpak-builder 1.2.x or newer.
1515

1616
## Usage
1717

18-
Poetry users: first activate your virtualenv by running `poetry shell`.
18+
1. Install poetry v2 https://python-poetry.org/docs/#installation
19+
2. Run `poetry env activate` inside the `cargo` folder
20+
3. `python3 flatpak-cargo-generator.py /path/to/Cargo.lock -o cargo-sources.json`
1921

20-
Convert the locked dependencies by Cargo into a format flatpak-builder can understand:
22+
The generated cargo manifest file `cargo-sources.json` should be added
23+
to the Flatpak manifest inside `sources`. An example of a complete
24+
Flatpak manifest for a Rust project is provided below.
25+
26+
```yaml
27+
app-id: com.example.my_rust_app
28+
# Replace with target runtime
29+
runtime: org.freedesktop.Platform
30+
# Replace with latest runtime version
31+
runtime-version: "24.08"
32+
sdk: org.freedesktop.Sdk
33+
sdk-extensions:
34+
- org.freedesktop.Sdk.Extension.rust-stable
35+
command: my_app
36+
finish-args:
37+
- --device=dri
38+
- --share=ipc
39+
- --socket=wayland
40+
- --socket=fallback-x11
41+
modules:
42+
- name: my_app
43+
buildsystem: simple
44+
build-options:
45+
append-path: /usr/lib/sdk/rust-stable/bin
46+
env:
47+
CARGO_HOME: /run/build/my_app/cargo
48+
build-commands:
49+
- cargo --offline fetch --manifest-path Cargo.toml --verbose
50+
- cargo build --offline --release --all-features
51+
- install -Dm0755 target/release/my_app ${FLATPAK_DEST}/bin/my_app
52+
- install -Dm0644 logo.svg ${FLATPAK_DEST}/share/icons/hicolor/scalable/apps/${FLATPAK_ID}.svg
53+
- install -Dm0644 ${FLATPAK_ID}.desktop ${FLATPAK_DEST}/share/applications/${FLATPAK_ID}.desktop
54+
- install -Dm0644 ${FLATPAK_ID}.metainfo.xml ${FLATPAK_DEST}/share/metainfo/${FLATPAK_ID}.metainfo.xml
55+
sources:
56+
- type: archive
57+
url: https://github.com/my_app/my_app.git
58+
tag: "v0.1.1"
59+
commit "0284b00219cee734e3f6ee2cd6be2bd8004c3cf2"
60+
- cargo-sources.json
61+
```
62+
63+
Rust and cargo is provided by the Flatpak extension
64+
`org.freedesktop.Sdk.Extension.rust-stable`. To install it run:
65+
66+
```sh
67+
flatpak install flathub org.freedesktop.Sdk.Extension.rust-stable//$branch
2168
```
22-
python3 ./flatpak-cargo-generator.py ./quickstart/Cargo.lock -o cargo-sources.json
69+
70+
The `$branch` must match the `runtime-version` of `org.freedesktop.Sdk`.
71+
For example `24.08`. GNOME and KDE runtimes are based on
72+
`org.freedesktop.Sdk`. The correct branch of the Rust extension to
73+
install for a given GNOME or KDE runtime version can be found using:
74+
75+
```sh
76+
flatpak info -m org.kde.Sdk | grep -A 5 "org.freedesktop.Sdk.Extension" | grep -E "^version"
2377
```
2478

25-
The output file should be added to the manifest like
79+
`append-path: /usr/lib/sdk/rust-stable/bin` is used to add the location
80+
in the Flatpak extension where rust and cargo binaries are located to
81+
`$PATH` inside the build environment.
82+
83+
Either the `CARGO_HOME` environment variable needs to be set to
84+
`/run/build/$module-name/cargo` where `$module-name` is the flatpak
85+
module name or the config generated by `flatpak-cargo-generator` needs
86+
to be installed as `.cargo/config.toml` (the first line of `build-commands`).
87+
88+
For a complete example see the quickstart project.
89+
90+
## `CARGO_HOME` is set by buildsystem
91+
92+
It is often common for example when using meson to set the `CARGO_HOME`
93+
environment variable like this in `meson.build`:
94+
95+
```meson
96+
cargo_env = [ 'CARGO_HOME=' + meson.project_build_root() / 'cargo-home' ]
97+
```
98+
99+
But in this case, cargo cannot find the config generated by
100+
`flatpak-cargo-generator` causing it to try fetching dependencies over
101+
the network making non-networked builds fail. This may happen if
102+
bare git dependencies are present in the upstream `Cargo.toml`. It will
103+
usually fail with
104+
`can't checkout from '$git-url': you are in the offline mode`.
105+
106+
In this case, copy or symlink the generated config to `.cargo` in the
107+
Flatpak manifest like so:
108+
26109
```json
27110
{
28-
"name": "quickstart",
29-
"buildsystem": "simple",
30-
"build-commands": [
31-
"install -Dm644 cargo/config .cargo/config.toml",
32-
"cargo --offline fetch --manifest-path Cargo.toml --verbose",
33-
"cargo --offline build --release --verbose",
34-
"install -Dm755 ./target/release/quickstart -t /app/bin/"
35-
],
111+
"name": "my_app",
112+
"buildsystem": "meson",
113+
"build-options": {
114+
"append-path": "/usr/lib/sdk/rust-stable/bin",
115+
"env": {
116+
"CARGO_NET_OFFLINE": "true"
117+
}
118+
},
36119
"sources": [
37120
{
38-
"type": "dir",
39-
"path": "."
121+
"type": "git",
122+
"url": "https://github.com/ghost/my_app.git",
123+
"tag": "v0.0.1"
40124
},
41-
"cargo-sources.json"
125+
"cargo-sources.json",
126+
{
127+
"type": "shell",
128+
"commands": [
129+
"mkdir -p .cargo",
130+
"cp -vf cargo/config .cargo/config.toml"
131+
]
132+
}
42133
]
43134
}
44135
```
45136

46-
Make sure to override CARGO_HOME env variable to point it to `/run/build/$module-name/cargo` where `$module-name` is the flatpak module name, `quickstart` in this example.
47-
48-
49-
For a complete example see the quickstart project.
50-
51137
## Development
52138

53139
1. Install Poetry v2 https://python-poetry.org/docs/#installation

0 commit comments

Comments
 (0)