Skip to content

Commit

Permalink
Nixify ZMK config
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcharly committed Nov 15, 2024
1 parent 1913601 commit 30f241b
Show file tree
Hide file tree
Showing 9 changed files with 536 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Dim direnv status messages
export DIRENV_LOG_FORMAT=$'\033[2mdirenv: %s\033[0m'

# Use flake
use flake

# Watch additional files
watch_file draw/default.nix
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Highlight keymap files as C++, which is better than nothing.
*.dtsi text linguist-language=C++
*.keymap text linguist-language=C++
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.build/
.direnv/
.venv/
.west/
modules/
firmware/
zephyr/
zmk/
# Ignore keymap-drawer output for now
draw/skeletyl.svg
draw/skeletyl.yaml
79 changes: 79 additions & 0 deletions .justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
default:
@just --list --unsorted

config := absolute_path('config')
build := absolute_path('.build')
out := absolute_path('firmware')
draw := absolute_path('draw')

# parse build.yaml and filter targets by expression
_parse_targets $expr:
#!/usr/bin/env bash
attrs="[.board, .shield]"
filter="(($attrs | map(. // [.]) | combinations), ((.include // {})[] | $attrs)) | join(\",\")"
echo "$(yq -r "$filter" build.yaml | grep -v "^," | grep -i "${expr/#all/.*}")"
# build firmware for single board & shield combination
_build_single $board $shield *west_args:
#!/usr/bin/env bash
set -euo pipefail
artifact="${shield:+${shield// /+}-}${board}"
build_dir="{{ build / '$artifact' }}"
echo "Building firmware for $artifact..."
west build -s zmk/app -d "$build_dir" -b $board {{ west_args }} -- \
-DZMK_CONFIG="{{ config }}" ${shield:+-DSHIELD="$shield"}

if [[ -f "$build_dir/zephyr/zmk.uf2" ]]; then
mkdir -p "{{ out }}" && cp "$build_dir/zephyr/zmk.uf2" "{{ out }}/$artifact.uf2"
else
mkdir -p "{{ out }}" && cp "$build_dir/zephyr/zmk.bin" "{{ out }}/$artifact.bin"
fi

# build firmware for matching targets
build expr *west_args:
#!/usr/bin/env bash
set -euo pipefail
targets=$(just _parse_targets {{ expr }})
[[ -z $targets ]] && echo "No matching targets found. Aborting..." >&2 && exit 1
echo "$targets" | while IFS=, read -r board shield; do
just _build_single "$board" "$shield" {{ west_args }}
done

# clear build cache and artifacts
clean:
rm -rf {{ build }} {{ out }}

# clear all automatically generated files
clean-all: clean
rm -rf .west zmk

# clear nix cache
clean-nix:
nix-collect-garbage --delete-old

# parse & plot keymap
draw:
#!/usr/bin/env bash
set -euo pipefail
keymap -c "{{ draw }}/config.yaml" parse -z "{{ config }}/skeletyl.keymap" >"{{ draw }}/skeletyl.yaml"
keymap -c "{{ draw }}/config.yaml" draw "{{ draw }}/skeletyl.yaml" -k "bastardkb/skeletyl" >"{{ draw }}/skeletyl.svg"
# initialize west
init:
west init -l config
west update
west zephyr-export

# list build targets
list:
@just _parse_targets all | sed 's/,$//' | sort | column

# update west
update:
west update

# upgrade zephyr-sdk and python dependencies
upgrade-sdk:
nix flake update --flake .
2 changes: 2 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
proseWrap: "always"
editorconfig: true
8 changes: 8 additions & 0 deletions draw/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parse_config:
zmk_preamble: "#include <zmk-helpers/key-labels/34.h>"
zmk_additional_includes:
- "modules/helpers/include"

draw_config:
footer_text: "0xcharly/zmk-config"
dark_mode: auto
42 changes: 42 additions & 0 deletions draw/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
lib,
buildPythonApplication,
fetchPypi,
poetry-core,
pydantic,
pcpp,
pyparsing,
pyyaml,
platformdirs,
pydantic-settings,
}:
buildPythonApplication rec {
pname = "keymap-drawer";
version = "0.18.1";
pyproject = true;

src = fetchPypi {
pname = "keymap_drawer";
inherit version;
hash = "sha256-MHjxsopXoYWZFuXUbeaI7BCSx3HkRaeVidY+mc8lj+s=";
};

build-system = [poetry-core];

propagatedBuildInputs = [
pydantic
pcpp
pyparsing
pyyaml
platformdirs
pydantic-settings
];

doCheck = false;

meta = {
homepage = "https://github.com/caksoylar/keymap-drawer";
description = "Parse QMK & ZMK keymaps and draw them as vector graphics";
license = lib.licenses.mit;
};
}
Loading

0 comments on commit 30f241b

Please sign in to comment.