MCPcopy Index your code
hub / github.com/chainguard-dev/melange

github.com/chainguard-dev/melange @v0.56.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.56.1 ↗ · + Follow
802 symbols 2,718 edges 118 files 342 documented · 43%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

melange

Build apk packages using declarative pipelines.

Commonly used to provide custom packages for container images built with apko. The majority of apks are built for use with either the Wolfi or Alpine Linux ecosystems.

Key features:

  • Pipeline-oriented builds. Every step of the build pipeline is defined and controlled by you, unlike traditional package managers which have distinct phases.
  • Multi-architecture by default. QEMU is used to emulate various architectures, avoiding the need for cross-compilation steps.

Why

Secure software factories are the evolution of DevOps, allowing a user to prove the provenance of all artifacts incorporated into a software appliance. By building and capturing software artifacts into packages, DevOps teams can manage their software artifacts as if they were any other component of an image.

This is especially useful when building software appliances in the form of OCI container images with apko.

Installation

You can install Melange from Homebrew:

brew install melange

You can also install Melange from source:

go install chainguard.dev/melange@latest

You can also use the Melange container image:

docker run cgr.dev/chainguard/melange version

To use the examples, you'll generally want to mount your current directory into the container and provide elevated privileges, e.g.:

docker run --privileged -v "$PWD":/work cgr.dev/chainguard/melange build examples/gnu-hello.yaml

Running outside of a container requires Docker, but should also work with other runtimes such as podman.

Quickstart

A melange build file looks like:

package:
  name: hello
  version: 2.12
  epoch: 0
  description: "the GNU hello world program"
  copyright:
    - attestation: |
        Copyright 1992, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2005,
        2006, 2007, 2008, 2010, 2011, 2013, 2014, 2022 Free Software Foundation,
        Inc.
      license: GPL-3.0-or-later
  dependencies:
    runtime:

environment:
  contents:
    repositories:
      - https://dl-cdn.alpinelinux.org/alpine/edge/main
    packages:
      - alpine-baselayout-data
      - busybox
      - build-base
      - scanelf
      - ssl_client
      - ca-certificates-bundle

pipeline:
  - uses: fetch
    with:
      uri: https://ftp.gnu.org/gnu/hello/hello-${{package.version}}.tar.gz
      expected-sha256: cf04af86dc085268c5f4470fbae49b18afbc221b78096aab842d934a76bad0ab
  - uses: autoconf/configure
  - uses: autoconf/make
  - uses: autoconf/make-install
  - uses: strip

subpackages:
  - name: "hello-doc"
    description: "Documentation for hello"
    dependencies:
      runtime:
        - foo
    pipeline:
      - uses: split/manpages
    test:
      pipeline:
        - uses: test/docs

test:
  environment:
    contents:
      packages:
        - bar
  pipeline:
    - runs: |
        hello
        hello --version

We can build this with:

melange build examples/gnu-hello.yaml

or, with Docker:

docker run --privileged --rm -v "${PWD}":/work \
  cgr.dev/chainguard/melange build examples/gnu-hello.yaml

This will create a packages folder, with an entry for each architecture supported by the package. If you only want to build for the current architecture, you can add --arch $(uname -m) to the build command. Inside the architecture directory you should find apk files for each package built in the pipeline.

If you want to sign your apks, create a signing key with the melange keygen command:

melange keygen
 generating keypair with a 4096 bit prime, please wait...
 wrote private key to melange.rsa
 wrote public key to melange.rsa.pub

And then pass the --signing-key argument to melange build.

Debugging melange Builds

To include debug-level information on melange builds, edit your melange.yaml file and include set -x in your pipeline. You can add this flag at any point of your pipeline commands to further debug a specific section of your build.

...
pipeline:
  - name: Build Minicli application
    runs: |
      set -x
      APP_HOME="${{targets.destdir}}/usr/share/hello-minicli"
...

Default Substitutions

Melange provides the following default substitutions which can be referenced in the build file pipeline:

Substitution Description
${{package.name}} Package name
${{package.version}} Package version
${{package.epoch}} Package epoch
${{package.full-version}} ${{package.version}}-r${{package.epoch}}
${{package.description}} Package description
${{package.srcdir}} Package source directory (--source-dir)
${{subpkg.name}} Subpackage name
${{context.name}} main package or subpackage name
${{targets.outdir}} Directory where targets will be stored
${{targets.contextdir}} Directory where targets will be stored for main packages and subpackages
${{targets.destdir}} Directory where targets will be stored for main
${{targets.subpkgdir}} Directory where targets will be stored for subpackages
${{build.arch}} Architecture of current build (e.g. x86_64, aarch64)
${{build.goarch}} GOARCH of current build (e.g. amd64, arm64)

An example build file pipeline with substitutions:

pipeline:
  - name: 'Create tmp dir'
    runs: mkdir ${{targets.destdir}}/var/lib/${{package.name}}/tmp

More detailed documentation

Usage with apko

To use a melange built apk in apko, either upload it to a package repository or use a "local" repository. Using a local repository allows a melange build and apko build to run in the same directory (or GitHub repo) without using external storage. An example of this approach can be seen in the nginx-image-demo repo.

Extension points exported contracts — how you extend this code

VersionHandler (Interface)
VersionHandler is an interface that defines methods for retrieving version filtering and stripping parameters. It is use [4 …
pkg/config/config.go
SCAHandle (Interface)
SCAHandle represents all of the state necessary to analyze a package. [2 implementers]
pkg/sca/sca.go
Debugger (Interface)
(no doc) [3 implementers]
pkg/container/runner.go
Classifier (Interface)
NOTE: the detection logic is done via a Classifier type as this is how it was implemented, for instance, in the go-licen [1 …
pkg/license/license.go
Element (Interface)
Element represents any referenceable entity in an SBOM. [1 implementers]
pkg/sbom/element.go
Renovator (FuncType)
Renovator performs a renovation.
pkg/renovate/renovate.go
VariableLookupFunction (FuncType)
A VariableLookupFunction designates how variables should be resolved when evaluating expressions.
pkg/cond/parser.go
ApkSigner (Interface)
(no doc) [2 implementers]
pkg/sign/apk.go

Core symbols most depended-on inside this repo

Close
called by 134
pkg/container/runner.go
TempDir
called by 72
pkg/container/runner.go
Run
called by 52
pkg/container/runner.go
Error
called by 35
pkg/config/config.go
Close
called by 34
pkg/container/qemu_runner.go
ParseConfiguration
called by 27
pkg/config/config.go
Name
called by 23
pkg/container/runner.go
NodeFromMapping
called by 22
pkg/renovate/yit.go

Shape

Function 464
Method 197
Struct 117
FuncType 11
Interface 10
TypeAlias 3

Languages

Go100%
C1%

Modules by API surface

pkg/config/config.go126 symbols
pkg/container/qemu_runner.go63 symbols
pkg/config/config_test.go36 symbols
pkg/sca/sca.go31 symbols
pkg/container/qemu_runner_test.go25 symbols
pkg/sca/sca_test.go22 symbols
pkg/linter/types/types.go20 symbols
pkg/cli/scan.go20 symbols
pkg/container/observability_test.go18 symbols
pkg/container/docker/docker_runner.go18 symbols
pkg/container/bubblewrap_runner.go18 symbols
pkg/container/qemu_ssh_test.go17 symbols

For agents

$ claude mcp add melange \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact