Skip to content

Instantly share code, notes, and snippets.

@adnelson
Last active December 22, 2015 17:29
Show Gist options
  • Save adnelson/cf85f487c8ebeabcf900 to your computer and use it in GitHub Desktop.
Save adnelson/cf85f487c8ebeabcf900 to your computer and use it in GitHub Desktop.
{pkgs}:
{
# Fetches a tag from a github repo.
fetchRepo = {owner, repo, tag, sha256, requireToken ? false}:
let
url = "https://api.github.com/repos/${owner}/${repo}/tarball/${tag}";
in
pkgs.stdenv.mkDerivation {
inherit requireToken;
name = "${repo}-${tag}-snapshot.tar.gz";
buildInputs = [pkgs.curl pkgs.cacert];
# Environment variable read by curl
CURL_CA_BUNDLE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
outputHash = sha256;
outputHashAlgo = "sha256";
outputHashMode = "flat";
github_token = getEnv "GITHUB_TOKEN";
buildCommand = ''
if [ -z $github_token ] && [ ! -z $requireToken ]; then
echo "Please set the GITHUB_TOKEN environment variable."
exit 1
elif ! [ -z $github_token ]; then
curl --fail -fL -H "Authorization: token $github_token" ${url} > $out
else
curl --fail -fL ${url} > $out
fi
'';
};
# Uses the repo fetched from `fetchRepo` to pull the package source out.
fetchPathInRepo = {owner, repo, tag, pathInRepo, requireToken ? false, sha256}:
let
fetchedRepo = fetchRepo {inherit owner repo tag sha256 requireToken;};
in
pkgs.stdenv.mkDerivation {
name = "${tag}-folder";
buildCommand = ''
tar -xf ${fetchedRepo}
cp -r ${owner}-${repo}-*/${pathInRepo} $out
'';
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment