nixos-config/home-manager/modules/rclone/default.nix

96 lines
2.4 KiB
Nix
Raw Normal View History

2024-02-05 11:46:52 +01:00
{
pkgs,
config,
lib,
...
}:
2022-07-07 16:39:01 +02:00
with lib;
let
cfg = config.eboskma.programs.rclone;
2024-02-05 11:46:52 +01:00
rcloneConnectionString =
mount:
2022-07-07 16:39:01 +02:00
let
type = mount.remoteConfig.type;
config = builtins.removeAttrs mount.remoteConfig [ "type" ];
connectionStringOptions = mapAttrsToList (key: value: "${key}=${builtins.toJSON value}") config;
in
":${type},${builtins.concatStringsSep "," connectionStringOptions}:${mount.remote}";
2024-02-05 11:46:52 +01:00
rcloneRemote =
with types;
2024-04-16 19:32:32 +02:00
attrsOf (oneOf [
str
int
bool
]);
2022-07-07 16:39:01 +02:00
2024-02-05 11:46:52 +01:00
rcloneMount =
with types;
submodule {
options = {
remoteConfig = mkOption {
description = "Configuration for the remote to use";
type = rcloneRemote;
};
remote = mkOption {
description = "Remote path";
type = str;
};
local = mkOption {
description = "Local mountpoint";
type = path;
};
2022-07-07 16:39:01 +02:00
};
};
in
{
options.eboskma.programs.rclone = {
enable = mkEnableOption "rclone";
mounts = mkOption {
description = "Mount remotes on a local path";
type = with types; listOf rcloneMount;
default = [ ];
};
};
2022-10-05 11:32:13 +02:00
config = mkIf cfg.enable {
2022-07-07 16:39:01 +02:00
home.packages = [ pkgs.rclone ];
2024-02-05 11:46:52 +01:00
systemd.user.services = builtins.listToAttrs (
2024-04-16 19:32:32 +02:00
builtins.map (
mount:
let
localPath = toString mount.local;
unitName = builtins.replaceStrings [ "/" ] [ "-" ] (
builtins.substring 1 (builtins.stringLength localPath) localPath
);
in
{
name = "rclone-${unitName}";
value = {
Unit = {
Description = "rclone mount ${unitName}";
After = [ "network.target" ];
AssertPathIsDirectory = toString mount.local;
};
2022-07-07 16:39:01 +02:00
2024-04-16 19:32:32 +02:00
Service = {
Environment = [ "PATH=/run/wrappers/bin:$PATH" ];
ExecStart = "${pkgs.rclone}/bin/rclone mount --vfs-cache-mode full --vfs-cache-poll-interval 5s --poll-interval 5s --dir-cache-time 10s --umask 022 --allow-other ${rcloneConnectionString mount} ${mount.local}";
ExecStop = "/run/wrappers/bin/fusermount -zu ${mount.local}";
Restart = "on-failure";
RestartSec = 10;
};
2022-07-07 16:39:01 +02:00
2024-04-16 19:32:32 +02:00
Install = {
WantedBy = [ "default.target" ];
2022-07-07 16:39:01 +02:00
};
2024-04-16 19:32:32 +02:00
};
}
) cfg.mounts
2024-02-05 11:46:52 +01:00
);
2022-07-07 16:39:01 +02:00
};
}