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

let cfg = config.sydnix.dots;
in {
  options.sydnix.dots = {
    enable = lib.mkEnableOption "management of the dotfiles directory" // {
      default = true;
    };
    directory = lib.mkOption {
      default = "/persist/dots";
      type = lib.types.path;
      description = ''
        Path to a checkout of the Sydnix repo.  The environment variable
        SYDNIX_PATH will be set to this.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    environment.variables.SYDNIX_PATH = cfg.directory;

    # Group that owns `cfg.directory`.
    users.groups."dots" = {};

    # Ensure correct permissions on `cfg.directory`.
    systemd.tmpfiles.settings."50-dots" = {
      ${cfg.directory} = {
        z.group = "dots";
        z.user = "root";
        z.mode = "2770";
      };
    };
  };
}
