The cx Book 1.0.0
cx: a package manager and build tool for C and C++
Loading...
Searching...
No Matches
Publishing

cx package turns a package into an archive. cx publish puts that archive into a registry, and cx yank marks a published version so new resolutions skip it.

$ cx package # target/package/mylib-1.2.0.tar.gz, built once to check it
$ cx publish # package and publish to the default registry
$ cx publish --registry internal --dry-run
$ cx yank mylib@1.2.0 # and `cx yank mylib@1.2.0 --undo`

What goes into a package

  • Always: cx.toml.
  • By default: include/, src/, tests/, examples/, benches/, the files of [[bin]], [[example]] and [[bench]] paths, and top-level LICENSE*, README* and COPYING* files.
  • Never: target/, .git/, .cx/ and cx.lock.

Adjust the list with globs in [package]. * matches within a directory, ** across directories, and a plain directory name matches everything in it:

[package]
include = ["data/**", "scripts/gen.py"]
exclude = ["tests/fixtures/huge"]

cx package --list prints the files without packaging them.

The archive is a .tar.gz with one top-level directory, <name>-<version>/. It's reproducible: entries are sorted, timestamps are 0, and owners are 0, so the same sources always give the same checksum. It is compressed with cx's own deflate encoder, whose output depends only on the input (not on a zlib version), about as small as gzip -6.

Verification

cx package (and so cx publish) unpacks the archive into target/package/<name>-<version>/ and builds it there, the way a dependent would: with its registry dependencies and without dev-dependencies. A package that only builds because of a file that isn't in the archive fails here, not on someone else's machine. --no-verify skips the build.

What a registry requires

cx publish refuses a package that:

  • lacks metadata: description and license must be set in [package].
  • has git or path dependencies: a registry package can only depend on registry packages from the same registry. Publish the dependency first. (Dev-dependencies are not part of the published dependencies, so they can be anything.)
  • is already published: versions can't be replaced. Bump package.version.

System dependencies are fine. They aren't listed in the index; cx reads them from the package's cx.toml.

Static registries

A static registry is a directory of files (see Hosting a registry), served by any web server or used straight from disk. cx publish writes into a local copy of it:

$ cx publish --index ~/src/my-index # a checkout of the registry's files
$ cd ~/src/my-index && git commit -am "mylib 1.2.0" && git push
  • The archive is copied to where the index's dl template points.
  • The index file gets the new [[version]] block appended. Index files only ever grow.
  • Publishing the files (git, rsync, CI) is up to you.

When the default registry (or --registry) itself is a directory or file:// URL, --index isn't needed.

Dependencies of the package must come from the registry --index is a copy of: the default registry, or the one named with --registry.

API registries

A registry with an api URL in its config.toml accepts uploads over HTTP:

# config.toml at the root of the index
dl = "https://cdn.example.com/archives/{name}-{version}.tar.gz"
api = "https://registry.example.com/api"

cx publish and cx yank send the token stored with `cx login` (or from CX_TOKEN, CX_TOKEN_<NAME> or a credential provider) as Authorization: Bearer <token>, only to the API's host.

The API, for anyone writing a registry server:

Request Body Meaning
PUT <api>/v1/packages/<name>/<version> the index entry, then the archive, each preceded by its length as a 32-bit little-endian number publish
PUT <api>/v1/packages/<name>/<version>/yank empty yank
PUT <api>/v1/packages/<name>/<version>/unyank empty undo a yank
  • The index entry is the exact [[version]] block to append to the package's index file, including the archive's checksum. The server should check that the checksum matches the archive and that the version is new.
  • A relative api (api = "api") is relative to the index URL.
  • Answers: any 2xx is success. 401 and 403 mean the token was missing or refused. For any other status, cx shows the start of the response body as the reason.

Running a registry: cx-registry

cx-registry is a small registry server that comes with cx. It serves a static index directory and implements the API above on top of it, so a team can run a private registry with authenticated uploads:

$ cx-registry init /srv/cx --signing-key /etc/cx/index.key
$ cx-registry token add /srv/cx --name ci --scope publish
Created token `ci`; it is shown only once:
cxr_0f3c…
$ cx-registry serve /srv/cx --listen 0.0.0.0:8080 --signing-key /etc/cx/index.key
Listening on http://0.0.0.0:8080

Clients use it like any HTTP registry (registries.company = "https://cx.example.com") and publish to it with cx publish.

  • init <ROOT> writes config.toml with dl and api = "api". A relative api is relative to wherever clients find the index, so the server works behind a proxy without knowing its address. --url <URL> writes an absolute api instead. With --signing-key, it also signs config.toml.
  • token add <ROOT> --name <NAME> prints a new token once and stores only its SHA-256 hash in <ROOT>/.cx-registry/tokens.toml (mode 0600; --tokens <FILE> puts it elsewhere). --scope is publish or yank (repeatable; default both), and --package <GLOB> limits a token to some packages. token list and token remove <ROOT> <NAME> manage them. The server reads the file on every upload, so changes need no restart.
  • serve <ROOT> listens on --listen HOST:PORT (default 127.0.0.1:8080; port 0 picks a free one). Index files and archives are served with ETags, so clients' conditional requests work. Files and directories starting with . are never served. --max-upload <MIB> limits uploads (default 64); an upload may unpack to at most 16 times that.

Before it writes anything, the server checks each upload: the token and its scope, that the checksum matches the archive, that the archive contains <name>-<version>/cx.toml with the same name and version as the URL, and that the index entry is exactly what that cx.toml produces (so a client can't publish dependencies its package doesn't have). Versions are never replaced. With --signing-key, every index file it changes is signed again.

The index directory has the same layout as a static registry, so it can also be served by any static web server, copied with rsync, or published to with cx publish --index. The server handles one request at a time: run one per directory. It has no TLS; put it behind a reverse proxy such as Caddy or nginx for https://.

Yanking

A yanked version stays in the registry, so lockfiles that pin it keep working, but new resolutions don't pick it. For a static registry, cx yank edits the yanked line of that version in place (--index for a checkout). It's the one change to an index file that isn't an append.