{
  description = "Alo cluster";

  inputs = {
    deploy-rs.url = "github:serokell/deploy-rs";
    deploy-rs.inputs.nixpkgs.follows = "nixpkgs";
    impermanence.url = "github:nix-community/impermanence";
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
    nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
    disko.url = "github:nix-community/disko";
    disko.inputs.nixpkgs.follows = "nixpkgs";
    ethereum-nix = {
      url = "github:nix-community/ethereum.nix";
      inputs.nixpkgs.follows = "nixpkgs-unstable";
    };
    home-manager = {
      url = "github:nix-community/home-manager/release-25.11";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    nix-index-database = {
      url = "github:nix-community/nix-index-database";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    nixvim = {
      url = "github:nix-community/nixvim";
      inputs.nixpkgs.follows = "nixpkgs-unstable";
    };
    sops-nix = {
      url = "github:Mic92/sops-nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    browser-previews = {
      url = "github:nix-community/browser-previews";
      inputs.nixpkgs.follows = "nixpkgs-unstable";
    };
    nix-colors.url = "github:misterio77/nix-colors";
    nixos-hardware.url = "github:NixOS/nixos-hardware/master";
  };

  outputs =
    {
      self,
      nixpkgs,
      nixpkgs-unstable,
      deploy-rs,
      disko,
      ethereum-nix,
      home-manager,
      impermanence,
      sops-nix,
      browser-previews,
      nix-colors,
      nixos-hardware,
      ...
    }@inputs:
    let
      inherit (self);

      overlay-unstable = final: prev: {
        unstable = import nixpkgs-unstable {
          system = prev.stdenv.hostPlatform.system;
          config.allowUnfree = true;
        };
      };

      overlay-browser-previews = final: prev: {
        browser-previews = browser-previews.packages.${prev.stdenv.hostPlatform.system};
      };

      mkHost =
        system: profile: modules:
        let
          # Profile parameter is only used by home-manager for user environment
          # NixOS system configuration is handled via explicit imports in host configs
        in
        nixpkgs.lib.nixosSystem {
          system = system;
          modules = [
            (
              { config, pkgs, ... }:
              {
                nixpkgs.overlays = [ overlay-unstable overlay-browser-previews ];
                nixpkgs.config.allowUnfree = true;
              }
            )
            disko.nixosModules.disko
            sops-nix.nixosModules.sops
            impermanence.nixosModules.impermanence
            home-manager.nixosModules.home-manager
            (
              { lib, ... }:
              {
                home-manager = {
                  useGlobalPkgs = true;
                  useUserPackages = true;
                  users.ppetru = {
                    imports = [
                      inputs.nix-index-database.homeModules.nix-index
                      inputs.nixvim.homeModules.nixvim
                      ./home
                    ] ++ lib.optionals (profile == "desktop") [
                      nix-colors.homeManagerModules.default
                    ];
                  };
                  extraSpecialArgs = {
                    inherit profile nix-colors;
                  };
                };
              }
            )
          ] ++ nixpkgs.lib.optionals (profile == "desktop") [
            ./common/desktop
          ] ++ modules;
          specialArgs = {
            inherit inputs self;
          };
        };

      pkgsFor =
        system:
        import nixpkgs {
          inherit system;
          overlays = [ overlay-unstable ];
        };

      deployPkgsFor =
        system:
        import nixpkgs {
          inherit system;
          overlays = [
            overlay-unstable
            deploy-rs.overlays.default
            (self: super: {
              deploy-rs = {
                inherit (pkgsFor system) deploy-rs;
                lib = super.deploy-rs.lib;
              };
            })
          ];
        };
    in
    {
      nixosConfigurations = {
        c1 = mkHost "x86_64-linux" "minimal" [ ./hosts/c1 ];
        c2 = mkHost "x86_64-linux" "minimal" [ ./hosts/c2 ];
        c3 = mkHost "x86_64-linux" "minimal" [ ./hosts/c3 ];
        alo-cloud-1 = mkHost "aarch64-linux" "cloud" [ ./hosts/alo-cloud-1 ];
        zippy = mkHost "x86_64-linux" "minimal" [ ./hosts/zippy ];
        chilly = mkHost "x86_64-linux" "workstation" [ ./hosts/chilly ];
        sparky = mkHost "x86_64-linux" "minimal" [ ./hosts/sparky ];
        beefy = mkHost "x86_64-linux" "desktop" [ ./hosts/beefy ];
        stinky = mkHost "aarch64-linux" "minimal" [
          nixos-hardware.nixosModules.raspberry-pi-4
          ./hosts/stinky
        ];
      };

      deploy = {
        nodes = {
          c1 = {
            hostname = "c1";
            profiles.system = {
              user = "root";
              path = (deployPkgsFor "x86_64-linux").deploy-rs.lib.activate.nixos self.nixosConfigurations.c1;
            };
          };
          c2 = {
            hostname = "c2";
            profiles.system = {
              user = "root";
              path = (deployPkgsFor "x86_64-linux").deploy-rs.lib.activate.nixos self.nixosConfigurations.c2;
            };
          };
          c3 = {
            hostname = "c3";
            profiles.system = {
              user = "root";
              path = (deployPkgsFor "x86_64-linux").deploy-rs.lib.activate.nixos self.nixosConfigurations.c3;
            };
          };
          alo-cloud-1 = {
            hostname = "alo-cloud-1";
            #hostname = "49.13.163.72";
            profiles = {
              system = {
                user = "root";
                path = (deployPkgsFor "aarch64-linux").deploy-rs.lib.activate.nixos self.nixosConfigurations.alo-cloud-1;
              };
            };
          };
          zippy = {
            hostname = "zippy";
            profiles = {
              system = {
                user = "root";
                path = (deployPkgsFor "x86_64-linux").deploy-rs.lib.activate.nixos self.nixosConfigurations.zippy;
              };
            };
          };
          chilly = {
            hostname = "chilly";
            profiles = {
              system = {
                user = "root";
                path = (deployPkgsFor "x86_64-linux").deploy-rs.lib.activate.nixos self.nixosConfigurations.chilly;
              };
            };
          };
          sparky = {
            hostname = "sparky";
            profiles = {
              system = {
                user = "root";
                path = (deployPkgsFor "x86_64-linux").deploy-rs.lib.activate.nixos self.nixosConfigurations.sparky;
              };
            };
          };
          beefy = {
            hostname = "beefy";
            profiles = {
              system = {
                user = "root";
                path = (deployPkgsFor "x86_64-linux").deploy-rs.lib.activate.nixos self.nixosConfigurations.beefy;
              };
            };
          };
          stinky = {
            hostname = "stinky";
            profiles = {
              system = {
                user = "root";
                path = (deployPkgsFor "aarch64-linux").deploy-rs.lib.activate.nixos self.nixosConfigurations.stinky;
              };
            };
          };
        };
      };

      # SD card image for stinky (Raspberry Pi 4)
      packages.aarch64-linux.stinky-sdImage = self.nixosConfigurations.stinky.config.system.build.sdImage;

      # Apps - utility scripts
      apps.x86_64-linux.diff-configs = {
        type = "app";
        program = "${(pkgsFor "x86_64-linux").writeShellScriptBin "diff-configs" (builtins.readFile ./scripts/diff-configs.sh)}/bin/diff-configs";
      };

      apps.aarch64-linux.diff-configs = {
        type = "app";
        program = "${(pkgsFor "aarch64-linux").writeShellScriptBin "diff-configs" (builtins.readFile ./scripts/diff-configs.sh)}/bin/diff-configs";
      };

      # Development shells
      devShells.x86_64-linux.default = (pkgsFor "x86_64-linux").mkShell {
        packages = with (pkgsFor "x86_64-linux"); [
          nvd
        ];
      };

      devShells.aarch64-linux.default = (pkgsFor "aarch64-linux").mkShell {
        packages = with (pkgsFor "aarch64-linux"); [
          nvd
        ];
      };

      checks = builtins.mapAttrs (system: deployLib: deployLib.deployChecks self.deploy) deploy-rs.lib;

      formatter.x86_64-linux = nixpkgs.legacyPackages.x86_64-linux.nixfmt-rfc-style;
    };
}
