Arion Documentation

repository·main·Indexed 21 days ago

https://github.com/hercules-ci/arion

Arion is a tool within the Hercules-CI ecosystem for building and running multi-container applications using NixOS modules. It provides specialized support for Docker images built with Nix and can be deployed via remote Docker sockets, registry uploads, or as a NixOS module using the virtualisation.arion configuration. The tool includes a CLI for managing service lifecycles, executing commands in containers, and evaluating Docker Compose files as JSON.

Tokens
2.4K
Snippets
6
Records
11
Agent score
75%

What's inside Arion

  1. Overview of Arion

    main
    Arion is a tool designed to build and run multi-container applications using NixOS modules. It provides specialized support for Docker images built with Nix, which enables a smoother development experience and improved performance compared to standard Docker workflows.
  2. Use nixified scripts to hack on the arion command

    main

    The arion command is written in Haskell. While you can make changes directly to the Haskell code, you can use the provided nixified scripts in the repository root to manage common development tasks without needing deep experience with Haskell tooling.

    # Typechecking
    ./build
    ./live-check
    
    # Running tests
    ./live-unit-tests
    
    # Haskell REPL
    ./repl
    
    # Running arion
    ./run-arion
    ./run-arion-via-nix
  3. Prototype new features using the module system

    main
    The Arion module system does not distinguish between modules and configurations. You can prototype new features by factoring out functionality directly within a real-world project instead of modifying the core source code.
  4. Deploy Arion projects using Docker images

    main

    When useHostStore is disabled, Arion builds Docker images that can be deployed to any Docker host, including non-NixOS hosts. You have two primary methods for deployment:

    1. Remote Docker socket: You can deploy directly to a single Docker host using its socket. This method does not require a registry. Note that access to a Docker socket is equivalent to root access on the host.
    2. Registry upload: You can push images to a registry using arion push. Alternatively, you can implement custom push logic using arion cat, the eval function on the arion package, or the lib.eval function on the flake to retrieve the defined images.
  5. Deploy Arion projects as a NixOS module

    main

    Arion projects can be integrated into a NixOS configuration. This approach allows for efficient transfers using Nix's copy-closure algorithm and utilizes transparent binary caches instead of a stateful Docker registry.

    Important Note: This deployment method does NOT use an arion-pkgs.nix file; it reuses the host's pkgs instead.

    To use this, add the Arion NixOS module to your imports. Depending on how you use Arion, choose one of the following import methods:

    • NIV: ((import ./nix/sources.nix).arion + "/nixos-module.nix")
    • Flakes: arion.nixosModules.arion (where arion is a flake input)
    • Other: Use builtins.fetchTarball with the specific commit hash of Arion.

    Configure the project under virtualisation.arion.

    {
      imports = [
        # Pick one of:
        #  - niv
        ((import ./nix/sources.nix).arion + "/nixos-module.nix")
        #  - or flakes (where arion is a flake input)
        arion.nixosModules.arion
        #  - or other: copy commit hash of arion and replace HASH in:
        (builtins.fetchTarball "https://github.com/hercules-ci/arion/archive/HASH.tar.gz" + "/nixos-module.nix")
      ];
    
      virtualisation.arion = {
        backend = "podman-socket"; # or "docker"
        projects.example = {
          serviceName = "example"; # optional systemd service name, defaults to arion-example
          settings = {
            # Specify your project here, or import it from a file.
            # NOTE: This does NOT use ./arion-pkgs.nix, but defaults to NixOS' pkgs.
            imports = [ ./arion-compose.nix ];
          };
        };
      };
    }
  6. Iterate on built-in modules via local source

    main

    To refactor or modify existing built-in modules, you must fork and edit the Arion source code directly. For a fast iteration cycle, use the run-arion-quick script. Note that this may use outdated command logic.

    To start the environment:

    ~/src/arion/run-arion-quick up -d

    If you need to update the arion command logic for the next run, remove the cached result:

    rm ~/src/arion/result-run-arion-quick
    ~/src/arion/run-arion-quick up -d
    rm ~/src/arion/result-run-arion-quick
  7. Configure virtualisation.arion for NixOS

    main

    When using the NixOS module, use the virtualisation.arion attribute set to define your deployment settings:

    • backend: Specifies the container runtime socket to use (e.g., "podman-socket" or "docker").
    • projects.<name>: A set of project definitions.
      • serviceName: (Optional) The systemd service name. If not provided, it defaults to arion-<project-name>.
      • settings: A set of settings for the project. You can use imports to pull in your project configuration from a file (e.g., imports = [ ./arion-compose.nix ];). Note that these settings will use the host's pkgs rather than a project-specific arion-pkgs.nix.
  8. Arion Common CLI Options

    main

    These options can be applied to most Arion commands to configure the Nix evaluation and Docker Compose behavior.

    • -f, --file FILE: Specify one or more configuration files. If multiple are provided, they are merged. Defaults to ./arion-compose.nix.
    • -p, --pkgs EXPR: Use the Nix expression EXPR to obtain the Nixpkgs attribute set used for bootstrapping and evaluation. Defaults to ./arion-pkgs.nix.
    • --nix-arg ARG: Pass an extra argument to the Nix evaluator. Example: --nix-arg --option --nix-arg substitute --nix-arg false.
    • --prebuilt-file JSONFILE: Use a prebuilt JSON file instead of evaluating Nix expressions. This causes other evaluation-related options to be ignored.
    • --no-ansi: Avoid ANSI control sequences in output.
    • --compatibility: If set, Docker Compose will attempt to convert deploy keys in v3 files to their non-Swarm equivalent.
    • --log-level LEVEL: Set the log level to DEBUG, INFO, WARNING, ERROR, or CRITICAL.
    # Example usage with custom files and nix args
    arion -f custom-compose.nix -p custom-pkgs.nix --nix-arg --option flag true up
  9. Arion Docker Compose Commands

    main

    Arion passes many standard Docker Compose commands through to the underlying engine. These are grouped by their effect on the lifecycle of the services:

    Build/Lifecycle Commands:

    • build: Build or rebuild services.
    • bundle: Generate a Docker bundle from the Compose file.
    • create: Create services.
    • pull: Pull service images.
    • push: Push service images.
    • restart: Restart services.
    • run: Run a one-off command.
    • scale: Set number of containers for a service.
    • start: Start services.
    • up: Create and start containers.

    Management/State Commands:

    • config: Validate and view the Compose file.
    • down: Stop and remove containers, networks, images, and volumes.
    • events: Receive real-time events from containers.
    • images: List images.
    • kill: Kill containers.
    • logs: View output from containers.
    • pause: Pause services.
    • port: Print the public port for a port binding.
    • ps: List containers.
    • rm: Remove stopped containers.
    • stop: Stop services.
    • top: Display the running processes.
    • unpause: Unpause services.

    Utility Commands:

    • help: Get help on a command.
    • version: Show the Docker-Compose version information.
  10. Execute commands in containers with `exec`

    main

    The exec command allows you to run a command inside a running service container.

    Usage: arion exec [OPTIONS] SERVICE [COMMAND] [ARGS...]

    Options:

    • -d, --detach: Run the command in the background.
    • --privileged: Give extended privileges to the process.
    • -u, --user USER: Run the command as this user.
    • -T: Disable pseudo-tty allocation.
    • --index INDEX: Index of the container if there are multiple instances of a service (must be >= 1).
    • -e, --env KEY=VALUE: Set environment variables (can be used multiple times).
    • -w, --workdir DIR: Working directory in which to start the command in the container.

    If no COMMAND is provided, Arion attempts to use the defaultExec defined in the service configuration. If that is also missing, it defaults to /bin/sh.

    # Run a command as root in the 'web' service
    arion exec --user root web ls /app
    
    # Run a detached command with an environment variable
    arion exec -d -e DEBUG=true web python manage.py migrate
    
    # Run in a specific container instance (index 2)
    arion exec --index 2 web top