Skip to content

Commit

Permalink
add flake
Browse files Browse the repository at this point in the history
  • Loading branch information
wochap committed Aug 27, 2024
1 parent ea4624e commit a0b27f2
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.build
.direnv
.venv
.west
modules
firmware
zmk
zephyr
91 changes: 91 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
default:
@just --list --unsorted

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

# parse combos.dtsi and adjust settings to not run out of slots
_parse_combos:
#!/usr/bin/env bash
set -euo pipefail
cconf="{{ config / 'combos.dtsi' }}"
if [[ -f $cconf ]]; then
# set MAX_COMBOS_PER_KEY to the most frequent combos count
count=$(
tail -n +10 $cconf |
grep -Eo '[LR][TMBH][0-9]' |
sort | uniq -c | sort -nr |
awk 'NR==1{print $1}'
)
sed -Ei "/CONFIG_ZMK_COMBO_MAX_COMBOS_PER_KEY/s/=.+/=$count/" "{{ config }}"/*.conf
echo "Setting MAX_COMBOS_PER_KEY to $count"
# set MAX_KEYS_PER_COMBO to the most frequent key count
count=$(
tail -n +10 $cconf |
grep -o -n '[LR][TMBH][0-9]' |
cut -d : -f 1 | uniq -c | sort -nr |
awk 'NR==1{print $1}'
)
sed -Ei "/CONFIG_ZMK_COMBO_MAX_KEYS_PER_COMBO/s/=.+/=$count/" "{{ config }}"/*.conf
echo "Setting MAX_KEYS_PER_COMBO to $count"
fi
# 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: _parse_combos
#!/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

# 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

7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ Git commit and push, GH Actions will build the firmware
```

If your keymap is correct, you will see a progress in building until it fails; otherwise, it will simply fail outright

#### Build locally with nix

```
$ cd ./path_to_this_repository
$ just build all
```
190 changes: 190 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";

# Version of requirements.txt installed in pythonEnv
zephyr.url = "github:zephyrproject-rtos/zephyr/v3.5.0";
zephyr.flake = false;

# Zephyr sdk and toolchain
zephyr-nix.url = "github:urob/zephyr-nix";
zephyr-nix.inputs.zephyr.follows = "zephyr";

flake-utils.url = "github:numtide/flake-utils";
};

outputs = { nixpkgs, zephyr-nix, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
zephyr = zephyr-nix.packages.${system};
in {
devShells.default = pkgs.mkShell {
packages = [
zephyr.pythonEnv
(zephyr.sdk.override { targets = [ "arm-zephyr-eabi" ]; })

pkgs.cmake
pkgs.dtc
pkgs.ninja
pkgs.qemu # needed for native_posix target
];
};
});
}

0 comments on commit a0b27f2

Please sign in to comment.