Skip to content

Commit

Permalink
refactor: split up packages
Browse files Browse the repository at this point in the history
  • Loading branch information
MattSturgeon committed Mar 29, 2024
1 parent a1e6f1a commit a8e4e71
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 175 deletions.
69 changes: 0 additions & 69 deletions drawer/default.nix

This file was deleted.

110 changes: 4 additions & 106 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@
};

outputs = {
self,
nixpkgs,
glove80-zmk,
keymap-drawer,
poetry2nix,
flake-parts,
...
} @ inputs:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = nixpkgs.lib.systems.flakeExposed;

imports = [
./drawer
./packages
];

perSystem = {
Expand All @@ -40,113 +37,14 @@
system,
...
}: {
# `nix build`
packages.default = let
firmware = import glove80-zmk {inherit pkgs;};

keymap = ./config/glove80.keymap;
kconfig = ./config/glove80.conf;

glove80_left = firmware.zmk.override {
inherit keymap kconfig;
board = "glove80_lh";
};

glove80_right = firmware.zmk.override {
inherit keymap kconfig;
board = "glove80_rh";
};
in
firmware.combine_uf2 glove80_left glove80_right;

# `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
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!"
'';
};
# `nix build`
packages.default = config.packages.firmware;

formatter = pkgs.alejandra;
};
Expand Down
8 changes: 8 additions & 0 deletions packages/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
imports = [
./flash.nix
./firmware.nix
./draw.nix
./keymap-drawer.nix
];
}
52 changes: 52 additions & 0 deletions packages/draw.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
perSystem = {
config,
pkgs,
system,
...
}: {
# Draw SVG images of the keymap
packages.draw = pkgs.writeShellApplication {
name = "draw";
runtimeInputs = [
pkgs.yq-go
config.packages.keymap-drawer
];
text = ''
set +e
out=./img
keymap_dir=./config
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 "- Removing old images"
rm "$out"/"$name".yaml
rm "$out"/"$name".svg
rm "$out"/"$name"_*.svg
echo "- Parsing keymap-drawer"
cmd parse --zmk-keymap "$file" > "$config"
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
done
'';
};
};
}
26 changes: 26 additions & 0 deletions packages/firmware.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{inputs, ...}: {
perSystem = {
config,
pkgs,
system,
...
}: {
packages.firmware = let
firmware = import inputs.glove80-zmk {inherit pkgs;};

keymap = ../config/glove80.keymap;
kconfig = ../config/glove80.conf;

left = firmware.zmk.override {
inherit keymap kconfig;
board = "glove80_lh";
};

right = firmware.zmk.override {
inherit keymap kconfig;
board = "glove80_rh";
};
in
firmware.combine_uf2 left right;
};
}
91 changes: 91 additions & 0 deletions packages/flash.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
perSystem = {
config,
pkgs,
system,
...
}: {
# 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
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.firmware}" "$kb" \
&& echo "Done!" || echo "Error: Unable to flash firmware!"
'';
};
};
}
Loading

0 comments on commit a8e4e71

Please sign in to comment.