{
  config,
  lib,
  ...
}:
let
  cfg = config.khscodes.networking.fqdn;
in
{
  options.khscodes.networking.fqdn = lib.mkOption {
    type = lib.types.nullOr lib.types.str;
    default = null;
    description = "Sets the FQDN of the machine. This is a prerequisite for many modules to be used";
  };

  config = lib.mkIf (cfg != null) (
    let
      hostname = builtins.head (lib.strings.splitString "." cfg);
      domain = if hostname == cfg then null else (lib.strings.removePrefix "${hostname}." cfg);
    in
    {
      networking.hostName = lib.mkForce hostname;
      networking.domain = lib.mkForce domain;
      networking.fqdn = cfg;
      # Add the name of the server to the ssh host certificate domains, but let other configs enable getting the host certificates.
      khscodes.services.openssh.hostCertificate.hostNames = [ cfg ];
      boot.kernel.sysctl = {
        "kernel.hostname" = cfg;
      };
    }
  );
}
