summary refs log tree commit diff stats
path: root/package.nix
blob: b5607a53887dd9ae9d91e1fd291ffc9ddb63a6d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
{
  stdenv,
  fetchgit,
  lib,
  writeText,
  # deps
  qmk,
  gnumake,
  git,
  fd,
}: let
  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";
    inherit version;

    src = fetchgit {
      url = "https://github.com/qmk/qmk_firmware";
      rev = "refs/tags/0.27.1";
      hash = "sha256-pVKZsfQypW26oWD9CLevCW7Z3fF4JbcY+Bo1tTLgBGk=";

      fetchSubmodules = true;

      # 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
      */
      ''
        runHook preBuild

        # 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="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
        mkdir ./.build
        make "zsa/moonlander:${layout_id}" | tee ./.build/make_build.log

        runHook postBuild
      '';
    installPhase = ''
      runHook preInstall

      set -e

      # mkdir --parents "$out/raw_output"
      # cp --recursive "." "$out/raw_output"

      mkdir --parents "$out/binary_output"
      cp --recursive "./.build" "$out/build"

      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/"

      runHook postInstall
    '';
  }