{
  lib,
  config,
  options,
  ...
}:
let
  inherit (lib) mkIf mkOption mkEnableOption;
  inherit (lib.types)
    enum
    listOf
    submodule
    nonEmptyStr
    ;
  cfg = config.modules.server.navidrome;
in
{
  options.modules.server.navidrome = {
    enable = mkEnableOption "Navidrome";

    library = {
      type = mkOption {
        type = enum [
          "local"
          "nfs"
        ];
        default = "local";
      };
      path = mkOption { type = nonEmptyStr; };
      source = mkOption {
        default = { };
        type = submodule {
          options = {
            ip = mkOption { type = nonEmptyStr; };
            path = mkOption { type = nonEmptyStr; };
            options = mkOption {
              default = [
                "x-systemd.automount"
                "noauto"
                "ro"
              ];
              type = listOf nonEmptyStr;
            };
          };
        };
      };
    };
    settings = options.services.navidrome.settings;
  };

  config = mkIf cfg.enable {
    fileSystems.${cfg.library.path} = mkIf (cfg.library.type == "nfs") {
      device = "${cfg.library.source.ip}:${cfg.library.source.path}";
      fsType = "nfs";
      options = cfg.library.source.options;
    };

    services.navidrome = {
      enable = true;
      openFirewall = true;
      settings = {
        MusicFolder = cfg.library.path;
      } // cfg.settings;
    };

    systemd.services.navidrome.serviceConfig = {
      EnvironmentFile = config.age.secrets.navidrome.path;
    };
  };
}