{
  description = "A blink template for STM32G4";

  inputs = {
    nixpkgs.url = "nixpkgs/nixpkgs-unstable";
    utils.url = "github:numtide/flake-utils";
    libopencm3-src = {
      url = "github:libopencm3/libopencm3";
      flake = false;
    };
    pre-commit-hooks = {
      url = "github:cachix/pre-commit-hooks.nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs =
    {
      self,
      nixpkgs,
      utils,
      libopencm3-src,
      pre-commit-hooks,
      ...
    }:
    {
      overlays.default = final: prev: {
        libopencm3-stm32 = final.stdenvNoCC.mkDerivation {
          pname = "libopencm3-stm32";
          version = "latest";

          src = libopencm3-src;

          postPatch = ''
            patchShebangs scripts
          '';

          nativeBuildInputs = [
            final.gcc-arm-embedded
            final.python3
            final.gnumake
          ];

          enableParallelBuilding = true;

          makeFlags = [
            "PREFIX=arm-none-eabi-"
          ];

          TARGETS = "stm32/f0 stm32/f1 stm32/f2 stm32/f3 stm32/f4 stm32/f7 stm32/l0 stm32/l1 stm32/l4 stm32/g0 stm32/g4 stm32/h7 stm32/u5";

          installPhase = ''
            runHook preInstall

            mkdir -p $out
            cp -r include lib mk scripts ld $out/

            patchShebangs $out/scripts

            runHook postInstall
          '';

          meta = with final.lib; {
            description = "Open source ARM Cortex-M microcontroller library (STM32 targets)";
            homepage = "http://libopencm3.org/";
            license = licenses.lgpl3Plus;
            platforms = platforms.all;
          };
        };
      };
    }
    // utils.lib.eachDefaultSystem (
      system:
      let
        pkgs = import nixpkgs {
          inherit system;
          overlays = [ self.overlays.default ];
        };
        pname = "blink";
        version = "0.1.0";
      in
      {
        packages = {
          inherit (pkgs) libopencm3-stm32;
          default = pkgs.stdenvNoCC.mkDerivation {
            inherit pname version;
            src = pkgs.lib.cleanSource ./.;

            nativeBuildInputs = with pkgs; [
              gcc-arm-embedded
              gnumake
              python3
            ];

            makeFlags = [
              "OPENCM3_DIR=${pkgs.libopencm3-stm32}"
              "PREFIX=arm-none-eabi-"
            ];

            installPhase = ''
              runHook preInstall
              mkdir -p $out/bin
              cp build/${pname}.elf $out/bin/
              cp build/${pname}.bin $out/bin/
              cp build/${pname}.hex $out/bin/
              runHook postInstall
            '';
          };
        };

        devShells.default =
          let
            # FIXME: 'gnu/stubs-32.h' file not found
            clangdWrapper = pkgs.writeShellScriptBin "clangd" ''
              exec ${pkgs.clang-tools}/bin/clangd \
                --query-driver=${pkgs.gcc-arm-embedded}/bin/arm-none-eabi-* \
                "$@"
            '';
          in
          pkgs.mkShell {
            name = "stm32-blink";
            packages = self.packages.${system}.default.nativeBuildInputs ++ [
              clangdWrapper

              pkgs.bear
              pkgs.openocd
              pkgs.stlink
            ];
            OPENCM3_DIR = pkgs.libopencm3-stm32;
            PREFIX = "arm-none-eabi-";
          };

        formatter =
          let
            inherit (self.checks.${system}.pre-commit-check.config) package configFile;
            script = ''
              ${pkgs.lib.getExe package} run --all-files --config ${configFile}
            '';
          in
          pkgs.writeShellScriptBin "pre-commit-run" script;

        checks = {
          build-packages = pkgs.linkFarm "flake-packages-${system}" self.packages.${system};

          pre-commit-check = pre-commit-hooks.lib.${system}.run {
            src = ./.;
            hooks = {
              nixfmt.enable = true;
              clang-format = {
                enable = true;
                types_or = pkgs.lib.mkForce [
                  "c"
                  "c++"
                ];
              };
            };
          };
        };
      }
    );
}
