forked from zmkfirmware/zmk
-
Notifications
You must be signed in to change notification settings - Fork 62
/
release.nix
208 lines (171 loc) · 5.35 KB
/
release.nix
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
{ pkgs ? (import ./nix/pinned-nixpkgs.nix {}), revision ? "HEAD" }:
let
lib = pkgs.lib;
zmkPkgs = (import ./default.nix { inherit pkgs; });
lambda = (import ./lambda { inherit pkgs; });
ccacheWrapper = pkgs.callPackage ./nix/ccache.nix {};
nix-utils = pkgs.fetchFromGitHub {
owner = "iknow";
repo = "nix-utils";
rev = "c13c7a23836c8705452f051d19fc4dff05533b53";
sha256 = "0ax7hld5jf132ksdasp80z34dlv75ir0ringzjs15mimrkw8zcac";
};
ociTools = pkgs.callPackage "${nix-utils}/oci" {};
inherit (zmkPkgs) zmk zephyr;
accounts = {
users.deploy = {
uid = 999;
group = "deploy";
home = "/home/deploy";
shell = "/bin/sh";
};
groups.deploy.gid = 999;
};
baseLayer = {
name = "base-layer";
path = [ pkgs.busybox ];
entries = ociTools.makeFilesystem {
inherit accounts;
tmp = true;
usrBinEnv = "${pkgs.busybox}/bin/env";
binSh = "${pkgs.busybox}/bin/sh";
};
};
depsLayer = {
name = "deps-layer";
path = [ pkgs.ccache ];
includes = zmk.buildInputs ++ zmk.nativeBuildInputs ++ zmk.zephyrModuleDeps;
};
dts2yml = pkgs.writeShellScriptBin "dts2yml" ''
set -eo pipefail
${pkgs.gcc-arm-embedded}/bin/arm-none-eabi-cpp -P -D__DTS__ -E -nostdinc \
-I "${zmk.src}/app/dts" -I "${zmk.src}/app/include" \
-I "${zephyr}/zephyr/dts" -I "${zephyr}/zephyr/dts/common" -I "${zephyr}/zephyr/dts/arm" \
-I "${zephyr}/zephyr/include" -I "${zephyr}/zephyr/include/zephyr"\
-undef -x assembler-with-cpp - |\
${pkgs.dtc}/bin/dtc -I dts -O yaml
'';
zmkCompileScript = let
zmk' = zmk.override {
gcc-arm-embedded = ccacheWrapper.override {
unwrappedCC = pkgs.gcc-arm-embedded;
};
};
zmk_glove80_rh = zmk.override { board = "glove80_rh"; };
realpath_coreutils = if pkgs.stdenv.isDarwin then pkgs.coreutils else pkgs.busybox;
in pkgs.writeShellScriptBin "compileZmk" ''
set -eo pipefail
function usage() {
echo "Usage: compileZmk [-m] [-k keymap_file] [-c kconfig_file] [-b board]"
}
function checkPath() {
if [ -z "$1" ]; then
return 0
elif [ ! -f "$1" ]; then
echo "Error: Missing $2 file" >&2
usage >&2
exit 1
fi
${realpath_coreutils}/bin/realpath "$1"
}
keymap="${zmk.src}/app/boards/arm/glove80/glove80.keymap"
kconfig=""
board="glove80_lh"
merge_rhs=""
while getopts "hk:c:d:b:m" opt; do
case "$opt" in
h|\?)
usage >&2
exit 1
;;
k)
keymap="$OPTARG"
;;
c)
kconfig="$OPTARG"
;;
b)
board="$OPTARG"
;;
m)
merge_rhs=t
;;
esac
done
if [ "$board" = "glove80_rh" -a -n "$merge_rhs" ]; then
echo "Cannot merge static RHS with built RHS" >&2
exit 2
fi
keymap="$(checkPath "$keymap" keymap)"
kconfig="$(checkPath "$kconfig" Kconfig)"
export PATH=${lib.makeBinPath (with pkgs; zmk'.nativeBuildInputs ++ [ ccache ])}:$PATH
export CMAKE_PREFIX_PATH=${zephyr}
export CCACHE_BASEDIR=$PWD
export CCACHE_NOHASHDIR=t
export CCACHE_COMPILERCHECK=none
if [ -n "$DEBUG" ]; then ccache -z; fi
cmake -G Ninja -S ${zmk'.src}/app ${lib.escapeShellArgs zmk'.cmakeFlags} "-DUSER_CACHE_DIR=/tmp/.cache" "-DKEYMAP_FILE=$keymap" "-DBOARD=$board" "-DEXTRA_CONF_FILE=$kconfig" "-DBUILD_VERSION=${revision}"
ninja
if [ -n "$DEBUG" ]; then ccache -s; fi
if [ -n "$merge_rhs" ]; then
cat zephyr/zmk.uf2 ${zmk_glove80_rh}/zmk.uf2 > zmk.uf2
else
mv zephyr/zmk.uf2 zmk.uf2
fi
'';
ccacheCache = pkgs.runCommandNoCC "ccache-cache" {
nativeBuildInputs = [ zmkCompileScript ];
} ''
export CCACHE_DIR=$out
mkdir /tmp/build
cd /tmp/build
compileZmk -b glove80_lh -k ${zmk.src}/app/boards/arm/glove80/glove80.keymap
rm -fr /tmp/build
mkdir /tmp/build
cd /tmp/build
compileZmk -b glove80_rh -k ${zmk.src}/app/boards/arm/glove80/glove80.keymap
'';
entrypoint = pkgs.writeShellScriptBin "entrypoint" ''
set -euo pipefail
if [ ! -d "$CCACHE_DIR" ]; then
cp -r ${ccacheCache} "$CCACHE_DIR"
chmod -R u=rwX,go=u-w "$CCACHE_DIR"
fi
if [ ! -d /tmp/build ]; then
mkdir /tmp/build
fi
exec "$@"
'';
startLambda = pkgs.writeShellScriptBin "startLambda" ''
set -euo pipefail
export PATH=${lib.makeBinPath [ zmkCompileScript dts2yml ]}:$PATH
cd ${lambda.source}
${lambda.bundleEnv}/bin/bundle exec aws_lambda_ric "app.LambdaFunction::Handler.process"
'';
simulateLambda = pkgs.writeShellScriptBin "simulateLambda" ''
${pkgs.aws-lambda-rie}/bin/aws-lambda-rie ${startLambda}/bin/startLambda
'';
lambdaImage =
let
appLayer = {
name = "app-layer";
path = [ startLambda zmkCompileScript ];
};
in
ociTools.makeSimpleImage {
name = "zmk-builder-lambda";
layers = [ baseLayer depsLayer appLayer ];
config = {
User = "deploy";
WorkingDir = "/tmp";
Entrypoint = [ "${entrypoint}/bin/entrypoint" ];
Cmd = [ "startLambda" ];
Env = [ "CCACHE_DIR=/tmp/ccache" "REVISION=${revision}" ];
};
};
in {
inherit lambdaImage zmkCompileScript dts2yml ccacheCache;
directLambdaImage = lambdaImage;
# nix shell -f release.nix simulateLambda -c simulateLambda
inherit simulateLambda;
}