Using Nix with Gitea Actions

Using Nix to provide a reproducible build environment inside Gitea Actions

Carl Sagan once said, "If you wish to make an apple pie from scratch, you must first invent the universe." A reproducible build does not need to go quite that far. Nix can provide the build environment inside Gitea Actions with a small addition to the workflow.

I use Nix and maintain several packages. I appreciate the reproducible build environments it provides and how quickly packages are updated.

The workflow below installs Nix in a Gitea Actions job and runs hello from nix-shell:

name: nix

on:
  push:

jobs:
  lint-backend:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Install dependencies for Nix setup action
        run: |
          apt update -y
          apt install sudo -y          
      - uses: cachix/install-nix-action@v27
        with:
          nix_path: nixpkgs=channel:nixos-unstable
      - name: Test running command with Nix
        run: nix-shell -p hello --run "hello"

The default Gitea Actions runner image does not include sudo, which cachix/install-nix-action requires, so the workflow installs it first. A custom runner that already includes sudo can skip that step.

I prefer to live on the bleeding edge with nixpkgs=channel:nixos-unstable, but you can pin a more stable channel instead. The cachix/install-nix-action Action does not configure a channel by default, so the workflow must specify one.

Installing Nix adds setup time and requires sudo on this runner image. In return, the job can use Nix package definitions instead of reproducing the environment with runner-specific installation commands.

Editor's Note: Using DetSys' Nix installer action can speed up the install of nix in your workflows, and also reduce the customization of the runner needed.