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

with lib;
let
  cfg = config.modules.system.bitcoin;

  home = "/var/lib/bitcoind";
  conf = pkgs.writeText "bitcoin.conf" (import ./config);


in
{ options.modules.system.bitcoin = { enable = mkEnableOption "system.bitcoin"; };
  imports = [ ./modules ];
  config = mkIf cfg.enable {
    nixpkgs.overlays = [
      (final: prev: {
        bitcoind = prev.bitcoind.overrideAttrs (old: rec {
          version = "27.0";
          src = fetchTarball {
            url = "https://github.com/bitcoin/bitcoin/archive/refs/tags/v${version}.tar.gz";
            sha256 = "sha256-U2tR3WySD3EssA3a14wUtA3e0t/5go0isqNZSSma7m4=";
          };
        });
      })
    ];

    users = {
      users = {
        "btc" = {
          inherit home;
          description = "Bitcoin Core system user";
          isSystemUser = true;
          group = "bitcoin";
          createHome = true;
        };
      };
      groups = {
        "bitcoin" = {
          members = [
            "btc"
            "electrs"
          ];
        };
      };
    };

    programs.bash.shellAliases = {
      btc = "bitcoind";
    };

    networking.firewall.allowedTCPPorts = [ 8333 ];

    services.bitcoind = {
      "btc" = {
        enable = true;
        user = "btc";
        group = "bitcoin";
        configFile = conf;
        dataDir = home;
        pidFile = "${home}/bitcoind.pid";
      };
    };
  };
}
