{
  config,
  pkgs,
  lib,
  ...
}:
with lib;
let
  cfg = config.custom.tailscale;
in
{
  imports = [ ./tailscale_lib.nix ];

  options.custom.tailscale = {
    enable = mkOption {
      type = lib.types.bool;
      default = true;
      description = "Whether to enable Tailscale";
    };
  };

  config = mkIf cfg.enable {
    services.tailscaleAutoconnect.enable = true;
    services.tailscale.package = pkgs.unstable.tailscale;
    environment.persistence.${config.custom.impermanence.persistPath}.directories = [ "/var/lib/tailscale" ];
  };
}
