Skip to content

Commit

Permalink
Setup app to call script via nix run
Browse files Browse the repository at this point in the history
  • Loading branch information
sestrella committed Nov 9, 2023
1 parent 5539fd7 commit fed8db0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
28 changes: 24 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@
versions = builtins.fromJSON (builtins.readFile ./versions.json);
in
{
apps.update-versions = {
type = "app";
program =
let
python3 = pkgs-unstable.python3.withPackages (ps: [
ps.pygithub
ps.semver
]);
script = pkgs-unstable.writeShellApplication {
name = "update-versions";
runtimeInputs = [ pkgs-unstable.nix-prefetch ];
text = ''
${python3}/bin/python3 ${./update-versions.py} ${./vendor-hash.nix}
'';
};
in
"${script}/bin/update-versions";
};
# https://github.com/NixOS/nix/issues/7165
checks = self.packages.${system};
packages = builtins.listToAttrs
Expand All @@ -32,11 +50,13 @@
(builtins.attrNames versions));
devShell = pkgs.mkShell {
buildInputs = [
pkgs-unstable.python3
pkgs-unstable.python3Packages.pygithub
pkgs-unstable.python3Packages.semver
(pkgs-unstable.python3.withPackages
(ps: [
ps.pygithub
ps.semver
])
)
pkgs-unstable.nix-prefetch
pkgs-unstable.nix-prefetch-git
];
};
}) // {
Expand Down
41 changes: 23 additions & 18 deletions bin/update_versions → update-versions.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/env python3

# vi: ft=python

import argparse
import functools
import os
from re import sub
import github
import json
import os
import pathlib
import semver
import subprocess

Expand All @@ -17,19 +16,22 @@ def read_versions():

def is_stable(release):
version = release.tag_name.removeprefix("v")
return semver.compare(version, "1.5.6") >= 0 and not (release.draft or release.prerelease)
return semver.compare(version, "1.5.5") >= 0 and not (release.draft or release.prerelease)

def by_version(release):
return release.tag_name.removeprefix("v").split(".")

def to_version(versions, release):
version = release.tag_name.removeprefix("v")
calculated_hash = calculate_hash(version)
versions[version] = {
"hash": calculated_hash,
"vendorHash": calculate_vendor_hash(version, calculated_hash)
}
return versions
def to_version(vendor_hash_file):
def add_version(versions, release):
version = release.tag_name.removeprefix("v")
calculated_hash = calculate_hash(version)
versions[version] = {
"hash": calculated_hash,
"vendorHash": calculate_vendor_hash(vendor_hash_file, version, calculated_hash)
}
return versions

return add_version

def calculate_hash(version):
current_hash = current_versions.get(version, {}).get("hash")
Expand All @@ -48,7 +50,7 @@ def calculate_hash(version):
f"v{version}"
])

def calculate_vendor_hash(version, calculated_hash):
def calculate_vendor_hash(vendor_hash_file, version, calculated_hash):
current_vendor_hash = current_versions.get(version, {}).get("vendorHash")
if current_vendor_hash:
print(f"Using existing vendorHash for {version}")
Expand All @@ -57,7 +59,7 @@ def calculate_vendor_hash(version, calculated_hash):
print(f"Calculating vendorHash for {version}")
return nix_prefetch([
"--file",
"./vendor_hash.nix",
vendor_hash_file.resolve(),
"--argstr",
"version",
version,
Expand All @@ -69,12 +71,15 @@ def calculate_vendor_hash(version, calculated_hash):
def nix_prefetch(args):
return subprocess.check_output([
"nix-prefetch",
"--silent",
# "--silent",
"--option",
"extra-experimental-features",
"flakes",
] + args, text=True).strip()

parser = argparse.ArgumentParser(description='Update versions.json file')
parser.add_argument("vendor_hash_file", type=pathlib.Path)
args = parser.parse_args()

auth = github.Auth.Token(os.environ["GITHUB_TOKEN"])
g = github.Github(auth=auth)
Expand All @@ -86,6 +91,6 @@ def nix_prefetch(args):
# https://endoflife.date/api/terraform.json
releases = list(filter(is_stable, repo.get_releases().get_page(0)))
releases.sort(reverse=True,key=by_version)
versions = functools.reduce(to_version, releases, {})
versions = functools.reduce(to_version(args.vendor_hash_file), releases, {})
with open("versions.json", "w") as f:
json.dump(versions, f, indent=2)
File renamed without changes.

0 comments on commit fed8db0

Please sign in to comment.