{ lib, config, options, pkgs, modulesPath, ... }:
with lib;
let
  cfg = config.yoda.spin-down.hd-idle;
in
{
  options = {
    yoda.spin-down.hd-idle = mkOption {
      type = types.listOf types.str;
      default = [ ];
      example = ["ata-ST6000DM003-2CY186_ZR11WA9K"];
      description = ''
        List with IDs (/dev/disk/by-id/<ID>) of HDDs to spin down with hd-idle after 5 minutes.

        Please try to use yoda.spin-down.hdparm first.
      '';
    };
  };

  config = mkIf (length cfg > 0) {
    systemd.services."hd-idle" = {
      description = "Spin down inactive HDDs with hd-idle";
      wantedBy = [ "multi-user.target" ];
      script =
        let
          args = strings.concatMapStrings (x: " -a disk/by-id/${x} -i 300") cfg;
        in ''
          ${pkgs.hd-idle}/bin/hd-idle ${args}
        '';
    };
  };
}
