nixfiles/modules/nixos/rcloneServe.nix

88 lines
2.1 KiB
Nix
Raw Normal View History

{
config,
lib,
pkgs,
...
2023-09-18 03:56:58 +01:00
}: let
inherit (lib.modules) mkIf mkMerge;
inherit (lib.options) mkOption;
inherit (lib.strings) concatStringsSep;
inherit (lib) types;
inherit (builtins) listToAttrs;
2022-10-27 20:27:22 +01:00
2023-09-18 03:56:58 +01:00
cfg = config.services.rclone-serve;
2022-10-27 20:27:22 +01:00
2023-09-18 03:56:58 +01:00
daemonService = serveConfig:
mkMerge [
2022-11-03 06:44:02 +00:00
{
wantedBy = ["multi-user.target"];
after = ["network.target"];
wants = ["network.target"];
2022-11-03 06:44:02 +00:00
serviceConfig = {
Type = "simple";
Restart = "on-failure";
2023-09-18 03:56:58 +01:00
RestartSec = "10s";
2022-11-03 06:44:02 +00:00
2023-09-18 03:56:58 +01:00
User = serveConfig.user;
2022-11-03 06:44:02 +00:00
2023-09-18 03:56:58 +01:00
ExecStart = "${pkgs.rclone}/bin/rclone serve ${serveConfig.type} ${serveConfig.remote} ${
concatStringsSep " " serveConfig.extraArgs
}";
2022-11-03 06:44:02 +00:00
};
}
2023-09-18 03:56:58 +01:00
serveConfig.serviceConfig
2022-11-03 06:44:02 +00:00
];
2022-10-27 20:27:22 +01:00
in {
options = {
services.rclone-serve = {
enable = mkOption {
type = types.bool;
default = false;
};
remotes = mkOption {
type = types.listOf (types.submodule {
options = {
2023-09-18 03:56:58 +01:00
id = mkOption {
type = types.str;
# TODO: add a assertion for this
description = "ID for the serve systemd unit; doesn't need to be unique as long as there is no other with same ID and same remote";
};
remote = mkOption {
type = types.str;
};
type = mkOption {
type = types.str;
default = "webdav";
};
user = mkOption {
type = types.str;
default = "root";
};
serviceConfig = mkOption {
type = types.attrs;
default = {};
};
extraArgs = mkOption {
type = types.listOf types.str;
default = [];
};
2022-10-27 20:27:22 +01:00
};
});
default = [];
2022-10-27 20:27:22 +01:00
};
};
};
config = mkMerge [
(mkIf (cfg.enable && cfg.remotes != []) {
2022-10-27 20:27:22 +01:00
systemd.services = listToAttrs (map (remote: {
2023-09-18 03:56:58 +01:00
name = "rclone-serve-${remote.type}-${remote.id}";
value = daemonService remote;
})
cfg.remotes);
2022-10-27 20:27:22 +01:00
})
];
}