Skip to content

Commit

Permalink
Merge pull request #5348 from dokku/5338-nixpacks
Browse files Browse the repository at this point in the history
Add nixpacks builder support
  • Loading branch information
josegonzalez committed Oct 15, 2023
2 parents 728b81f + 5074da6 commit f755d3a
Show file tree
Hide file tree
Showing 21 changed files with 648 additions and 5 deletions.
155 changes: 155 additions & 0 deletions docs/deployment/builders/nixpacks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Nixpacks

> New as of 0.32.0
The `nixpacks` builder builds apps via [Nixpacks](https://nixpacks.com/), a buildpack alternative.

## Usage

### Requirements

The `nixpacks` cli tool is not included by default with Dokku or as a dependency. It must also be installed as shown on [this page](https://nixpacks.com/docs/install#debian-(and-derivatives-like-ubuntu)).

Builds will proceed with the `nixpacks` cli for the app from then on.

### Detection

This builder will be auto-detected in the following case:

- A `nixpacks.toml` exists in the root of the app repository.

The builder may also be selected via the `builder:set` command

```shell
dokku builder:set node-js-app selected nixpacks
```

### Supported languages

See the [upstream nixpacks documentation](https://nixpacks.com/docs) for further information on what languages and frameworks are supported.

### Build-time configuration variables

For security reasons - and as per [Docker recommendations](https://github.com/docker/docker/issues/13490) - nixpacks-based deploys have variables available only during runtime.

For users that require customization in the `build` phase, you may use build arguments via the [docker-options plugin](/docs/advanced-usage/docker-options.md). All environment variables set by the `config` plugin are automatically exported within the nixpacks build environment, and thus `--env` only requires setting a key without a value.

```shell
dokku docker-options:add node-js-app build '--env NODE_ENV'
```

Alternatively, a full value may be provided in the form of `--env KEY=VALUE`:

```shell
dokku docker-options:add node-js-app build '--env NODE_ENV=production'
```

### Changing the `nixpacks.toml` location

When deploying a monorepo, it may be desirable to specify the specific path of the `nixpacks.toml` file to use for a given app. This can be done via the `builder-nixpacks:set` command. If a value is specified and that file does not exist in the app's build directory, then the build will fail.

```shell
dokku builder-nixpacks:set node-js-app nixpackstoml-path nixpacks2.toml
```

The default value may be set by passing an empty value for the option:

```shell
dokku builder-nixpacks:set node-js-app nixpackstoml-path
```

The `nixpackstoml-path` property can also be set globally. The global default is `nixpacks.toml`, and the global value is used when no app-specific value is set.

```shell
dokku builder-nixpacks:set --global nixpackstoml-path nixpacks2.toml
```

The default value may be set by passing an empty value for the option.

```shell
dokku builder-nixpacks:set --global nixpackstoml-path
```

### Disabling cache

Cache is enabled by default, but can be disabled by setting the `no-cache` property to `true`:

```shell
dokku builder-nixpacks:set node-js-app no-cache true
```

The default value may be set by passing an empty value for the option:

```shell
dokku builder-nixpacks:set node-js-app no-cache
```

The `no-cache` property can also be set globally. The global default is `false`, and the global value is used when no app-specific value is set.

```shell
dokku builder-nixpacks:set --global no-cache true
```

The default value may be set by passing an empty value for the option.

```shell
dokku builder-nixpacks:set --global no-cache
```

### Displaying builder-nixpacks reports for an app

You can get a report about the app's storage status using the `builder-nixpacks:report` command:

```shell
dokku builder-nixpacks:report
```

```
=====> node-js-app builder-nixpacks information
Builder-nixpacks computed nixpackstoml path: nixpacks2.toml
Builder-nixpacks global nixpackstoml path: nixpacks.toml
Builder-nixpacks nixpackstoml path: nixpacks2.toml
Builder-nixpacks computed no cache: true
Builder-nixpacks global no cache: false
Builder-nixpacks no cache: true
=====> python-sample builder-nixpacks information
Builder-nixpacks computed nixpackstoml path: nixpacks.toml
Builder-nixpacks global nixpackstoml path: nixpacks.toml
Builder-nixpacks nixpackstoml path:
Builder-nixpacks computed no cache: false
Builder-nixpacks global no cache: false
Builder-nixpacks no cache:
=====> ruby-sample builder-nixpacks information
Builder-nixpacks computed nixpackstoml path: nixpacks.toml
Builder-nixpacks global nixpackstoml path: nixpacks.toml
Builder-nixpacks nixpackstoml path:
Builder-nixpacks computed no cache: false
Builder-nixpacks global no cache: false
Builder-nixpacks no cache:
```

You can run the command for a specific app also.

```shell
dokku builder-nixpacks:report node-js-app
```

```
=====> node-js-app builder-nixpacks information
Builder-nixpacks computed nixpackstoml path: nixpacks2.toml
Builder-nixpacks global nixpackstoml path: nixpacks.toml
Builder-nixpacks nixpackstoml path: nixpacks2.toml
Builder-nixpacks computed no cache: true
Builder-nixpacks global no cache: false
Builder-nixpacks no cache: true
```

You can pass flags which will output only the value of the specific information you want. For example:

```shell
dokku builder-nixpacks:report node-js-app --builder-nixpacks-no-cache
```

```
true
```
67 changes: 67 additions & 0 deletions plugins/builder-nixpacks/builder-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/internal-functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

trigger-builder-nixpacks-builder-build() {
declare desc="builder-nixpacks builder-build plugin trigger"
declare trigger="builder-build"
declare BUILDER_TYPE="$1" APP="$2" SOURCECODE_WORK_DIR="$3"

if [[ "$BUILDER_TYPE" != "nixpacks" ]]; then
return
fi

dokku_log_info1 "Building $APP from nixpacks"

if ! command -v "nixpacks" &>/dev/null; then
dokku_log_fail "Missing nixpacks, install it"
fi

local IMAGE=$(get_app_image_name "$APP")
local DOCKER_BUILD_LABEL_ARGS=("--label=dokku" "--label=org.label-schema.schema-version=1.0" "--label=org.label-schema.vendor=dokku" "--label=com.dokku.image-stage=build" "--label=com.dokku.builder-type=dockerfile" "--label=com.dokku.app-name=$APP")

pushd "$SOURCECODE_WORK_DIR" &>/dev/null

plugn trigger pre-build "$BUILDER_TYPE" "$APP" "$SOURCECODE_WORK_DIR"

no_cache="$(fn-builder-nixpacks-computed-no-cache "$APP")"
NIXPACKS_ARGS=""
if [[ "$no_cache" == "true" ]]; then
NIXPACKS_ARGS="$NIXPACKS_ARGS --no-cache"
fi

local DOCKER_ARGS=$(: | plugn trigger docker-args-build "$APP" "$BUILDER_TYPE")
DOCKER_ARGS+=$(: | plugn trigger docker-args-process-build "$APP" "$BUILDER_TYPE")

# strip --link, --volume and -v args from DOCKER_ARGS
local DOCKER_ARGS=$(sed -e "s/^--link=[[:graph:]]\+[[:blank:]]\?//g" -e "s/^--link[[:blank:]]\?[[:graph:]]\+[[:blank:]]\?//g" -e "s/^--volume=[[:graph:]]\+[[:blank:]]\?//g" -e "s/^--volume[[:blank:]]\?[[:graph:]]\+[[:blank:]]\?//g" -e "s/^-v[[:blank:]]\?[[:graph:]]\+[[:blank:]]\?//g" <<<"$DOCKER_ARGS")
declare -a ARG_ARRAY
eval "ARG_ARRAY=($DOCKER_ARGS)"

eval "$(config_export app "$APP" --merged)"

if [[ -f "$SOURCECODE_WORK_DIR/Procfile" ]]; then
if procfile-util exists --process-type release; then
procfile-util delete --process-type release
fi
fi

# shellcheck disable=SC2086
if ! nixpacks build "${DOCKER_BUILD_LABEL_ARGS[@]}" $DOKKU_GLOBAL_BUILD_ARGS "${ARG_ARRAY[@]}" $NIXPACKS_ARGS --name "$IMAGE" "$SOURCECODE_WORK_DIR"; then
dokku_log_warn "Failure building image"
return 1
fi

if ! suppress_output "$DOCKER_BIN" image build -f "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/dockerfiles/builder-build.Dockerfile" --build-arg APP_IMAGE="$IMAGE" -t "$IMAGE" "$SOURCECODE_WORK_DIR"; then
dokku_log_warn "Failure injecting docker labels and custom entrypoint on image"
return 1
fi

plugn trigger post-build "$BUILDER_TYPE" "$APP" "$SOURCECODE_WORK_DIR"
popd &>/dev/null || pushd "/tmp" >/dev/null
}

trigger-builder-nixpacks-builder-build "$@"
17 changes: 17 additions & 0 deletions plugins/builder-nixpacks/builder-detect
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/config/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

trigger-builder-nixpacks-builder-detect() {
declare desc="builder-nixpacks builder-detect plugin trigger"
declare trigger="builder-detect"
declare APP="$1" SOURCECODE_WORK_DIR="$2"

if [[ -f "$SOURCECODE_WORK_DIR/nixpacks.toml" ]]; then
echo "nixpacks"
return
fi
}

trigger-builder-nixpacks-builder-detect "$@"
31 changes: 31 additions & 0 deletions plugins/builder-nixpacks/builder-release
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

trigger-builder-nixpacks-builder-release() {
declare desc="builder-nixpacks builder-release plugin trigger"
declare trigger="builder-release"
declare BUILDER_TYPE="$1" APP="$2" IMAGE_TAG="$3"

if [[ "$BUILDER_TYPE" != "nixpacks" ]]; then
return
fi

local DOCKER_BUILD_LABEL_ARGS="--label=org.label-schema.schema-version=1.0 --label=org.label-schema.vendor=dokku --label=com.dokku.app-name=$APP --label=com.dokku.image-stage=release --label=dokku"

plugn trigger pre-release-builder "$BUILDER_TYPE" "$APP" "$IMAGE"

TMP_WORK_DIR="$(mktemp -d "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX")"
trap "rm -rf '$TMP_WORK_DIR' >/dev/null" RETURN INT TERM EXIT

local IMAGE=$(get_app_image_name "$APP" "$IMAGE_TAG")
if ! suppress_output "$DOCKER_BIN" image build "${DOCKER_BUILD_LABEL_ARGS[@]}" $DOKKU_GLOBAL_BUILD_ARGS -f "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/dockerfiles/builder-release.Dockerfile" --build-arg APP_IMAGE="$IMAGE" -t "$IMAGE" "$TMP_WORK_DIR"; then
dokku_log_warn "Failure injecting docker labels on image"
return 1
fi

plugn trigger post-release-builder "$BUILDER_TYPE" "$APP" "$IMAGE"
}

trigger-builder-nixpacks-builder-release "$@"
15 changes: 15 additions & 0 deletions plugins/builder-nixpacks/commands
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/help-functions"

case "$1" in
help | builder-nixpacks:help)
cmd-builder-nixpacks-help "$@"
;;

*)
exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
;;

esac
30 changes: 30 additions & 0 deletions plugins/builder-nixpacks/core-post-extract
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

trigger-builder-nixpacks-core-post-extract() {
declare desc="builder-nixpacks post-extract plugin trigger"
declare trigger="post-extract"
declare APP="$1" SOURCECODE_WORK_DIR="$2"
local NEW_NIXPACKS_YML="$(fn-builder-nixpacks-computed-nixpackstoml-path "$APP")"

pushd "$TMP_WORK_DIR" >/dev/null

if [[ -z "$NEW_NIXPACKS_YML" ]]; then
return
fi

if [[ ! -f "$NEW_NIXPACKS_YML" ]]; then
rm -f nixpacks.toml
return
fi

if [[ "$NEW_NIXPACKS_YML" != "nixpacks.toml" ]]; then
mv "$NEW_NIXPACKS_YML" nixpacks.toml
fi
popd &>/dev/null || pushd "/tmp" >/dev/null
}

trigger-builder-nixpacks-core-post-extract "$@"
7 changes: 7 additions & 0 deletions plugins/builder-nixpacks/dockerfiles/builder-build.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ARG APP_IMAGE
FROM $APP_IMAGE

RUN printf '#!/usr/bin/env bash\nexec bash -l -c -- \"$*\"\n' > /usr/local/bin/entrypoint && \
chmod +x /usr/local/bin/entrypoint

ENTRYPOINT ["/usr/local/bin/entrypoint"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ARG APP_IMAGE
FROM $APP_IMAGE
33 changes: 33 additions & 0 deletions plugins/builder-nixpacks/help-functions
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

cmd-builder-nixpacks-help() {
declare desc="help command"
declare CMD="$1"
local plugin_name="builder-nixpacks"
local plugin_description="Manage the nixpacks builder integration for an app"

if [[ "$CMD" == "${plugin_name}:help" ]]; then
echo -e "Usage: dokku ${plugin_name}[:COMMAND]"
echo ''
echo "$plugin_description"
echo ''
echo 'Additional commands:'
fn-help-content | sort | column -c2 -t -s,
elif [[ $(ps -o command= $PPID) == *"--all"* ]]; then
fn-help-content
else
cat <<help_desc
$plugin_name, $plugin_description
help_desc
fi
}

fn-help-content() {
declare desc="return help content"
cat <<help_content
builder-nixpacks:report [<app>] [<flag>], Displays a builder-nixpacks report for one or more apps
builder-nixpacks:set <app> <property> (<value>), Set or clear a builder-nixpacks property for an app
help_content
}
16 changes: 16 additions & 0 deletions plugins/builder-nixpacks/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

trigger-builder-nixpacks-install() {
declare desc="installs the builder-nixpacks plugin"
declare trigger="install"

fn-plugin-property-setup "builder-nixpacks"

mkdir -p "${DOKKU_LIB_ROOT}/data/builder-nixpacks"
chown -R "${DOKKU_SYSTEM_USER}:${DOKKU_SYSTEM_GROUP}" "${DOKKU_LIB_ROOT}/data/builder-nixpacks"
}

trigger-builder-nixpacks-install "$@"

0 comments on commit f755d3a

Please sign in to comment.