2024-03-20 14:19:37 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# SPDX-FileCopyrightText: 2023 The TVL Authors
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
set -ueo pipefail
|
|
|
|
|
|
|
|
# Evaluates a Nix expression.
|
|
|
|
#
|
|
|
|
# Receives input parameters as JSON from stdin.
|
|
|
|
# It expects a dict with the following keys:
|
|
|
|
#
|
|
|
|
# - `attrpath`: the attribute.path pointing to the expression to instantiate.
|
|
|
|
# Required.
|
|
|
|
# - `entrypoint`: the path to the Nix file to invoke.
|
|
|
|
# Optional. If omitted, will shell out to git to determine the repo root,
|
|
|
|
# and Nix will use `default.nix` in there.
|
2024-03-21 17:06:08 +01:00
|
|
|
# - `argstr_json`: A string JSON-encoding a map containing string keys and
|
|
|
|
# values which should be passed to Nix as `--argstr $key $value`.
|
2024-03-20 14:19:37 +01:00
|
|
|
# command line args. Optional.
|
2024-03-20 14:42:02 +01:00
|
|
|
# - `build`: A boolean (or string being "true" or "false") stating whether the
|
|
|
|
# expression should also be built/substituted on the machine executing this script.
|
2024-03-20 14:19:37 +01:00
|
|
|
#
|
|
|
|
# jq's @sh format takes care of escaping.
|
2024-03-21 17:06:08 +01:00
|
|
|
eval "$(jq -r '@sh "attrpath=\(.attrpath) && entrypoint=\(.entrypoint) && argstr=\((.argstr_json // "{}"|fromjson) | to_entries | map ("--argstr", .key, .value) | join(" ")) build=\(.build)"')"
|
2024-03-20 14:19:37 +01:00
|
|
|
|
|
|
|
# Evaluate the expression.
|
|
|
|
[[ -z "$entrypoint" ]] && entrypoint=$(git rev-parse --show-toplevel)
|
|
|
|
# shellcheck disable=SC2086,SC2154
|
|
|
|
drv=$(nix-instantiate -A "${attrpath}" "${entrypoint}" ${argstr})
|
|
|
|
|
2024-03-20 14:42:02 +01:00
|
|
|
# If `build` is set to true, invoke nix-build on the .drv.
|
|
|
|
# We need to swallow all stdout, to not garble the JSON printed later.
|
|
|
|
# shellcheck disable=SC2154
|
|
|
|
if [ "${build}" == "true" ]; then
|
|
|
|
nix-build --no-out-link "${drv}" > /dev/null
|
|
|
|
fi
|
|
|
|
|
2024-03-20 14:28:02 +01:00
|
|
|
# Determine the output path.
|
|
|
|
outPath=$(nix show-derivation "${drv}" | jq -r ".\"${drv}\".outputs.out.path")
|
|
|
|
|
2024-03-20 14:19:37 +01:00
|
|
|
# Return a JSON back to stdout.
|
|
|
|
# It contains the following keys:
|
|
|
|
#
|
|
|
|
# - `drv`: the store path of the Derivation that has been instantiated.
|
2024-03-20 14:28:02 +01:00
|
|
|
# - `outPath`: the output store path.
|
|
|
|
jq -n --arg drv "$drv" --arg outPath "$outPath" '{"drv":$drv, "outPath":$outPath}'
|