Skip to content

Instantly share code, notes, and snippets.

@nrdxp
Last active May 11, 2022 21:44
Show Gist options
  • Save nrdxp/33e22cfb7d9afdef5e6c5b909bc6834d to your computer and use it in GitHub Desktop.
Save nrdxp/33e22cfb7d9afdef5e6c5b909bc6834d to your computer and use it in GitHub Desktop.
SBT lock file
REPO_DIR="$(git rev-parse --show-toplevel)"
LOCK_FILE="$REPO_DIR/nix/cloud/packages/<ommited>/deps.lock"
cd $REPO_DIR
# We want our lock file to be reproducible so ensure the cache from previous runs is removed
rm -rf .nix
# This is for CI to check if the lockfile has changed
if eval __argCheck__; then
lock_hash=$(nix hash file "$LOCK_FILE")
fi
# run the sbt-derivation cache warmup command, but due so outside of the confines of a Nix build
# to avoid building the project to fetch dependencies, see this issue: https://github.com/zaninime/sbt-derivation/issues/8
# tl;dr: our `depsWarmupCommand` is set to `sbt "dependencyList ; consoleQuick" <<< ":quit"`
nix develop -i "$REPO_DIR#<ommited>.depsDerivation" --accept-flake-config -c -- bash -c 'eval "runHook () { :; }; ${buildPhase:-buildPhase}"'
# The previous command sets all the SBT directories to live under `.nix` so the cache lives here
cd .nix/coursier-cache/https
# We only need jars, xml for ivy packages, pom for maven packages, and sha1's for packages that include them
deps=($(fd -e pom -e xml -e jar -e sha1 -t f . --strip-cwd-prefix))
for dep in "${deps[@]}"; do
(
# the filepath of "$dep" is almost the entire remote url. All we need is to prepend "https://" to get a valid url to the resource
echo '{"fetch": {"url": "https://'"$dep"'", "sha256": "'$(nix hash file "$dep")'"}, "path": "'"$dep"'"}'
# We need to fetch compiler bridge sources as well
if [[ ${dep##*/} =~ compiler-bridge && ${dep##*.} == "jar" ]] && ! [[ $dep =~ -sources.jar ]]; then
dep="${dep%.*}-sources.jar"
if ! [[ -f $dep ]]; then
echo '{"fetch": {"url": "https://'"$dep"'", "sha256": "'"$(curl --silent https://$dep | nix hash file /dev/stdin)"'"}, "path": "'"$dep"'"}'
fi
fi
) &
done | jq -c -s 'sort_by(.path) | . | unique' >"$LOCK_FILE"
if [[ -v lock_hash ]]; then
if [[ $(nix hash file "$LOCK_FILE") == "$lock_hash" ]]; then
exit 0
else
echo >&2 "error: lock file is out of date"
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment