From 1432019df21d200dc7aed804269ec76155409618 Mon Sep 17 00:00:00 2001 From: Leandro Lisboa Penz Date: Sat, 7 Oct 2023 13:24:21 +0100 Subject: [PATCH 1/2] Add a Dockerfile that allows efficient local builds The Dockerfile essentially builds a docker image by downloading moergo-sc/zmk and the current main branch of moergo-sc/glove80-zmk-config, and then building the latter to warm up the image's nix store. When running the image, very little gets downloaded, and the image just builds glove80.uf2 and copies it back to the current directory. The provided `build.sh` script both builds and runs the docker image, calling docker with the appropriate arguments. --- Dockerfile | 28 ++++++++++++++++++++++++++++ build.sh | 8 ++++++++ 2 files changed, 36 insertions(+) create mode 100644 Dockerfile create mode 100755 build.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..433aaad8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +FROM nixpkgs/nix:nixos-23.05 + +ENV PATH=/root/.nix-profile/bin:/usr/bin:/bin + +RUN set -e -x; \ + nix-env -iA cachix -f https://cachix.org/api/v1/install; \ + cachix use moergo-glove80-zmk-dev; \ + mkdir -p /glove80-zmk-config/config; \ + git clone -b main https://github.com/moergo-sc/zmk /glove80-zmk-config/src; \ + : Pre-build zmk to populate the nix store with the dependencies; \ + cd /glove80-zmk-config/src; \ + nix-shell --run true --attr zmk . + +RUN set -e -x; \ + (\ + echo '#!/bin/bash'; \ + echo 'DST=$PWD'; \ + echo 'set -e -x'; \ + echo 'cp -v config/* /glove80-zmk-config/config'; \ + echo 'cd /glove80-zmk-config'; \ + echo 'nix-build config -o combined'; \ + echo 'cp -v combined/glove80.uf2 "$DST/glove80.uf2"'; \ + echo 'chown "$UID:$GID" "$DST/glove80.uf2"'; \ + ) > entrypoint.sh; \ + chmod a+x entrypoint.sh +ENTRYPOINT ["/entrypoint.sh"] + +# Run build.sh to use this file diff --git a/build.sh b/build.sh new file mode 100755 index 00000000..c51c5a85 --- /dev/null +++ b/build.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +IMAGE=glove80 + +set -e -x + +docker build -t "$IMAGE" . +docker run --rm -v "$PWD:$PWD" -w "$PWD" -e UID="$(id -u)" -e GID="$(id -g)" "$IMAGE" From a94e0377d95af5e74d13bebdb42ef6beea975637 Mon Sep 17 00:00:00 2001 From: Chris Andreae Date: Thu, 4 Apr 2024 21:16:42 +0900 Subject: [PATCH 2/2] Update Dockerfile, support specifying branch/tag as argument --- .gitignore | 1 + Dockerfile | 62 +++++++++++++++++++++++++++++----------------- build.sh | 7 +++--- config/default.nix | 5 ++-- 4 files changed, 47 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index b25c15b8..972873c8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *~ +*.uf2 diff --git a/Dockerfile b/Dockerfile index 433aaad8..268b5b14 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,28 +1,44 @@ -FROM nixpkgs/nix:nixos-23.05 +FROM nixpkgs/nix:nixos-23.11 ENV PATH=/root/.nix-profile/bin:/usr/bin:/bin -RUN set -e -x; \ - nix-env -iA cachix -f https://cachix.org/api/v1/install; \ - cachix use moergo-glove80-zmk-dev; \ - mkdir -p /glove80-zmk-config/config; \ - git clone -b main https://github.com/moergo-sc/zmk /glove80-zmk-config/src; \ - : Pre-build zmk to populate the nix store with the dependencies; \ - cd /glove80-zmk-config/src; \ - nix-shell --run true --attr zmk . - -RUN set -e -x; \ - (\ - echo '#!/bin/bash'; \ - echo 'DST=$PWD'; \ - echo 'set -e -x'; \ - echo 'cp -v config/* /glove80-zmk-config/config'; \ - echo 'cd /glove80-zmk-config'; \ - echo 'nix-build config -o combined'; \ - echo 'cp -v combined/glove80.uf2 "$DST/glove80.uf2"'; \ - echo 'chown "$UID:$GID" "$DST/glove80.uf2"'; \ - ) > entrypoint.sh; \ - chmod a+x entrypoint.sh -ENTRYPOINT ["/entrypoint.sh"] +RUN <&2 + cd /src + git fetch origin + git checkout -q --detach "\$BRANCH" + + echo 'Building Glove80 firmware' >&2 + cd /config + nix-build ./config --arg firmware 'import /src/default.nix {}' -j2 -o /tmp/combined --show-trace + install -o "\$UID" -g "\$GID" /tmp/combined/glove80.uf2 ./glove80.uf2 +EOF + +ENTRYPOINT ["/bin/entrypoint.sh"] # Run build.sh to use this file diff --git a/build.sh b/build.sh index c51c5a85..73249c4a 100755 --- a/build.sh +++ b/build.sh @@ -1,8 +1,9 @@ #!/bin/bash -IMAGE=glove80 +set -euo pipefail -set -e -x +IMAGE=glove80-zmk-config-docker +BRANCH="${1:-main}" docker build -t "$IMAGE" . -docker run --rm -v "$PWD:$PWD" -w "$PWD" -e UID="$(id -u)" -e GID="$(id -g)" "$IMAGE" +docker run --rm -v "$PWD:/config" -e UID="$(id -u)" -e GID="$(id -g)" -e BRANCH="$BRANCH" "$IMAGE" diff --git a/config/default.nix b/config/default.nix index 0d9645c9..c781cb68 100644 --- a/config/default.nix +++ b/config/default.nix @@ -1,7 +1,8 @@ -{ pkgs ? import {} }: +{ pkgs ? import {} +, firmware ? import ../src {} +}: let - firmware = import ../src {}; config = ./.; glove80_left = firmware.zmk.override { board = "glove80_lh"; keymap = "${config}/glove80.keymap"; kconfig = "${config}/glove80.conf"; };