{ config, lib, pkgs, ... }:

let cfg = config.sydnix.gpg;
    mutableSymlink = config.lib.file.mkOutOfStoreSymlink;
in {
  options = {
    sydnix.gpg = {
      enable = lib.mkEnableOption "GnuPG";
    };
  };

  config = lib.mkIf cfg.enable {
    programs.gpg = {
      enable = true;
    };

    services.gpg-agent = {
      enable = true;
      enableSshSupport = true;
      enableBashIntegration = config.programs.bash.enable;
      extraConfig =
        "allow-loopback-pinentry"
        + lib.optionalString
          config.programs.emacs.enable "\nallow-emacs-pinentry";
    };

    home.file."private-keys/gpg/crumb".source =
      mutableSymlink "/persist/private-keys/gpg/${config.home.username}";
  };
}
