Skip to content

Commit

Permalink
refactor: remove devshell & rewrite scripts
Browse files Browse the repository at this point in the history
- Use `pkgs.writeShellApplication` instead of devshell commands
- Rewrite `flash` script to be more robust
- Tweak `draw` script so unrelated SVGs aren't removed
  • Loading branch information
MattSturgeon committed Mar 29, 2024
1 parent b74ccad commit a1e6f1a
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 146 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/draw-keymaps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
uses: DeterminateSystems/magic-nix-cache-action@main

- name: Draw keymaps
run: nix run . -- draw
run: nix run .#draw

# TODO print to $GITHUB_STEP_SUMMARY
- name: Commit changes
Expand Down
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,13 @@ Before flashing, ensure `udisks2` is setup and enabled so that the keyboard can

#### Flashing automatically

The easiest way to flash the firmware is using the `flash` command in the `nix develop` shell.
The easiest way to flash the firmware is using `nix run`.

This can also be run without entering the dev shell by using `nix run . -- flash`.

- Enter shell using `nix develop`
- `cd` into the project root
- Connect the **right** half (in bootloader mode)
- Run `flash`
- Run `nix run`
- Connect the **left** half (in bootloader mode)
- Run `flash`
- Run `nix run`
- Done!

#### Flashing manually
Expand Down
107 changes: 55 additions & 52 deletions drawer/default.nix
Original file line number Diff line number Diff line change
@@ -1,66 +1,69 @@
{
inputs,
lib,
...
}: let
in {
perSystem = {
config,
pkgs,
system,
...
}: let
inherit (inputs.poetry2nix.lib.mkPoetry2Nix {inherit pkgs;}) mkPoetryApplication;

exe = lib.getExe config.packages.keymap-drawer;
yq = lib.getExe pkgs.yq-go;
in {
packages = {
keymap-drawer = mkPoetryApplication {
projectDir = inputs.keymap-drawer;
preferWheels = true;
meta = {
mainProgram = "keymap";
homepage = "https://github.com/caksoylar/keymap-drawer";
};
}: {
perSystem = {
config,
pkgs,
system,
...
}: let
inherit (inputs.poetry2nix.lib.mkPoetry2Nix {inherit pkgs;}) mkPoetryApplication;
in {
packages = {
keymap-drawer = mkPoetryApplication {
projectDir = inputs.keymap-drawer;
preferWheels = true;
meta = {
mainProgram = "keymap";
homepage = "https://github.com/caksoylar/keymap-drawer";
};
};
};

devshells.default.commands = [
{
name = "draw";
command = /*bash*/ ''
set +e

out="$PRJ_ROOT"/img
keymap_dir="$PRJ_ROOT"/config
cmd="${exe} --config $keymap_dir/keymap_drawer.yaml"
# Draw SVG images of the keymap
draw = pkgs.writeShellApplication {
name = "draw";
runtimeInputs = [
pkgs.yq-go
config.packages.keymap-drawer
];
text = ''
set +e
echo "Removing previous images"
rm "$out"/*.{yaml,svg}
out=./img
keymap_dir=./config
for file in "$keymap_dir"/*.keymap
do
name="$(basename --suffix=".keymap" "$file")"
config="$out/$name.yaml"
echo "Found $name keymap"
cmd() {
keymap --config "$keymap_dir"/keymap_drawer.yaml "$@"
}
for file in "$keymap_dir"/*.keymap
do
name="$(basename --suffix=".keymap" "$file")"
config="$out/$name.yaml"
echo "Found $name keymap"
echo "- Parsing keymap-drawer"
$cmd parse --zmk-keymap "$file" > "$config"
echo "- Removing old images"
rm "$out"/"$name".yaml
rm "$out"/"$name".svg
rm "$out"/"$name"_*.svg
echo "- Drawing all layers"
$cmd draw "$config" > "$out"/"$name".svg
echo "- Parsing keymap-drawer"
cmd parse --zmk-keymap "$file" > "$config"
layers=$(${yq} '.layers | keys | .[]' "$config")
for layer in $layers
do
echo "- Drawing $layer layer"
$cmd draw "$config" --select-layers "$layer" > "$out"/"$name"_"$layer".svg
done
echo "- Drawing all layers"
cmd draw "$config" > "$out"/"$name".svg
layers=$(yq '.layers | keys | .[]' "$config")
for layer in $layers
do
echo "- Drawing $layer layer"
cmd draw "$config" --select-layers "$layer" > "$out"/"$name"_"$layer".svg
done
'';
help = "Draw SVG images of the keymap";
}
];
done
'';
};
};
};
}
59 changes: 2 additions & 57 deletions flake.lock

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

119 changes: 89 additions & 30 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
url = "github:hercules-ci/flake-parts";
inputs.nixpkgs-lib.follows = "nixpkgs";
};
devshell = {
url = "github:numtide/devshell";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = {
Expand All @@ -30,12 +26,11 @@
keymap-drawer,
poetry2nix,
flake-parts,
devshell,
} @ inputs:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = nixpkgs.lib.systems.flakeExposed;

imports = [
inputs.devshell.flakeModule
./drawer
];

Expand All @@ -45,6 +40,7 @@
system,
...
}: {
# `nix build`
packages.default = let
firmware = import glove80-zmk {inherit pkgs;};

Expand All @@ -63,31 +59,94 @@
in
firmware.combine_uf2 glove80_left glove80_right;

# Expose devshells as a runnable app too
apps.default = config.devShells.default.flakeApp;

devshells.default.commands = [
{
name = "flash";
command = /*bash*/ ''
set +e
root="/run/media/$(whoami)"
dest_folder_name=$(ls $root | grep GLV80)
dest="$root"/"$dest_folder_name"
if [[ -n "$dest_folder_name" && -d "$dest" ]]; then
echo Flashing to "$dest"
cp ${config.packages.default}/glove80.uf2 "$dest"/CURRENT.UF2
exit 0
else
echo "Error: Glove80 keyboard is not plugged-in"
exit 1
# `nix run`
apps.default = {
type = "app";
program = config.packages.flash;
};

# Builds the firmware and copies it to the plugged-in keyboard half
packages.flash = pkgs.writeShellApplication {
name = "flash";
text = ''
set +e
# Disable -u because empty arrays are treated as "unbound"
set +u
# Enable nullglob so that non-matching globs have no output
shopt -s nullglob
# Indent piped input 4 spaces
indent() {
sed -e 's/^/ /'
}
# Platform specific disk candidates
declare -a disks
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Linux/GNU
# - /run/media/<user>/<disk>
disks=(/run/media/"$(whoami)"/GLV80*)
elif [[ "$OSTYPE" == "darwin"* ]]; then
# Mac OSX
# - /Volumes/<disk>
disks=(/Volumes/GLV80*)
elif [[ "$OSTYPE" == "msys" || "$OSTYPE" == "cygwin" ]]; then
# Cygwin or Msys2
# - /<drive letter>
disks=(/?)
elif (grep -sq Microsoft /proc/version); then
# WSL
# - /mnt/<drive letter>
disks=(/mnt/?)
else
echo "Error: Unable to detect platform!"
echo "OSTYPE=$OSTYPE"
echo "/proc/version"
indent < /proc/version
exit 1
fi
# Disks that have a matching INFO_UF2
declare -a matches
for disk in "''${disks[@]}"; do
if (grep -sq Glove80 "$disk"/INFO_UF2.TXT); then
matches+=("$disk")
fi
'';
help = "builds the firmware and copies it to the plugged-in keybaord half";
}
];
done
# Assert we found exactly one keyboard
count="''${#matches[@]}"
if [[ "$count" -lt 1 ]]; then
# No matches. Exit
echo "Error: No Glove80 connected!"
exit 1
elif [[ "$count" -gt 1 ]]; then
# Multiple matches. Print them and exit
echo "Error: $count Glove80s connected. Expected 1!"
for i in "''${!matches[@]}"; do
kb="''${matches[$i]}"
# Print the relevant lines from INFO_UF2
echo "$((i + 1)). $kb"
grep --no-filename --color=never Glove80 "$kb"/INFO_UF2.TXT | indent
done
exit 1
fi
# We have a winner!
kb="''${matches[0]}"
echo "Found keyboard:"
echo "$kb"
indent < "$kb"/INFO_UF2.TXT
echo
# Flash by copying the firmware package
echo "Flashing firmware..."
cp -r "${config.packages.default}" "$kb" \
&& echo "Done!" || echo "Error: Unable to flash firmware!"
'';
};

formatter = pkgs.alejandra;
};
Expand Down

0 comments on commit a1e6f1a

Please sign in to comment.