Hydra

From NixOS Wiki
Revision as of 07:46, 9 July 2024 by Mic92 (talk | contribs) (Created page with "Ein Jobset kann wie folgt konfiguriert werden.")

Hydra ist ein Werkzeug für kontinuierliche Integrationstests und Softwarefreigabe, das eine rein funktionale Sprache zur Beschreibung von Build-Jobs und deren Abhängigkeiten verwendet. Kontinuierliche Integration ist eine einfache Technik zur Verbesserung der Qualität des Softwareentwicklungsprozesses. Ein automatisiertes System prüft kontinuierlich oder periodisch den Quellcode eines Projekts, baut ihn, führt Tests durch und erstellt Berichte für die Entwickler. Auf diese Weise werden verschiedene Fehler, die versehentlich in die Codebasis aufgenommen werden könnten, automatisch erkannt.

Die offiziellen Hydra-Server bieten vorgefertigte Binärpakete an, um die Aktualisierungszeit für Nixpgs zu verkürzen: Die Benutzer müssen sie nicht auf ihren eigenen Computern kompilieren.

Das Hydra Handbuch bietet eine Übersicht der Funktionalität und Funktionen von Hydra sowohl als auch eine aktuelle Installationsanleitung.

Installation

Eine vollständige Installation kann wie folgt konfiguriert werden:

  services.hydra = {
    enable = true;
    hydraURL = "http://localhost:3000"; # extern sichtbare URL
    notificationSender = "hydra@localhost"; # E-Mail des Hydra-Services
    # Bei einer eigenständigen Hydra muss die buildMachinesFiles-Liste deaktiviert werden, um zu vermeiden, dass ein nicht vorhandenes /etc/nix/machines verwendet wird.
    buildMachinesFiles = [];
    # Diese Option verhindert das *alles* von Grund auf neu gebaut
    useSubstitutes = true;
  };

Dieses Modul aktiviert PostgreSQL automatisch, außer die services.hydra.dbi wird geändert. Das Datenbanklayout wird automatisch erstellt vom Hydra-Service. Allerdings ist anzumerken, das zusätzliche Daten in der Datenbank gespeichert werden, was eine vollständige deklarative Konfiguration unmöglich macht. Daher sind Backups erforderlich.

Webserverkonfiguration

Hydra stellt ein Webinterface auf localhost und Port 3000 bereit. Allerdings muss ein neuer Admin-Account (als UNIX user hydra) erstellt werden, bevor man in der Lage ist Änderungen vorzunehmen:

# su - hydra
$ hydra-create-user alice --full-name 'Alice Q. User' \
    --email-address 'alice@example.org' --password-prompt --role admin

Virtuelle Maschinen

Hydra benutzt localhost als Standardbuildmaschine, wenn nichts anderes konfiguriert ist. Standardmäßig sind die Systemfunktionen kvm> und nixos-test in Nix, zum benutzen von Virtuellen Maschinen nicht aktiviert. Jobs, die dies Funktionen benötigen werden daraufhin auf unbestimmte Zeit in die Warteschlange gestellt. Die folgenden Optionen aktieren diese Systemfeature:

{
  nix.buildMachines = [
    { hostName = "localhost";
      system = "x86_64-linux";
      supportedFeatures = ["kvm" "nixos-test" "big-parallel" "benchmark"];
      maxJobs = 8;
    }
  ];
}

Diese Konfiguration erzeugt die Datei /etc/nix/machines. Falls die Hydra-option buildMachinesFiles immer noch auf eine leere Liste gesetzt ist, wird diese Einstellung ignoriert. Deswegen muss diese Option wieder entfernt werden oder /etc/nix/machines hinzugefügt werden.

Flake-Jobset

Ein Jobset kann wie folgt konfiguriert werden.

The Flake output should have the attribute hydraJobs containing an attribute set that may be nested and reference derivations.

A sample Flake output that makes Hydra build all packages could look like this:

{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
  };
  outputs = { self, nixpkgs, ... }: {
    packages.x86_64-linux = {
      ...
    };
</div>

    <div lang="en" dir="ltr" class="mw-content-ltr">
hydraJobs = {
      inherit (self)
        packages;
    };
  };
}

Restricted Mode

Hydra evaluates flakes in restricted mode. This prevents access to files outside of the nix store, including those fetched as flake inputs. Update your nix.settings.allowed-uris to include URI prefixes from which you expect flake inputs to be fetched:

nix.settings.allowed-uris = [
  "github:"
  "git+https://github.com/"
  "git+ssh://github.com/"
];

Build a single Package from nixpkgs

Right now it is not possible to build a single package from nixpkgs with just that input. You will need to provide a supplementary repository which defines what to build. For examples you can check the hydra-example by makefu and in the Hydra Manual.

Imperative Building

These steps are required to build the hello package.

  1. log into Hydra after creating a user with hydra-create-user
  2. create new project
  • identifier: example-hello
  • display name: example-hello
  1. Actions -> Create jobset
  • identifier: hello
  • Nix expression: release.nix in hydra-example -> will evaluate the file release.nix in the given input
  • check interval: 60
  • scheduling shares: 1
  • Inputs:
Input Name Type Value Note
nixpkgs git checkout https://github.com/nixos/nixpkgs nixos-21.11 will check out branch nixos-21.11, will be made available to the nix expression via <nixpkgs>.
hydra-example git checkout https://github.com/makefu/hydra-example hydra-example is used by the jobset as input, release.nix is in the root directory

After creation, the jobset should be in the evaluation phase where inputs will be fetched. This phase may take some time as the complete nixpkgs repository needs to be downloaded before continuing. The result of the evaluation should be a single job which will get built.

Declarative Building

Since 2016, Hydra supports declarative creation of jobsets. Check out the example repository and description by Shea Levy.

Hydra Internals

Definitions

This subsection provides an overview of the Hydra-specific definitions and how to configure them.

Project

A cluster of Jobs which are all coming from a single input (like a git checkout), the first thing you will need to create. Every Job should be able to be built independently from another. Most of the time the project maps to a single repository like nixpkgs. It is comparable to the project definition in Jenkins.

Jobset

Eine Liste von Jobs, die ausgeführt werden sollen. Oft passt ein Jobset zu einem bestimmten Branch (master, staging, stable). Ein Jobset wird durch seine Eingaben definiert und wird ausgelöst, wenn sich diese Eingaben ändern, z.B. wenn ein neuer Commit zu einem Branch hinzugefügt wird. Jobsets können voneinander abhängen

Job

Ein Closure, welches als Teil eines Jobsatzes erstellt wird (wie ein einzelnes Paket, ISO-Image oder Tarball)

Release Set

Definiert alle Jobs, die in Ihrem Release beschrieben sind. Konventionell wird eine Datei namens release.nix verwendet. Eine ausführliche Beschreibung der Struktur finden Sie im Hydra Handbuch für Build Recipes.

Evaluation

The process of interpreting nix code into a list of .drv files. These files are the build recipes for all related outputs. You can introspect these files by running nix show-derivation nixpkgs.hello.

Build

Instantiation of a Job which is being triggered by being part of the release set.

Known Issues

  • hydra-queue-runner sometimes gets stuck even with builds are in the queue, and the builds are not scheduled. The issue is being tracked here. In the meantime, a workaround is to add a cron job that regularly restarts the hydra-queue-runner systemd service. Possible fix: [1]
  • If you see error: unexpected end-of-file it can mean multiple things, some of them are:
  1. You have a miss-match between nix versions on the Hydra server and the builder
  2. It can also mean that hydra-queue-runner needs privileges on the build server. Reference: [2]
  • The default timeout for git operations is 600 seconds [3], which might cause fetches of large repositories like nixos/nixpkgs to fail: error fetching latest change from git repo at `https://github.com/nixos/nixpkgs': timeout. The timeout can be increased with the following configuration.nix snippet:
{
  services.hydra.extraConfig = ''
    <git-input>
      timeout = 3600
    </git-input>
  '';
}

Hydra for NixOS releases

Hydra is used for managing official Nix project releases. The project Hydra server: https://hydra.nixos.org/

Some Hydra trackers for Nix projects: