{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
    pre-commit-hooks = {
      url = "github:cachix/pre-commit-hooks.nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    ysyx-workbench = {
      url = "git+https://git.xinyang.life/xin/ysyx-workbench";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, flake-utils, nixpkgs, pre-commit-hooks, ysyx-workbench }:
    flake-utils.lib.eachDefaultSystem (system:
      let
        pkgs = import nixpkgs { inherit system; };

        rv32CrossConfig = import nixpkgs {
          localSystem = system;
          crossSystem = {
            config = "riscv32-none-elf";
            gcc = {
              abi = "ilp32";
              arch = "rv32if";
            };
          };
        };

        ysyx-pkgs = ysyx-workbench.packages.${system};
      in
      {
        checks = {
          pre-commit-check = pre-commit-hooks.lib.${system}.run {
            src = ./.;
            hooks = {
              trim-trailing-whitespace.enable = true;
              end-of-file-fixer.enable = true;
              cmake-format.enable = true;
            };
          };
        };

        packages = {
          am-kernels = pkgs.callPackage ./default.nix { inherit (ysyx-pkgs) abstract-machine; arch = "native"; };

          rv32Cross = {
            am-kernels-npc = rv32CrossConfig.callPackage ./default.nix {
              inherit (ysyx-pkgs.rv32Cross) abstract-machine;
              arch = "riscv-npc";
            };
            am-kernels-nemu = rv32CrossConfig.callPackage ./default.nix {
              inherit (ysyx-pkgs.rv32Cross) abstract-machine;
              arch = "riscv-nemu";
            };
          };
        };
      });
}
