A Forgejo Actions step that pushes what a job built to a niks3 cache, using the job's OIDC ID token.
README.md

niks3-push #

A Forgejo Actions step that pushes the Nix store paths a job built to a niks3 binary cache. It authenticates with the OIDC ID token Forgejo gives the job, so no cache credential is stored anywhere: not in the repository's secrets and not on the runner.

git clone https://git.jcollie.dev/jeff/niks3-push.git

It is mirrored at https://tangled.org/jcollie.dev/niks3-push.

Using it #

The job asks Forgejo for an ID token with enable-openid-connect: true, and the step goes last:

jobs:
  test:
    runs-on: nixos-do-s
    enable-openid-connect: true
    steps:
      - name: Check out
        uses: https://code.forgejo.org/actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Build
        run: nix build -L .#default

      - name: Push to the cache
        if: ${{ forge.ref == 'refs/heads/main' }}
        uses: https://git.jcollie.dev/jeff/niks3-push@<commit>
        with:
          server-url: https://niks3.example.com

Pin it to a full commit hash, as with any action.

The if: saves a push that the server would refuse anyway. Which jobs may push is decided by niks3, not by the workflow; see The server below.

Inputs #

Input Default What it is
server-url required The niks3 server to push to.
audience server-url The audience to ask for in the ID token. It must match the audience of the OIDC provider configured on the server.

What it pushes #

Everything the job built, and nothing it only downloaded. Nix marks a path it built itself as ultimate, and that is what the step selects out of nix path-info --all, leaving out .drv files.

That includes fixed-output derivations, which is the point for a Zig project: the derivation that fetches a project's Zig dependencies is a build input, not part of any output, so pushing the outputs of nix build would miss it and every later job would fetch the dependencies from their origins again.

niks3 uploads each pushed path's whole closure, skipping only what the cache already holds, so the first push of a package also uploads its runtime dependencies.

When it pushes nothing #

The step succeeds without pushing, and says why, when:

  • the job has no ID token: it did not set enable-openid-connect: true, or it is a pull request from a fork, which Forgejo never gives one; or
  • the job built nothing, because everything it needed was substituted.

A push the server refuses fails the step.

The server #

niks3 has to trust the forge's issuer. For a Forgejo instance at https://git.example.com, in the niks3 NixOS module:

services.niks3.oidc.providers.forgejo = {
  issuer = "https://git.example.com/api/actions";
  audience = "https://niks3.example.com";
  boundClaims = {
    repository_owner = [ "me" ];
    ref = [ "refs/heads/main" ];
  };
  scopes = [ "write" ];
};

boundClaims is what keeps the cache trustworthy: only a token whose claims all match may write, so a branch, a pull request, or anyone else's repository on the forge can read the cache but not add to it. Granting only write leaves reads open to everyone.

The niks3 client is pinned in flake.nix to the revision the server runs. Move the two together.

How it works #

action.yml is a composite action with one step, nix run on this repository's own flake. The flake builds push.sh into a script with curl, jq and niks3 on its PATH, so a job needs nothing but Nix. The script:

  1. reads ACTIONS_ID_TOKEN_REQUEST_URL and ACTIONS_ID_TOKEN_REQUEST_TOKEN, which forgejo-runner sets for a job that may have an ID token;
  2. lists what the job built with nix path-info --all;
  3. asks Forgejo for a token with the configured audience; and
  4. runs niks3 push with that token.

License #

MIT. See LICENSES/MIT.txt. The project follows the REUSE specification.

References cited #