From 548c105faec4743e815549c4028e79a7daf0508f Mon Sep 17 00:00:00 2001
From: prescientmoon <git@moonythm.dev>
Date: Sat, 5 Oct 2024 03:22:19 +0200
Subject: [PATCH] Fix nix build

Also implement an "export chart info to csv" feature
---
 flake.nix                    | 28 ++++++------
 nix/shimmeringmoon.nix       | 86 ++++++++++++++++++++----------------
 scripts/export-chart-info.sh | 21 +++++++++
 3 files changed, 85 insertions(+), 50 deletions(-)
 create mode 100755 scripts/export-chart-info.sh

diff --git a/flake.nix b/flake.nix
index d07de71..e00cc2c 100644
--- a/flake.nix
+++ b/flake.nix
@@ -13,7 +13,8 @@
     inputs.flake-utils.lib.eachSystem (with inputs.flake-utils.lib.system; [ x86_64-linux ]) (
       system:
       let
-        pkgs = inputs.nixpkgs.legacyPackages.${system};
+        # pkgs = inputs.nixpkgs.legacyPackages.${system};
+        pkgs = inputs.nixpkgs.legacyPackages.${system}.extend inputs.fenix.overlays.default;
         # pkgs = inputs.nixpkgs.legacyPackages.${system}.extend (import inputs.rust-overlay);
         # pkgs = import inputs.nixpkgs {
         #   inherit system;
@@ -21,12 +22,14 @@
         # };
         # toolchain = pkgs.rust-bin.selectLatestNightlyWith (toolchain: toolchain.default);
         # toolchain = pkgs.rust-bin.stable.latest.default;
-        toolchain = inputs.fenix.packages.${system}.complete.toolchain;
+        rust-toolchain = pkgs.fenix.complete.toolchain;
         spkgs = inputs.self.packages.${system};
         inherit (pkgs) lib;
       in
       {
         packages = {
+          inherit rust-toolchain;
+
           kazesawa = pkgs.callPackage ./nix/kazesawa.nix { };
           exo = pkgs.callPackage ./nix/exo.nix { };
           geosans-light = pkgs.callPackage ./nix/geosans-light.nix { };
@@ -38,24 +41,23 @@
 
           default = spkgs.shimmeringmoon;
           shimmeringmoon = pkgs.callPackage ./nix/shimmeringmoon.nix {
-            # Pass the directory of fonts
-            inherit (spkgs) shimmering-fonts;
+            inherit (spkgs) shimmering-fonts rust-toolchain;
           };
         };
 
         #  {{{ Devshell
         devShell = pkgs.mkShell rec {
           nativeBuildInputs = with pkgs; [
-            # cargo
-            # rustc
-            # clippy
-            # rust-analyzer
-            # rustfmt
-            toolchain
+            # pkgs.cargo
+            # pkgs.rustc
+            # pkgs.clippy
+            # pkgs.rust-analyzer
+            # pkgs.rustfmt
+            spkgs.rust-toolchain
 
-            ruff
-            imagemagick
-            pkg-config
+            pkgs.ruff
+            pkgs.imagemagick
+            pkgs.pkg-config
           ];
 
           buildInputs = with pkgs; [
diff --git a/nix/shimmeringmoon.nix b/nix/shimmeringmoon.nix
index 16ac519..bdcc546 100644
--- a/nix/shimmeringmoon.nix
+++ b/nix/shimmeringmoon.nix
@@ -1,5 +1,4 @@
 {
-  rustPlatform,
   lib,
 
   pkg-config,
@@ -12,44 +11,57 @@
   openssl,
   sqlite,
   shimmering-fonts,
+  makeRustPlatform,
+
+  rust-toolchain,
 }:
-rustPlatform.buildRustPackage {
-  pname = "shimmeringmoon";
-  version = "unstable-2024-09-06";
+(makeRustPlatform {
+  cargo = rust-toolchain;
+  rustc = rust-toolchain;
+}).buildRustPackage
+  rec {
+    pname = "shimmeringmoon";
+    version = "unstable-2024-09-06";
+    src = lib.cleanSource ../.;
 
-  nativeBuildInputs = [
-    pkg-config
-    makeWrapper
-  ];
+    nativeBuildInputs = [
+      pkg-config
+      makeWrapper
+    ];
 
-  buildInputs = [
-    freetype
-    fontconfig
-    leptonica
-    tesseract
-    openssl
-    sqlite
-    shimmering-fonts
-  ];
+    # TODO: is this supposed to be here???
+    # LD_LIBRARY_PATH = lib.makeLibraryPath buildInputs;
+    buildInputs = [
+      freetype
+      fontconfig
+      leptonica
+      tesseract
+      openssl
+      sqlite
+    ];
 
-  # Tell the binary where to find the fonts
-  # postBuild = ''
-  #   wrapProgram $out/bin/shimmering-discord-bot \
-  #     --set SHIMMERING_FONTS_DIR ${shimmering-fonts}
-  # '';
-
-  checkFlags = [
-    # disable all tests
-    "--skip"
-  ];
-
-  src = lib.cleanSource ../.;
-
-  cargoLock = {
-    lockFile = ../Cargo.lock;
-    outputHashes = {
-      "hypertesseract-0.1.0" = "sha256-G0dos5yvvcfBKznAo1IIzLgXqRDxmyZwB93QQ6hVZSo=";
-      "plotters-0.4.0" = "sha256-9wtd7lig1vQ2RJVaEHdicfPZy2AyuoNav8shPMZ1EuE=";
+    cargoLock = {
+      lockFile = ../Cargo.lock;
+      outputHashes = {
+        "hypertesseract-0.1.0" = "sha256-G0dos5yvvcfBKznAo1IIzLgXqRDxmyZwB93QQ6hVZSo=";
+        "plotters-0.4.0" = "sha256-9wtd7lig1vQ2RJVaEHdicfPZy2AyuoNav8shPMZ1EuE=";
+        "faer-0.19.4" = "sha256-VXMk2S3caMMs0N0PJa/m/7aPykYgeXVVn7GWPnG63nQ=";
+      };
     };
-  };
-}
+
+    # Disable all tests
+    doCheck = false;
+
+    # Tell the binary where to find the fonts
+    postInstall = ''
+      wrapProgram $out/bin/shimmering-discord-bot \
+        --set SHIMMERING_FONTS_DIR ${shimmering-fonts}
+    '';
+
+    meta = {
+      description = "Arcaea score management toolchain";
+      homepage = "https://github.com/prescientmoon/shimmeringmoon";
+      mainProgram = "shimmering-cli";
+      platforms = [ "x86_64-linux" ];
+    };
+  }
diff --git a/scripts/export-chart-info.sh b/scripts/export-chart-info.sh
new file mode 100755
index 0000000..b6d4be5
--- /dev/null
+++ b/scripts/export-chart-info.sh
@@ -0,0 +1,21 @@
+#!/usr/bin/env bash
+
+if [ "$#" != 2 ]; then
+    echo "Usage: $0 <from> <to>"
+    exit 1
+fi
+
+from=$1
+to=$2
+
+echo "Creating destination..."
+rm -rf "$to"
+mkdir -p "$to"
+
+echo "Exporting info..."
+sqlite3 "$from" ".header on" ".mode csv" "select * from songs" \
+  >  $to/songs.csv
+sqlite3 "$from" ".header on" ".mode csv" "select * from charts" \
+  > $to/charts.csv
+
+echo "All done :3"