diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 000000000..75745800f --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,37 @@ +name: CI + +on: + push: + branches: + - master + workflow_dispatch: + +env: + NIXPKGS_ALLOW_BROKEN: 0 + +jobs: + build: + runs-on: ubuntu-latest + name: ${{ matrix.channel-branch }} + strategy: + matrix: + channel-branch: + - nixpkgs-unstable + + steps: + - uses: actions/checkout@v2 + with: + ref: master + - uses: cachix/install-nix-action@v15 + - name: Nix Version + run: nix --version + - name: Update lbstanza-bin + run: | + export NIX_PATH=nixpkgs=channel:${{ matrix.channel-branch }} + nix-shell nix/update-lbstanza-bin/shell.nix --command run_and_commit + - name: Build + run: | + export NIX_PATH=nixpkgs=channel:${{ matrix.channel-branch }} + nix-build --show-trace + - name: Run Version + run: ./result/bin/stanza version diff --git a/.gitignore b/.gitignore index 8045a76a9..1f1399bd0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +result .DS_Store logs build diff --git a/default.nix b/default.nix new file mode 100644 index 000000000..eea22ccc6 --- /dev/null +++ b/default.nix @@ -0,0 +1,5 @@ +{ pkgs ? import { } }: +let + myNix = import ./nix { inherit pkgs; }; +in +myNix.lbstanza diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 000000000..00b7dd810 --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,5 @@ +{ pkgs ? import { } }: +rec { + lbstanza-bin = with pkgs; callPackage ./lbstanza-bin { }; + lbstanza = with pkgs; callPackage ./lbstanza { inherit lbstanza-bin; }; +} diff --git a/nix/lbstanza-bin/default.nix b/nix/lbstanza-bin/default.nix new file mode 100644 index 000000000..2095b8f5d --- /dev/null +++ b/nix/lbstanza-bin/default.nix @@ -0,0 +1,35 @@ +{ lib +, stdenv +, fetchzip +, autoPatchelfHook +}: + +stdenv.mkDerivation rec { + pname = "lbstanza-bin"; + version = "0.15.1"; + + src = fetchzip { + url = "http://lbstanza.org/resources/stanza/lstanza_${lib.replaceStrings ["."] ["_"] version}.zip"; + sha256 = "sha256-Ogfe5HX+fk3Tth5J07fWngyHnQjbkYBxKObqsNY8spc="; + stripRoot=false; + }; + + nativeBuildInputs = [ autoPatchelfHook ]; + + installPhase = '' + runHook preInstall + + mkdir -p "$out/bin" + cp -r * "$out" + cp "$out/stanza" "$out/bin/stanza" + + runHook postInstall + ''; + + meta = with lib; { + description = "L.B. Stanza Programming Language - stage 1"; + homepage = "http://lbstanza.org/"; + maintainers = [ "Scott Hamilton " ]; + platforms = platforms.linux; + }; +} diff --git a/nix/lbstanza/default.nix b/nix/lbstanza/default.nix new file mode 100644 index 000000000..134b6062c --- /dev/null +++ b/nix/lbstanza/default.nix @@ -0,0 +1,51 @@ +{ lib +, stdenv +, fetchFromGitHub +, lbstanza-bin +, nix-gitignore +}: + +stdenv.mkDerivation rec { + pname = "lbstanza"; + version = "master"; + + src = nix-gitignore.gitignoreSource [] ../..; + + postPatch = '' + patchShebangs scripts/*.sh + ''; + + preBuild = '' + mkdir build + export HOME=$(pwd) + cat << EOF > .stanza + install-dir = "${lbstanza-bin}" + platform = linux + aux-file = "mystanza.aux" + EOF + ''; + + buildPhase = '' + runHook preBuild + ./scripts/make.sh stanza linux compile-clean + runHook postBuild + ''; + + installPhase = '' + runHook preInstall + mkdir -p "$out/bin" + cp -r * "$out" + mv "$out/lpkgs" "$out/pkgs" + mv "$out/lstanza" "$out/bin/stanza" + runHook postInstall + ''; + + nativeBuildInputs = [ lbstanza-bin ]; + + meta = with lib; { + description = "L.B. Stanza Programming Language"; + homepage = "http://lbstanza.org/"; + maintainers = [ "Scott Hamilton " ]; + platforms = platforms.linux; + }; +} diff --git a/nix/update-lbstanza-bin/shell.nix b/nix/update-lbstanza-bin/shell.nix new file mode 100644 index 000000000..c4b185fdc --- /dev/null +++ b/nix/update-lbstanza-bin/shell.nix @@ -0,0 +1,32 @@ +{ pkgs ? import {} }: +let + customPython = pkgs.python38.buildEnv.override { + extraLibs = with pkgs.python38Packages; [ + # dateutil + packaging + # requests + ]; + }; +in +with pkgs; mkShell { + buildInputs = [ + customPython + ]; + shellHook = '' + run(){ + python nix/update-lbstanza-bin/update-lbstanza-bin.py + } + # For the CI + commit(){ + git config --global user.name 'SCOTT-HAMILTON' + git config --global user.email 'sgn.hamilton+github@protonmail.com' + git add nix/lbstanza-bin/default.nix + git commit -m "Automated nix/lbstanza-bin CI update" + git push + } + run_and_commit(){ + run || commit + } + ''; +} + diff --git a/nix/update-lbstanza-bin/update-lbstanza-bin.py b/nix/update-lbstanza-bin/update-lbstanza-bin.py new file mode 100755 index 000000000..a25266e43 --- /dev/null +++ b/nix/update-lbstanza-bin/update-lbstanza-bin.py @@ -0,0 +1,70 @@ +import re +import subprocess +from packaging import version + +# returns 1 if we updated a file, 0 otherwise + +lbstanza_bin_nix_file = 'nix/lbstanza-bin/default.nix' + +notNone = lambda r: r != None +current_version = next(filter(notNone, \ + [re.search(r'.*STANZA-VERSION = (.*)',line) \ + for line in open('compiler/stz-params.stanza')])).group(1) \ + [1:][::-1][1:][::-1].replace(' ', '.') +print(f"current version={current_version}") +ci_version = next(filter(notNone, \ + [re.search(r'^ version = "(.*)"',line) \ + for line in open(lbstanza_bin_nix_file)])).group(1) +print(f"ci version used={ci_version}") +ci_sha256 = next(filter(notNone, \ + [re.search(r'^ sha256 = "(.*)"',line) \ + for line in open(lbstanza_bin_nix_file)])).group(1) +print(f"ci sha256 used={ci_sha256}") +vci_versio = version.parse(ci_version) +v_curversion = version.parse(current_version) + +def search_n_replace(s, r, file, f): + def read_replace(file): + with open(file, 'r') as file: + return f(file.read()) + data = read_replace(file) + with open(file, 'w') as file: + file.write(data) +def str_search_n_replace(s, r, file): + return search_n_replace(s, r, file, + lambda d: d.replace(s, r)) + +def is_sri(h): + return h[:7] == 'sha256-' +def to_empty_hash(h): + if is_sri(h): + return 'sha256-'+len(h[7:][::-1][1:][::-1])*'A'+'=' + else: + return len(h)*'0' + +def update_ci_sha256(): + stderr_out = subprocess.run(["nix-build", "nix", "-A", "lbstanza-bin"], + capture_output=True).stderr.decode('utf-8') + new_sha256 = re.search(r'got: *(.*)', stderr_out).group(1) + if not is_sri(new_sha256): + new_sha256 = new_sha256[7:] + str_search_n_replace( + to_empty_hash(ci_sha256), + new_sha256, + lbstanza_bin_nix_file) + +if vci_versio != v_curversion: + print("Needs Update") + str_search_n_replace( + ci_version, + current_version, + lbstanza_bin_nix_file) + str_search_n_replace( + ci_sha256, + to_empty_hash(ci_sha256), + lbstanza_bin_nix_file) + update_ci_sha256() + exit(1) +else: + print("Doesn't need Update") + exit(0)