inputs@{
  nixpkgs,
  deploy-rs,
  self,
  pkgsForSystem,
  ...
}:

rec {
  configs =
    configs: builtins.foldl' (acc: val: nixpkgs.lib.recursiveUpdate (config val) acc) { } configs;
  config =
    {
      hostname,
      capabilities,
      type,
      home-only ? null,
      extra-modules ? [ ],
      system ? "x86_64-linux",
      deploy-hostname ? hostname,
      deploy-options ? {
        user = if builtins.isNull home-only then "root" else home-only;
        sshUser = if builtins.isNull home-only then "jana" else home-only;
      },
      home-manager ? builtins.isNull home-only,
      stateVersion ? "26.05",
    }:
    with nixpkgs.lib;
    let
      inherit (nixpkgs) lib;
      matches-capabilities =
        # all requirements are contained in the machine capabilities
        requirements: lib.all (req: builtins.elem req capabilities) requirements;
      program =
        {
          inputs,
          name,
          requirements ? [ ],
          home-config,
          system-config ? (_: { }),
        }:
        if (matches-capabilities requirements) then
          [ (if builtins.isNull home-only then (system-config inputs) else (home-config inputs)) ]
          ++

            (
              if builtins.isNull home-only then
                [
                  (_: {
                    custom.program.${name}.defered-config = home-config;
                  })
                ]
              else
                [ ]
            )
        else
          [ ];
      specialArgsForHomeSystem =
        {
          system,
          type,
          capabilities,
        }:
        home-only: {
          pkgs = pkgsForSystem system;
          flakes = inputs;
          inherit inputs;
          inherit (inputs.secrets.packages.${system}) secrets;
          machine = {
            inherit
              type
              capabilities
              stateVersion
              home-only
              program
              ;
          };
        };
      specialArgsForSystem = system: specialArgsForHomeSystem system null;

      specialArgs = specialArgsForSystem {
        inherit system type capabilities;
      };
      modules =
        extra-modules
        ++ [ ./hosts/${hostname}/configuration.nix ]
        ++ (
          if builtins.isNull home-only then
            [ ./defaults/machine-config.nix ]
          else
            [ ./defaults/machine-or-home-config.nix ]
        )
        ++ (
          if home-manager then
            [
              inputs.home-manager.nixosModules.default
              {
                home-manager.extraSpecialArgs = specialArgs;
              }
            ]
          else
            [ ]
        );
    in
    {
      deploy.nodes.${hostname} = {
        hostname = deploy-hostname;
        fastConnection = true;
        profiles.system = {
          path =
            if (builtins.isNull home-only) then
              deploy-rs.lib.x86_64-linux.activate.nixos self.nixosConfigurations.${hostname}
            else
              deploy-rs.lib.x86_64-linux.activate.home-manager self.nixosConfigurations.${hostname};
        }
        // deploy-options;
      };

      nixosConfigurations.${hostname} =
        if builtins.isNull home-only then
          (nixosSystem {
            inherit system modules specialArgs;
          })
        else
          inputs.home-manager.lib.homeManagerConfiguration {
            extraSpecialArgs = specialArgsForHomeSystem {
              inherit system type capabilities;
            } home-only;
            inherit modules;
            pkgs = pkgsForSystem system;
          };
    };
}
