galton/nix/hm-module.nix
2025-11-11 01:39:56 +01:00

103 lines
2.4 KiB
Nix

{
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.services.galton;
format = pkgs.formats.toml {};
removeNulls = filterAttrs (_: val: val != null);
configFile = format.generate "galton.toml" {
watch = removeNulls {
inherit (cfg) symlink notify;
remove-duplicates = cfg.removeDuplicates;
};
rule = map removeNulls cfg.rules;
};
in {
options.services.galton = {
enable = mkEnableOption "galton";
directory = mkOption {
type = types.str;
example = "~/Downloads";
description = "Directory to watch for new files";
};
symlink = mkOption {
type = types.nullOr types.str;
default = null;
description = "Create a symlink to matched files";
};
removeDuplicates = mkOption {
type = types.bool;
default = false;
description = "Remove duplicate downloads";
};
notify = mkOption {
type = types.bool;
default = false;
description = "Show notifications for moved downloads";
};
rules = mkOption {
default = [];
type = types.listOf (types.submodule {
options = {
name = mkOption {
type = types.nullOr types.str;
default = null;
};
url = mkOption {
type = types.nullOr types.str;
default = null;
};
referrer = mkOption {
type = types.nullOr types.str;
default = null;
};
move = mkOption {
type = types.nullOr types.str;
default = null;
};
rename = mkOption {
type = types.nullOr types.str;
default = null;
};
};
});
};
logLevel = mkOption {
type = types.str;
default = "info";
};
package = mkOption {
type = types.package;
defaultText = literalExpression "pkgs.galton";
description = "package to use";
};
};
config = mkIf cfg.enable {
systemd.user.services.galton = {
Unit = {
Description = "Galton directory watcher";
};
Service = {
Environment = "RUST_LOG=${cfg.logLevel}";
ExecStart = "${getExe cfg.package} --config ${configFile} watch ${cfg.directory}";
Restart = "on-failure";
RestartSec = 10;
};
Install = {
WantedBy = ["default.target"];
};
};
};
}