{ lib, ... }: {
  flake.lib.sshInventory =
    let
      hosts = {
        "14900k" = {
          hostName = "192.168.2.25";
          aliases = [ "14900k" "nixdesk" ];
          publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAILwUevBGnf+Y/sL1ZsB4bt0c50a89iqwPRoYUGP4UHsL 14900k";
        };

        ideapad = {
          hostName = "192.168.2.229";
          aliases = [ "ideapad" ];
          publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIQwaaI90xIMjZ46EcMyO8kBwGCxf7qVL75IYhw8Ssze ideapad";
        };

        t2mbp = {
          hostName = "192.168.2.15";
          aliases = [ "t2mbp" ];
          publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMhVWB9YXl/FuQvufle4VWUas/QM8qCKoRd5a83Tt3S6 t2mbp";
        };

        uConsole = {
          hostName = "192.168.2.99";
          aliases = [ "uConsole" "uconsole" ];
          publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIAVPR0lRAcywPR7iTchM3+eO7NCdXAR6NPzYXxalr+dP uConsole";
        };

        test = {
          hostName = "test";
          aliases = [ "test" ];
          publicKey = null;
        };

        nix-server = {
          hostName = "192.168.2.238";
          aliases = [ "nix-server" ];
          publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIL3KDicMjtOFR6LfZrFzfAD1gdYUdwv6ZM4PSgtmIuzd nix-server";
        };

        r5500 = {
          hostName = "192.168.2.100";
          aliases = [ "r5500" ];
          publicKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIK7iWCEtkYDLZFRF3w1gzyAok5VCAGUOwu4iWZdMjf3D r5500";
        };
      };

      mkIdentityFileName = hostName: ".ssh/id_ed25519_${lib.strings.toLower hostName}.pub";
      activeHosts = builtins.removeAttrs hosts (
        builtins.filter (name: hosts.${name}.publicKey == null) (builtins.attrNames hosts)
      );

      mkIdentityFiles = selectedHosts:
        builtins.listToAttrs (
          builtins.map
            (hostName: {
              name = mkIdentityFileName hostName;
              value.text = "${selectedHosts.${hostName}.publicKey}\n";
            })
            (builtins.attrNames selectedHosts)
        );

      # Gitea git-over-SSH listens on port 222. System SSH (nix deploy, server@…) uses port 22
      # via the catalog `nix-server` Host block — never list nix-server or 192.168.2.238 here.
      giteaSshBlock = identityAgent: ''
        Host git.chiasson.cloud gitea
          HostName 192.168.2.238
          Port 222
          User git
          IdentityAgent ${identityAgent}
          IdentitiesOnly no

        Match host nix-server,192.168.2.238 user git
          HostName 192.168.2.238
          Port 222
          User git
          IdentityAgent ${identityAgent}
          IdentitiesOnly no
      '';

      mkSshConfigTemplate =
        {
          selectedHosts ? activeHosts,
          user ? null,
          identityAgent ? "__RBW_SSH_SOCK__",
        }:
        let
          hostBlocks = builtins.map
            (hostName:
              let
                entry = selectedHosts.${hostName};
                hostPatterns = builtins.concatStringsSep " " (entry.aliases ++ [ entry.hostName ]);
                userLine = if user == null then "" else "  User ${user}\n";
                portLine =
                  if hostName == "nix-server" then
                    "  Port 22\n"
                  else
                    "";
              in
              ''
                Host ${hostPatterns}
                  HostName ${entry.hostName}
${userLine}${portLine}                  IdentityFile ~/${mkIdentityFileName hostName}
                  IdentityAgent ${identityAgent}
                  IdentitiesOnly yes
              '')
            (builtins.attrNames selectedHosts);
        in
        builtins.concatStringsSep "\n" (
          [
            (giteaSshBlock identityAgent)
          ]
          ++ hostBlocks
          ++ [
            ''
              Host *
                IdentitiesOnly yes
                IdentityAgent none
            ''
          ]
        );
    in
    {
      inherit hosts activeHosts mkIdentityFiles mkSshConfigTemplate;
      authorizedKeys = lib.unique (
        builtins.map (entry: entry.publicKey) (builtins.attrValues activeHosts)
      );
      identityFiles = mkIdentityFiles activeHosts;
      sshConfigTemplate = mkSshConfigTemplate { };
    };
}
