Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix: Add macOS script to flash the device #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@
inherit (self.legacyPackages.${system}) buildSplitKeyboard;
};

flash = pkgs.callPackage ./nix/flash.nix {
inherit firmware;
};
flash =
if pkgs.lib.strings.hasSuffix "linux" system
then
pkgs.callPackage ./nix/flash-linux.nix {
inherit firmware;
}
else
pkgs.callPackage ./nix/flash-darwin.nix {
inherit firmware;
};

update = pkgs.callPackage ./nix/update.nix {};
});
Expand Down
84 changes: 84 additions & 0 deletions nix/flash-darwin.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{ lib
, writeShellApplication
, firmware
, script-name ? "zmk-uf2-flash"
}:

writeShellApplication {
name = script-name;

runtimeInputs = [
];

text = ''
available() {
if system_profiler SPUSBDataType 2>/dev/null | grep -Fsq 'nRF UF2'; then
for device in $(diskutil list | rg -oNI '/dev/[^ ]*'); do
if diskutil info "$device" | grep -Fsq 'nRF UF2'; then
diskutil info "$device" | grep -F 'Mount Point:' | cut -d':' -f2 | tr -d '[:space:]'
fi
done
fi
}

mounted() {
findmnt "$device" -no target
}

flash=("$@")
parts=(${toString firmware.parts or ""})

if [ "''${#flash[@]}" -eq 0 ]; then
if [ "''${#parts[@]}" -eq 0 ]; then
flash=("")
else
flash=("''${parts[@]}")
fi
else
for part in "''${flash[@]}"; do
if ! printf '%s\0' "''${parts[@]}" | grep -Fxqz -- "$part"; then
echo "The '$part' part does not exist in the firmware '"'${firmware.name}'"'"
exit 1
fi
done
fi

for part in "''${flash[@]}"; do
echo -n "Double tap reset and plug in$([ -n "$part" ] && echo " the '$part' part of") the keyboard via USB or press ^C to cancel."
mountpoint="$(available)"
while [[ -z "$mountpoint" ]]; do
echo -n .
sleep 1
mountpoint="$(available)"
done
echo

echo -n "Copying firmware to$([ -n "$part" ] && echo " the '$part' part of") the device..."
# On macOS, the ZMK firmware finishes its "flashing" process before the
# `cp` command finishes its execution. This is normal, and expected
# behavior. For us, though, means that this piece of the script will
# signalize a failure to Nix, since `cp` will exit with code != than `0`.
# The way we prevent a "command failure", is to indicate to nix that we're
# handling the error ourselves, which simply indicates that the operation
# finished successfully.
if ! cp ${firmware}/*"$([ -n "$part" ] && echo "_$part")".uf2 "$mountpoint/" 2>/dev/null; then
echo "Done"
else
echo "ERROR"
echo "There was a failure during firmware copy. Your keyboard might still be in 'bootload' mode!"
exit 1
fi

sleep 1

mountpoint=""
done
'';

meta = with lib; {
description = "ZMK UF2 firmware flasher";
license = licenses.mit;
platforms = platforms.darwin;
maintainers = with maintainers; [lilyinstarlight];
};
}
3 changes: 2 additions & 1 deletion nix/flash.nix → nix/flash-linux.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
, writeShellApplication
, util-linux
, firmware
, script-name ? "zmk-uf2-flash"
}:

writeShellApplication {
name = "zmk-uf2-flash";
name = script-name;

runtimeInputs = [
util-linux
Expand Down