Skip to content

Commit db20a46

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

File tree

1 file changed

+126
-7
lines changed

1 file changed

+126
-7
lines changed

cargo/README.md

Lines changed: 126 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,23 @@ 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:
21-
```
22-
python3 ./flatpak-cargo-generator.py ./quickstart/Cargo.lock -o cargo-sources.json
23-
```
22+
The generated cargo manifest file `cargo-sources.json` should be added
23+
to the Flatpak manifest like so:
2424

25-
The output file should be added to the manifest like
2625
```json
2726
{
2827
"name": "quickstart",
2928
"buildsystem": "simple",
29+
"build-options": {
30+
"append-path": "/usr/lib/sdk/rust-stable/bin",
31+
"env": {
32+
"CARGO_NET_OFFLINE": "true"
33+
}
34+
},
3035
"build-commands": [
3136
"install -Dm644 cargo/config .cargo/config.toml",
3237
"cargo --offline fetch --manifest-path Cargo.toml --verbose",
@@ -43,11 +48,125 @@ The output file should be added to the manifest like
4348
}
4449
```
4550

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.
51+
An example of a complete Flatpak manifest for a Rust project is provided
52+
below.
53+
54+
<details>
55+
<summary>Manifest</summary>
56+
57+
```yaml
58+
app-id: com.example.my_rust_app
59+
# Replace with target runtime
60+
runtime: org.freedesktop.Platform
61+
# Replace with latest runtime version
62+
runtime-version: 24.08
63+
sdk: org.freedesktop.Sdk
64+
sdk-extensions:
65+
- org.freedesktop.Sdk.Extension.rust-stable
66+
command: my_app
67+
finish-args:
68+
- --device=dri
69+
- --share=ipc
70+
- --socket=wayland
71+
- --socket=fallback-x11
72+
modules:
73+
- name: my_app
74+
buildsystem: simple
75+
build-options:
76+
append-path: /usr/lib/sdk/rust-stable/bin
77+
env:
78+
CARGO_HOME: /run/build/my_app/cargo
79+
build-commands:
80+
- cargo --offline fetch --manifest-path Cargo.toml --verbose
81+
- cargo build --offline --release --all-features
82+
- install -Dm0755 target/release/my_app ${FLATPAK_DEST}/bin/my_app
83+
- install -Dm0644 logo.svg ${FLATPAK_DEST}/share/icons/hicolor/scalable/apps/${FLATPAK_ID}.svg
84+
- install -Dm0644 ${FLATPAK_ID}.desktop ${FLATPAK_DEST}/share/applications/${FLATPAK_ID}.desktop
85+
- install -Dm0644 ${FLATPAK_ID}.metainfo.xml ${FLATPAK_DEST}/share/metainfo/${FLATPAK_ID}.metainfo.xml
86+
sources:
87+
- type: archive
88+
url: https://github.com/my_app/my_app.git
89+
tag: "v0.1.1"
90+
commit "0284b00219cee734e3f6ee2cd6be2bd8004c3cf2"
91+
- cargo-sources.json
92+
```
93+
</details>
94+
95+
96+
Rust and cargo is provided by the Flatpak extension
97+
`org.freedesktop.Sdk.Extension.rust-stable`. To install it run:
98+
99+
```sh
100+
flatpak install flathub org.freedesktop.Sdk.Extension.rust-stable//$branch
101+
```
102+
103+
The `$branch` must match the `runtime-version` of `org.freedesktop.Sdk`.
104+
For example `24.08`. GNOME and KDE runtimes are based on
105+
`org.freedesktop.Sdk`. The correct branch of the Rust extension to
106+
install for a given GNOME or KDE runtime version can be found using:
107+
108+
```
109+
flatpak info -m org.kde.Sdk | grep -A 5 "org.freedesktop.Sdk.Extension" | grep -E "^version"
110+
```
111+
112+
`append-path: /usr/lib/sdk/rust-stable/bin` is used to add the location
113+
in the Flatpak extension where rust and cargo binaries are located to
114+
`$PATH` inside the build environment.
47115

116+
Either the `CARGO_HOME` environment variable needs to be set to
117+
`/run/build/$module-name/cargo` where `$module-name` is the flatpak
118+
module name or the config generated by `flatpak-cargo-generator` needs
119+
to be installed as `.cargo/config.toml` (the first line of `build-commands`).
48120

49121
For a complete example see the quickstart project.
50122

123+
## `CARGO_HOME` is set by buildsystem
124+
125+
It is often common for example when using meson to set the `CARGO_HOME`
126+
environment variable like this in `meson.build`:
127+
128+
```
129+
cargo_env = [ 'CARGO_HOME=' + meson.project_build_root() / 'cargo-home' ]
130+
```
131+
132+
But in this case, cargo cannot find the config generated by
133+
`flatpak-cargo-generator` causing it to try fetching dependencies over
134+
the network making non-networked builds fail. This may happen if
135+
bare git dependencies are present in the upstream `Cargo.toml`. It will
136+
usually fail with
137+
`can't checkout from '$git-url': you are in the offline mode`.
138+
139+
In this case, copy or symlink the generated config to `.cargo` in the
140+
Flatpak manifest like so:
141+
142+
```json
143+
{
144+
"name": "my_app",
145+
"buildsystem": "meson",
146+
"build-options": {
147+
"append-path": "/usr/lib/sdk/rust-stable/bin",
148+
"env": {
149+
"CARGO_NET_OFFLINE": "true"
150+
}
151+
},
152+
"sources": [
153+
{
154+
"type": "git",
155+
"url": "https://github.com/ghost/my_app.git",
156+
"tag": "v0.0.1"
157+
},
158+
"cargo-sources.json",
159+
{
160+
"type": "shell",
161+
"commands": [
162+
"mkdir -p .cargo",
163+
"ln -s cargo/config .cargo/config.toml"
164+
]
165+
}
166+
]
167+
}
168+
```
169+
51170
## Development
52171

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

0 commit comments

Comments
 (0)