日本語

Packaging

cargo packager builds an installer or package for each system from a table in Cargo.toml.

Install cargo-packager

hypersig_desktop builds no packages itself. cargo-packager reads a table in the application's Cargo.toml, builds the release binary, and wraps it in the formats each system installs from. The repository's CI packages examples/notes with version 0.11.8 on macOS, Windows, and Linux, so install that version:

cargo install cargo-packager --version 0.11.8 --locked
cargo packager -p example-notes --release

The second command, run from the workspace's root, packages the crate -p names. --release packages the binary under target/release, and the packages are written there too.

The packaging table

cargo-packager reads [package.metadata.packager] from the Cargo.toml of the crate it packages. This is the table of examples/notes, without the version line it also has:

examples/notes/Cargo.toml
[package.metadata.packager]
product-name = "hypersig notes"
identifier = "dev.hypersig.example-notes"
before-packaging-command = "cargo build --release -p example-notes"
binaries = [{ path = "example-notes", main = true }]
icons = ["icons/icon.png"]
KeyWhat it sets
product-nameThe application's name, which the packages take, as in hypersig notes.app.
identifierThe reverse domain name of the package. Give it the identifier App::new names.
before-packaging-commandThe command run first, which builds the release binary.
binariesThe binaries to package, by file name under target/release. main = true marks the one the package starts.
iconsThe icon files, relative to the directory of this Cargo.toml.
versionThe version of the package. Without it, cargo-packager takes the crate's version.

The identifier decides where the web view keeps its data. On macOS, WKWebView keeps each application's cookies and localStorage by the bundle's identifier, and on Windows and Linux they are kept under the directory App::data_dir returns, which App::new names. With the two identifiers equal, the packaged application and its data are found under one name on every system. CI checks that the .app of examples/notes names dev.hypersig.example-notes as its bundle identifier.

Leave version out, or keep it equal to the crate's version. Updates compares env!("CARGO_PKG_VERSION"), the crate's version, with the one the update endpoint announces, so a package built under another version is offered an update it already is, or misses one. Nothing checks that the two agree.

The icon

The AppImage needs a square icon: without one, cargo packager stops on Linux with Could not find a square icon to use as AppImage icon. examples/notes/icons/icon.png is a 256 by 256 PNG, which also becomes the .app's icon on macOS. icons takes glob patterns, so icons/*.png lists every PNG in that directory.

file-associations in the packaging table makes the application the one a file manager opens a file type with, and deep-link-protocols makes it the one a link of a URL scheme such as myapp:// opens. In cargo-packager 0.11.8's source, the .app lists them in its Info.plist, the NSIS installer and the MSI write them to the registry, and the Debian package's desktop entry names their MIME types. examples/notes opens .md files:

file-associations = [{ extensions = ["md"], mime-type = "text/markdown", description = "Markdown note" }]
deep-link-protocols = [{ schemes = ["myapp"] }]

Windows and Linux start the program with the file's path or the link on its command line, so a copy already running receives it through Os::launches as another start's arguments. macOS hands the file or the link to the copy running instead, and hypersig_desktop delivers it the same way: each file as its path and each link as its text in Launch::args. The first start is no exception on macOS, so a setup that takes Os::launches before it returns the router receives the file that started the application. CI checks that the .app of examples/notes lists .md. On macOS, open -a with a .md file put the file in the list of a running notes built into a bundle by hand, which the copy without this delivery did not; no run has opened a link yet, nor a file on Windows or Linux. examples/notes has no deep-link-protocols line, since it opens files only.

Keys of the macOS Info.plist

cargo-packager writes the .app's Info.plist and adds every key of the file [package.metadata.packager.macos] names as info-plist-path, read from the crate's directory. A page that uses the microphone or the camera needs NSMicrophoneUsageDescription or NSCameraUsageDescription there, and examples/meter adds the first:

examples/meter/Cargo.toml
[package.metadata.packager.macos]
info-plist-path = "Info.plist"

CI packages the meter on macOS and checks that its .app has the key.

What each system gets

Without --formats, cargo-packager builds the formats of the system it runs on, and CI runs it this way on each system:

SystemPackages
macOSan .app bundle and a .dmg disk image
Windowsan NSIS installer (.exe)
Linuxa .deb, an AppImage, and a pacman package

A package holds a binary for the system and architecture it was built on, so build each system's packages on that system. The binary is built with the features before-packaging-command names; the smoke feature of Smoke runs stays out of a release.

Code signing

Without a code signature, macOS refuses to open the downloaded .app, and Windows warns before it runs the installer. cargo-packager signs while it packages. These are the settings and variables its 0.11.8 source reads:

  • macOS: signing-identity in [package.metadata.packager.macos], such as Developer ID Application: NAME (TEAM_ID), turns signing on. APPLE_CERTIFICATE, the base64 of the .p12 file, and APPLE_CERTIFICATE_PASSWORD import the certificate into a keychain of its own, as CI needs. The binary is signed with the hardened runtime (--options runtime), and entitlements names the file of entitlements it is signed with.
  • macOS notarization follows signing when APPLE_ID, APPLE_PASSWORD (an app-specific password), and APPLE_TEAM_ID are set, or APPLE_API_KEY, APPLE_API_ISSUER, and APPLE_API_KEY_PATH, or APPLE_KEYCHAIN_PROFILE. It sends the .app to notarytool and staples the ticket. Without any of them, it warns and skips notarization.
  • The hardened runtime blocks the microphone and the camera unless the entitlements hold com.apple.security.device.audio-input or com.apple.security.device.camera as true. examples/meter names Entitlements.plist, which holds the first, for an application that allows the microphone.
  • Windows: certificate-thumbprint and digest-algorithm (SHA-256 is recommended) in [package.metadata.packager.windows] sign with signtool.exe from a certificate in the Windows store, with timestamp-url for the timestamp. sign-command, a command with %1 for the file, signs some other way, such as with osslsigncode.
No run in this repository has signed or notarized a package, since it needs the owner's Apple Developer account and Windows certificate. Issue #226 tracks it.

Signing the packages for updates

An Updater installs only a package whose signature its public key verifies. cargo packager signer generate makes the key pair: with --path, it writes the private key to that file and the public key to the same path with .pub added. With CARGO_PACKAGER_SIGN_PRIVATE_KEY set to the private key, or to the path of its file, and CARGO_PACKAGER_SIGN_PRIVATE_KEY_PASSWORD to its password, cargo packager signs each package it builds:

cargo packager signer generate --path ~/.keys/notes.key
CARGO_PACKAGER_SIGN_PRIVATE_KEY=~/.keys/notes.key \
CARGO_PACKAGER_SIGN_PRIVATE_KEY_PASSWORD='the password' \
cargo packager -p example-notes --release

In cargo-packager 0.11.8's source, signing writes the signature beside each package as a file with .sig added to its name, and first packs the .app directory into an .app.tar.gz, which it signs in place of the directory. The repository's CI packages without a key, so it does not run this step.

Keep the private key and its password out of the repository. Anyone who holds them can sign a package that every installed copy of the application accepts as an update.

Next

Updates finds a signed package from the running application and installs it. The application covers the identifier App::new takes.