{
  config,
  lib,
  outputs,
  ...
}: let
  mountDir = "/mnt/sync";
  configDir = "/etc/syncthing";
in {
  options.host.sync = {
    enable = lib.mkEnableOption "should sync thing be enabled on this device";
    folders = {
      share = {
        enable = lib.mkEnableOption "should the share folder by synced";
        calendar = {
          enable = lib.mkEnableOption "should the calendar folder be synced";
        };
      };
      leyla = {
        documents = {
          enable = lib.mkEnableOption "should the documents folder be synced";
        };
        notes = {
          enable = lib.mkEnableOption "should the notes folder by synced";
        };
      };
      extraFolders = lib.mkOption {
        type = lib.types.attrsOf (lib.types.submodule ({...}: {
          options = {
            path = lib.mkOption {
              type = lib.types.str;
            };
            devices = lib.mkOption {
              type = lib.types.listof lib.types.str;
            };
          };
        }));
        default = {};
      };
    };
  };

  config = lib.mkMerge [
    {
      systemd = lib.mkIf config.services.syncthing.enable {
        tmpfiles.rules = [
          "d ${mountDir} 2755 syncthing syncthing -"
          "d ${config.services.syncthing.dataDir} 775 syncthing syncthing -"
          "d ${config.services.syncthing.configDir} 755 syncthing syncthing -"
        ];
      };
    }
    (lib.mkIf config.host.sync.enable (lib.mkMerge [
      {
        services.syncthing = {
          enable = true;
          user = "syncthing";
          group = "syncthing";
          dataDir = "${mountDir}/default";
          configDir = configDir;
          overrideDevices = true;
          overrideFolders = true;
          configuration = outputs.syncthingConfiguration;
          deviceName = config.networking.hostName;
        };
      }

      (lib.mkIf config.host.impermanence.enable {
        assertions =
          [
            {
              assertion = config.services.syncthing.configDir == configDir;
              message = "syncthing config dir does not match persistence";
            }
          ]
          ++ lib.attrsets.mapAttrsToList (_: folder: {
            assertion = lib.strings.hasPrefix mountDir folder.path;
            message = "syncthing folder ${folder.label} is stored at ${folder.path} which not under the persisted path of ${mountDir}";
          })
          config.services.syncthing.settings.folders;
        environment.persistence = {
          "/persist/system/root" = {
            enable = true;
            hideMounts = true;
            directories = [
              {
                directory = mountDir;
                user = "syncthing";
                group = "syncthing";
              }
              {
                directory = configDir;
                user = "syncthing";
                group = "syncthing";
              }
            ];
          };
        };
      })
    ]))
  ];
}