copy hackage translator

This commit is contained in:
Erin 2022-12-29 10:44:25 -06:00
parent 97a7712e18
commit b3edf6c842
2 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,110 @@
{
pkgs,
utils,
translators,
...
}: {
type = "impure";
# A derivation which outputs a single executable at `$out`.
# The executable will be called by dream2nix for translation
# The input format is specified in /specifications/translator-call-example.json.
# The first arg `$1` will be a json file containing the input parameters
# like defined in /src/specifications/translator-call-example.json and the
# additional arguments required according to extraArgs
#
# The program is expected to create a file at the location specified
# by the input parameter `outFile`.
# The output file must contain the dream lock data encoded as json.
# See /src/specifications/dream-lock-example.json
translateBin =
utils.writePureShellScript
(with pkgs; [
bash
coreutils
curl
gnutar
gzip
haskellPackages.cabal-install
haskellPackages.ghc
jq
moreutils
nix
python3
util-linux
])
''
# accroding to the spec, the translator reads the input from a json file
jsonInput=$1
# read the json input
outputFile=$(realpath -m $(jq '.outputFile' -c -r $jsonInput))
name=$(jq '.project.name' -c -r $jsonInput)
version=$(jq '.project.version' -c -r $jsonInput)
source=$(jq '.source' -c -r $jsonInput)
relPath=$(jq '.project.relPath' -c -r $jsonInput)
# update the cabal index if older than 1 day
(
flock 9 || exit 1
# ... commands executed under lock ...
cabalIndex="$HOME/.cabal/packages/hackage.haskell.org/01-index.cache"
set -x
if [ -e "$cabalIndex" ]; then
indexTime=$(stat -c '%Y' "$cabalIndex")
age=$(( $(date +%s) - $indexTime ))
if [ "$age" -gt "$((60*60*24))" ]; then
cabal update
fi
else
cabal update
fi
) 9>/tmp/cabal-lock
pushd $TMPDIR
# download and unpack package source
mkdir source
url="https://hackage.haskell.org/package/$name-$version/$name-$version.tar.gz"
echo "downloading $url"
curl -L "$url" > $TMPDIR/tarball
cd source
cat $TMPDIR/tarball | tar xz --strip-components 1
# trigger creation of `dist-newstyle` directory
cabal freeze
cd -
# generate arguments for cabal-plan translator
echo "{
\"source\": \"$TMPDIR/source\",
\"outputFile\": \"$outputFile\",
\"project\": {
\"relPath\": \"\",
\"subsystemInfo\": {}
}
}" > $TMPDIR/newJsonInput
popd
# execute cabal-plan translator
${translators.cabal-plan.finalTranslateBin} $TMPDIR/newJsonInput
# finalize dream-lock. Add source and export default package
# set correct package version under `packages`
export version
export hash=$(sha256sum $TMPDIR/tarball | cut -d " " -f 1)
cat $outputFile \
| python3 ${./fixup-dream-lock.py} $TMPDIR/sourceInfo.json \
| sponge $outputFile
'';
# If the translator requires additional arguments, specify them here.
# When users run the CLI, they will be asked to specify these arguments.
# There are only two types of arguments:
# - string argument (type = "argument")
# - boolean flag (type = "flag")
# String arguments contain a default value and examples. Flags do not.
extraArgs = {
};
}

View File

@ -0,0 +1,17 @@
import json
import os
import sys
lock = json.load(sys.stdin)
version = os.environ.get('version')
hash = os.environ.get('hash')
# set default package version correctly
name = lock['_generic']['defaultPackage']
lock['sources'][name][version] = dict(
type="http",
url=f"https://hackage.haskell.org/package/{name}-{version}/{name}-{version}.tar.gz",
hash=f"sha256:{hash}",
)
print(json.dumps(lock, indent=2))