summary refs log tree commit diff stats
path: root/package.nix
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-12-29 23:57:32 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-12-29 23:57:32 +0100
commite0ca2d19231c50543a6dc6bcc6335a3f9a84c595 (patch)
treeaf4a581ebb6aacb456ea493a16a9934e01b5dcef /package.nix
parentfix(src): Add some default stuff (diff)
downloadqmk_layout-e0ca2d19231c50543a6dc6bcc6335a3f9a84c595.zip
feat(src): Make usable
Diffstat (limited to 'package.nix')
-rw-r--r--package.nix108
1 files changed, 89 insertions, 19 deletions
diff --git a/package.nix b/package.nix
index 8cde8bf..b5607a5 100644
--- a/package.nix
+++ b/package.nix
@@ -1,37 +1,97 @@
 {
   stdenv,
   fetchgit,
+  lib,
+  writeText,
   # deps
   qmk,
   gnumake,
   git,
+  fd,
 }: let
-  layout_id = "KWBYA";
-  layout_geometry = "moonlander";
-  firmware_version = "24";
+  layout_id = "soispha";
+
+  # The last two entries need to exist, so that the `qmk` cli accepts the repo as _the_
+  # `qmk_firmware` repository.
+  paths_to_keep = writeText "files_to_keep" ''
+    builddefs/
+    data/
+    drivers/
+    lib/
+    platforms/
+    quantum/
+    tmk_core/
+    version/
+    Makefile
+    paths.mk
+    qmk_version
+    requirements-dev.txt
+    requirements.txt
+  '';
+
+  # NOTE: Change this _EVERY_ time you actually use a new keymap. <2024-12-29>
+  version = "25";
 in
   stdenv.mkDerivation
   {
     pname = "moonlander-layout";
-    version = "25";
+    inherit version;
 
     src = fetchgit {
-      url = "https://github.com/qmk/qmk_firmware/";
+      url = "https://github.com/qmk/qmk_firmware";
       rev = "refs/tags/0.27.1";
-      hash = "sha256-Chq4R8rICY1eyt5r2dkPm34zDOm6TqPKAXQWPp0nByA=";
+      hash = "sha256-pVKZsfQypW26oWD9CLevCW7Z3fF4JbcY+Bo1tTLgBGk=";
 
       fetchSubmodules = true;
 
-      # This allows for a compiled in version
+      # This allows for a compiled-in version
       leaveDotGit = true;
+      deepClone = true;
+
+      postFetch = ''
+        set -e
+        cd "$out"
+
+        git_desc() {
+            ${lib.getExe git} describe --abbrev=0 --always --dirty --tags
+        }
+
+        mkdir --parents $out/version
+
+        # For the 'version.h' file
+        git_desc > $out/version/git_version
+        ${lib.getExe git} rev-parse HEAD > $out/version/git_qmk_hash
+        (cd lib/chibios && git_desc > $out/version/chibios_version)
+        (cd lib/chibios-contrib && git_desc > $out/version/chibios_contrib_version)
+
+        ${lib.getExe git} describe --abbrev=0 --tags > $out/version/qmk_version_make
+
+        ${lib.getExe fd} . --max-depth 1 --hidden | while read -r file; do
+            if ! grep "^$file" ${paths_to_keep}; then
+                rm --recursive "$file";
+            fi
+        done
+      '';
     };
 
     nativeBuildInputs = [
       gnumake
       qmk
       git
+      fd
     ];
 
+    patchPhase = ''
+      substituteInPlace ./lib/python/qmk/cli/generate/version_h.py \
+        --replace-fail 'git_version = "NA"'  "git_version = '$(cat ./version/git_version)'" \
+        --replace-fail 'git_qmk_hash = "NA"'  "git_qmk_hash = '$(cat ./version/git_qmk_hash)'" \
+        --replace-fail 'chibios_version = "NA"'  "chibios_version = '$(cat ./version/chibios_version)'" \
+        --replace-fail 'chibios_contrib_version = "NA"' "chibios_contrib_version = '$(cat ./version/chibios_contrib_version)'"
+
+      # substituteInPlace  ./lib/python/qmk/info.py \
+      #   --replace-fail "'keyboard_name': str(keyboard),"  ""
+    '';
+
     buildPhase =
       /*
       bash
@@ -39,34 +99,44 @@ in
       ''
         runHook preBuild
 
-        ## Set keyboard directory and make prefix based on firmware version
-        if [ "${firmware_version}" -ge 24 ]; then
-            keyboard_directory="keyboards/zsa"
-            keyboard_folder="zsa/${layout_geometry}"
-        else
-            keyboard_directory="keyboards"
-            keyboard_folder="${layout_geometry}"
-        fi
+        # Set the qmk_version without having to include the `.git` directory.
+        QMK_VERSION="$(cat ./version/git_version)-${version}"
+        SKIP_VERSION="true"
+        export QMK_VERSION
+        export SKIP_VERSION
 
         ## Copy layout files to the qmk folder
-        key_dir="$keyboard_directory/${layout_geometry}/keymaps/${layout_id}"
+        key_dir="keyboards/zsa/moonlander"
         [ -d "$key_dir" ] && rm --recursive "$key_dir"
 
         mkdir --parents "$(dirname "$key_dir")"
         cp --recursive "${./src}" "$key_dir"
 
+        # Needed by the `qmk` cli tool
+        mkdir --parents ./layouts/default
+
         ## Build the layout
-        make "$keyboard_folder:${layout_id}"
+        mkdir ./.build
+        make "zsa/moonlander:${layout_id}" | tee ./.build/make_build.log
 
         runHook postBuild
       '';
     installPhase = ''
       runHook preInstall
 
-      mkdir --parents "$out/binary_output"
+      set -e
 
+      # mkdir --parents "$out/raw_output"
+      # cp --recursive "." "$out/raw_output"
+
+      mkdir --parents "$out/binary_output"
       cp --recursive "./.build" "$out/build"
-      rm --recursive "$out/build/obj_zsa_moonlander_${layout_id}"
+
+      fd . $out/build --extension d --extension o --hidden --exec rm
+
+      while [ $(fd . $out/build --type empty --type directory --hidden | wc -l) -ne 0 ]; do
+        fd . $out/build --type empty --type directory --hidden --exec rm --dir
+      done
 
       ln --symbolic --relative "$out/build/zsa_moonlander_${layout_id}.bin" "$out/binary_output/"