From 15d0a3125d6f9588a1f5738e2da8b7027280cf87 Mon Sep 17 00:00:00 2001 From: Bryton Hall Date: Sun, 15 Jan 2023 13:40:16 -0500 Subject: [PATCH] add formatter and pre-commit hook --- CONTRIBUTING.md | 12 ++++-- flake.lock | 107 ++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 58 ++++++++++++++++++++------ treefmt.toml | 25 +++++++++++ 4 files changed, 186 insertions(+), 16 deletions(-) create mode 100644 treefmt.toml diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 19a9782..819fdde 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,10 +15,10 @@ nix develop # or use direnv or do the setup yourself, whatever Build the project with ```sh -make -j +nix build # or `make -j` ``` -The compiled firmware will be at `./out/firmware.bin`. +The compiled firmware will be at `./result/*.bin`. ## language @@ -40,13 +40,17 @@ make -j LANGUAGE=fr Flash both earbuds: - for i in 0 1; do bestool write-image --port /dev/ttyACM$i result/*.bin; done +```sh +nix run '.#flash' [./path/to/firmware.bin] +``` ## logs View logs over the serial port with - minicom -D /dev/ttyACM0 -b 2000000 +```sh +nix run '.#logs' +``` ## release diff --git a/flake.lock b/flake.lock index 485cb70..17dcd7e 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,57 @@ { "nodes": { + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1668681692, + "narHash": "sha256-Ht91NGdewz8IQLtWZ9LCeNXMSXHUss+9COoqu6JLmXU=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "009399224d5e398d03b22badca40a37ac85412a1", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "locked": { + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "pre-commit", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1660459072, + "narHash": "sha256-8DFJjXG8zqoONA1vXtgeKXy68KdJL5UaXR8NtVMUbx8=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "a20de23b925fd8264fd7fad6454652e142fd7f73", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1673163619, @@ -16,9 +68,64 @@ "type": "github" } }, + "nixpkgs-stable": { + "locked": { + "lastModified": 1671271954, + "narHash": "sha256-cSvu+bnvN08sOlTBWbBrKaBHQZq8mvk8bgpt0ZJ2Snc=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "d513b448cc2a6da2c8803e3c197c9fc7e67b19e3", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-22.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1671271357, + "narHash": "sha256-xRJdLbWK4v2SewmSStYrcLa0YGJpleufl44A19XSW8k=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "40f79f003b6377bd2f4ed4027dde1f8f922995dd", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "pre-commit": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "gitignore": "gitignore", + "nixpkgs": "nixpkgs_2", + "nixpkgs-stable": "nixpkgs-stable" + }, + "locked": { + "lastModified": 1673627351, + "narHash": "sha256-oppRxEg/7ICcG67ErBvu1UlXt3su6zMcNoQmKaHPs5I=", + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "496e4505c2ddf5f205242eae8064d7d89cd976c0", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, "root": { "inputs": { "nixpkgs": "nixpkgs", + "pre-commit": "pre-commit", "utils": "utils" } }, diff --git a/flake.nix b/flake.nix index 4c373a5..735ebb4 100644 --- a/flake.nix +++ b/flake.nix @@ -2,6 +2,7 @@ inputs = { utils.url = "github:numtide/flake-utils"; nixpkgs.url = "github:nixos/nixpkgs/nixos-22.11"; + pre-commit.url = "github:cachix/pre-commit-hooks.nix"; }; outputs = inputs@{ self, ... }: @@ -14,18 +15,33 @@ (system: let pkgs = inputs.nixpkgs.legacyPackages.${system}; in { + formatter = pkgs.treefmt; apps = { flash = inputs.utils.lib.mkApp { drv = pkgs.writeShellScriptBin "tts" '' # should correctly identify the pinebuds - id=/dev/serial/by-id/usb-wch.cn_USB_Dual_Serial_0123456789-if - for i in 00 02; do + path=/dev/serial/by-id/usb-wch.cn_USB_Dual_Serial_0123456789-if + for id in 02 00; do # use the given file or a default [ $# -eq 1 ] && bin=$1 || bin=result/little-buddy-*-''${LANGUAGE:-en}.bin - ${self.packages.${system}.bestool}/bin/bestool write-image --port $id$i $bin + ${self.packages.${system}.bestool}/bin/bestool write-image --port $path$id $bin done ''; }; + logs = inputs.utils.lib.mkApp { + drv = pkgs.writeShellScriptBin "tts" '' + if [ $1 == "left" ]; then + id=02 + elif [ $1 == "right" ]; then + id=00 + else + echo error: must pass either \"left\" or \"right\" as an argument + exit 1 + fi + path=/dev/serial/by-id/usb-wch.cn_USB_Dual_Serial_0123456789-if + ${pkgs.minicom}/bin/minicom -D $path$id -b 2000000 + ''; + }; tts = inputs.utils.lib.mkApp { drv = pkgs.writeShellScriptBin "tts" '' LANGUAGE=$1 @@ -60,22 +76,14 @@ udev ]; }; - default = pkgs.stdenv.mkDerivation rec { name = "little-buddy"; src = ./.; nativeBuildInputs = with pkgs; [ # https://github.com/NixOS/nixpkgs/issues/51907 gcc-arm-embedded-9 - bc hostname - - # serial - minicom - self.packages.${system}.bestool - - # audio ffmpeg xxd ]; @@ -91,5 +99,31 @@ ''; }; }; - }); + checks = { + pre-commit = inputs.pre-commit.lib."${system}".run { + src = ./.; + hooks = { + treefmt = { + name = "treefmt"; + enable = true; + types = [ "file" ]; + pass_filenames = true; + entry = "${pkgs.treefmt}/bin/treefmt"; + }; + }; + }; + }; + devShell = pkgs.mkShell { + inherit (self.checks.${system}.pre-commit) shellHook; + inputsFrom = [ self.packages.${system}.default ]; + buildInputs = with pkgs; [ + # formatters + treefmt + nixpkgs-fmt + nodePackages.prettier + clang + ]; + }; + } + ); } diff --git a/treefmt.toml b/treefmt.toml new file mode 100644 index 0000000..8e2a141 --- /dev/null +++ b/treefmt.toml @@ -0,0 +1,25 @@ +[formatter.nix] +command = "nixpkgs-fmt" +includes = ["*.nix"] + +[formatter.prettier] +command = "prettier" +options = ["--write"] +includes = [ + "*.css", + "*.html", + "*.js", + "*.json", + "*.jsx", + "*.md", + "*.mdx", + "*.scss", + "*.ts", + "*.yaml", +] + +# TODO: breaks compilation +# [formatter.clang] +# command = "clang-format" +# options = [ "-i" ] +# includes = [ "*.c", "*.cpp", "*.cc", "*.h", "*.hpp", "*.S" ]