{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-25.11";

    nix.url = "github:DeterminateSystems/nix-src/flake-schemas";

    flake-schemas.url = "github:DeterminateSystems/flake-schemas";

    git-hooks.url = "github:cachix/git-hooks.nix";
    git-hooks.inputs.nixpkgs.follows = "nixpkgs";

    home-manager.url = "github:nix-community/home-manager/release-25.11";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";

    nixvim.url = "github:nix-community/nixvim/nixos-25.11";
    nixvim.inputs.nixpkgs.follows = "nixpkgs";

    nur.url = "github:nix-community/NUR";
    nur.inputs.nixpkgs.follows = "nixpkgs";

    stylix.url = "github:nix-community/stylix/release-25.11";
    stylix.inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs =
    {
      self,
      nixpkgs,
      ...
    }@inputs:
    let
      supportedSystems = [
        "x86_64-linux"
        "aarch64-linux" # For testing only. Use at your own risk.
      ];

      forAllSystems = nixpkgs.lib.genAttrs supportedSystems;

      nixpkgsFor = forAllSystems (
        system:
        import nixpkgs {
          inherit system;
          overlays = [
            self.overlays.default
            inputs.nix.overlays.default
          ];
        }
      );

      test = {
        system = "x86_64-linux";
        lib = nixpkgs.lib.extend (final: prev: self.outputs.lib or { });
        inputs = inputs // {
          synix = self;
        };
        outputs = { };
        overlays = [
          self.overlays.default
          self.overlays.additions
          self.overlays.modifications
          (final: prev: { synix = self.packages."${final.system}"; })
        ];
      };
    in
    {
      inherit (inputs.flake-schemas) schemas;

      apps = forAllSystems (
        system:
        let
          pkgs = nixpkgs.legacyPackages.${system};
          mkApp = name: desc: {
            type = "app";
            program = pkgs.lib.getExe (pkgs.callPackage ./apps/${name} { });
            meta.description = desc;
          };
        in
        {
          create = mkApp "create" "Create a new NixOS configuration.";
          deploy = mkApp "deploy" "Deploy NixOS configurations in your flake.";
          install = mkApp "install" "Install a NixOS configuration.";
          rebuild = mkApp "rebuild" "Wrapper script for 'nixos-rebuild switch' and 'home-manager switch' commands.";
          update-packages = mkApp "update-packages" "Update all packages in this flake.";
          wake-host = mkApp "wake-host" "Wake a host with WakeOnLan.";
        }
      );

      lib = {
        utils = import ./lib/utils.nix { lib = nixpkgs.lib; };
      };

      packages = forAllSystems (
        system:
        let
          allArchs = import ./pkgs { pkgs = nixpkgs.legacyPackages.${system}; };
          x64only =
            if system == "x86_64-linux" then
              {
              }
            else
              { };
        in
        allArchs // x64only
      );

      overlays = import ./overlays { inherit inputs; };

      nixosModules = import ./modules/nixos;

      homeModules = import ./modules/home;

      # test configs
      nixosConfigurations = {
        nixos-hyprland = nixpkgs.lib.nixosSystem {
          inherit (test) system;
          modules = [
            ./tests/build/nixos-hyprland
            { nixpkgs.overlays = test.overlays; }
          ];
          specialArgs = {
            inherit (test) inputs outputs lib;
          };
        };
        nixos-server = nixpkgs.lib.nixosSystem {
          inherit (test) system;
          modules = [
            ./tests/build/nixos-server
            { nixpkgs.overlays = test.overlays; }
          ];
          specialArgs = {
            inherit (test) inputs outputs lib;
          };
        };
      };
      homeConfigurations = {
        hm-hyprland = inputs.home-manager.lib.homeManagerConfiguration {
          pkgs = import nixpkgs {
            inherit (test) overlays system;
          };
          extraSpecialArgs = {
            inherit (test) inputs outputs;
          };
          modules = [
            ./tests/build/hm-hyprland
          ];
        };
      };

      devShells = forAllSystems (
        system:
        let
          pkgs = nixpkgsFor.${system};
        in
        {
          default =
            let
              inherit (self.checks.${system}.pre-commit-check) shellHook enabledPackages;
            in
            pkgs.mkShell {
              inherit shellHook;
              nativeBuildInputs = [
                enabledPackages
                pkgs.nix
              ]
              ++ (with pkgs; [
                (python313.withPackages (
                  p: with p; [
                    mkdocs
                    mkdocs-material
                    mkdocs-material-extensions
                    pygments
                  ]
                ))
              ]);
            };
        }
      );

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

      checks = forAllSystems (
        system:
        let
          pkgs = nixpkgs.legacyPackages.${system};
          flakePkgs = self.packages.${system};
          overlaidPkgs = import nixpkgs {
            inherit system;
            overlays = [ self.overlays.modifications ];
          };
        in
        {
          pre-commit-check = inputs.git-hooks.lib.${system}.run {
            src = ./.;
            hooks = {
              nixfmt.enable = true;
            };
          };
          build-packages = pkgs.linkFarm "flake-packages-${system}" flakePkgs;
          build-overlays = pkgs.linkFarm "flake-overlays-${system}" {
            kicad = overlaidPkgs.kicad;
          };

          synapse-test =
            let
              testPkgs = import nixpkgs {
                inherit system;
                config.permittedInsecurePackages = [ "olm-3.2.16" ];
              };
            in
            testPkgs.testers.runNixOSTest ./tests/run/synapse.nix;

          # NOTE: disabled for now since the test takes too long to execute
          # open-webui-oci-test =
          #   let
          #     testPkgs = import nixpkgs {
          #       inherit system;
          #     };
          #   in
          #   testPkgs.testers.runNixOSTest ./tests/run/open-webui-oci.nix;
        }

      );

      hydraJobs = {
        inherit (self)
          packages
          ;
      };

      templates = {
        hetzner-amd = {
          path = ./templates/nix-configs/hetzner-amd;
          description = "Basic NixOS configuration for AMD based Hetzner VPS.";
        };
        hyprland = {
          path = ./templates/nix-configs/hyprland;
          description = "Basic NixOS configuration for clients running Hyprland with standalone Home Manager.";
        };
        pi4 = {
          path = ./templates/nix-configs/pi4;
          description = "Basic NixOS configuration for Raspberry Pi 4.";
        };
        server = {
          path = ./templates/nix-configs/server;
          description = "Basic NixOS configuration for servers.";
        };
        vm-uefi = {
          path = ./templates/nix-configs/vm-uefi;
          description = "Basic NixOS configuration for VMs (UEFI).";
        };

        microvm = {
          path = ./templates/microvm;
          description = "MicroVM NixOS configurations";
        };
        container = {
          path = ./templates/container;
          description = "Container NixOS configurations";
        };

        c-hello = {
          path = ./templates/dev/c-hello;
          description = "C hello world template.";
        };
        esp-blink = {
          path = ./templates/dev/esp-blink;
          description = "ESP32 blink template.";
        };
        flask-hello = {
          path = ./templates/dev/flask-hello;
          description = "Python Flask hello template.";
        };
        py-hello = {
          path = ./templates/dev/py-hello;
          description = "Python hello world template.";
        };
        rs-hello = {
          path = ./templates/dev/rs-hello;
          description = "Rust hello world template.";
        };
        stm32-blink = {
          path = ./templates/dev/stm32-blink;
          description = "STM32G4 blink template with libopencm3.";
        };
      };
    };
}
