{ config, lib, ... }:
let
  cfg = config.khscodes.services.alloy;
in
{
  options.khscodes.services.alloy = {
    enable = lib.mkEnableOption "Enables alloy";
    user = lib.mkOption {
      type = lib.types.str;
      default = "alloy";
    };
    group = lib.mkOption {
      type = lib.types.str;
      default = "alloy";
    };
    environment = lib.mkOption {
      type = lib.types.attrsOf lib.types.str;
      default = { };
    };
  };

  config = lib.mkIf cfg.enable {
    services.alloy.enable = true;
    systemd.services.alloy = {
      serviceConfig = {
        DynamicUser = lib.mkForce false;
        User = "${cfg.user}";
        Group = "${cfg.group}";
      };
      environment = cfg.environment;
    };
    users.users.${cfg.user} = {
      description = "Alloy service user";
      isSystemUser = true;
      group = cfg.group;
    };
    users.groups.${cfg.group} = { };
  };
}
