aboutsummaryrefslogtreecommitdiff
path: root/modules/qmk.nix
blob: 6288d7e85a557ef55fe8ee86b91d9bfc1b1dede2 (plain)
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
{
lib,
pkgs,
config,
...
}:
let
  inherit (lib) mkOption mkIf types;
in {
    options = {
      type = mkOption {
        type = types.enum [ "qmk" ];
      };


      qmk = {
        qmk_firmware = mkOption {
          type = types.path;
          default = pkgs.qmk_firmware;
          description = ''
          qmk firmware path
          '';
        };
        keyboard = {
          name = mkOption {
            type = types.str;
            description = "keyboard path";
            example = "4pplet/steezy60";
          };

          revision = mkOption {
            type = types.nullOr types.str;
            default = null;
            description = "keyboard revision";
            example = "rev_a";
          };

          _path = mkOption {
          type = types.str;
          internal = true;
          default = lib.concatStringsSep "/" ([config.qmk.keyboard.name] ++ lib.optional (config.qmk.keyboard.revision != null) config.qmk.keyboard.revision);
        };
        };

        keymap = {
          name = mkOption {
            type = types.str;
            default = "default";
            description = "keymap name";
          };

          src = mkOption {
            type = types.nullOr types.path;
            default = null;
            description = "qmk keymap files";
          };

        _path = mkOption {
          type = types.str;
          internal = true;
          default = "keyboards/${config.qmk.keyboard.name}/keymaps/${config.qmk.keymap.name}";
        };
        };

      build = {
        bin = mkOption {
          type = types.path;
          internal = true;
          readOnly = true;
          default = let
            ksrc = config.qmk.keymap.src;
          in pkgs.stdenv.mkDerivation {
              name = "qmk:${config.qmk.keyboard._path}:${config.qmk.keymap.name}";
              src = config.qmk.qmk_firmware;
              #   {
              #     name = "qmk_firmware";
              #     src = config.qmk.qmk_firmware;
              #   }
              # ] ++ lib.optional (ksrc != null) {
              #     name = "keymap";
              #     src = ksrc;
              #   };
              #
              # sourceRoot = "qmk_firmware";

              nativeBuildInputs = [
                pkgs.qmk
              ];

              dontFixup = true;

              preConfigure = lib.optionalString (ksrc != null) ''
                mkdir -pv "${config.qmk.keymap._path}"
                cp -v "${ksrc}"/* "${config.qmk.keymap._path}"
              '';

              buildPhase = ''
                # SKIP_GIT=true BUILD_DIR=build \
                #   make "${config.qmk.keyboard._path}:${config.qmk.keymap.name}"
                  qmk compile \
                    --env SKIP_GIT=true \
                    --env BUILD_DIR=build \
                    --keyboard "${config.qmk.keyboard._path}" \
                    --keymap "${config.qmk.keymap.name}"
              '';

              installPhase = ''
                mkdir -p $out
                cp -v build/*.{bin,hex,elf,dfu,uf2,eep} $out
              '';


            };
        };
        flash = mkOption {
          type = types.functionTo (types.either types.package types.str);
        };

      };
      };
    };

  config = mkIf (config.type == "qmk") {
    build.flash = let
      p = lib.replaceStrings [ "/" ":" ] [ "_" "_" ] "${config.qmk.keyboard._path}:${config.qmk.keymap.name}";
      n = config.qmk.build.flash "${builtins.toString config.qmk.build.bin}/${p}";
    in if builtins.isString n then pkgs.writeShellApplication { name = "flash"; text = n; } else n;
  };
}