Allow specifying global default systemd dependencies

This commit is contained in:
Manuel Thalmann 2024-05-14 15:39:41 +02:00
parent e27dadb8e4
commit b7198ddeac

View file

@ -1,5 +1,8 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
let let
cfg = config.programs.rclone;
targetName = "rclone";
mkProvider = mkProvider =
{ secretLoader ? ({ ... }: ""), envLoader ? ({ ... }: { }), options ? {}, ... }: { { secretLoader ? ({ ... }: ""), envLoader ? ({ ... }: { }), options ? {}, ... }: {
inherit inherit
@ -9,6 +12,16 @@ let
; ;
}; };
mkSystemdDependencyOption =
{ default, ... }: lib.mkOption {
type = lib.types.attrsOf lib.types.str;
description = "The systemd services this sync depends on.";
example = {
secrets = "sops-nix.service";
};
inherit default;
};
syncProviders = { syncProviders = {
nextcloud = mkProvider {}; nextcloud = mkProvider {};
proton = mkProvider {}; proton = mkProvider {};
@ -30,12 +43,8 @@ let
default = true; default = true;
}; };
systemdDependencies = lib.mkOption { systemdDependencies = mkSystemdDependencyOption {
type = lib.types.attrsOf lib.types.str; default = cfg.systemdDependencies;
description = "The systemd services this sync depends on.";
example = {
secrets = "sops-nix.service";
};
}; };
environment = lib.mkOption { environment = lib.mkOption {
@ -61,6 +70,10 @@ in {
programs.rclone = { programs.rclone = {
enable = lib.mkEnableOption "rclone"; enable = lib.mkEnableOption "rclone";
systemdDependencies = mkSystemdDependencyOption {
default = {};
};
configs = lib.mkOption { configs = lib.mkOption {
type = lib.types.attrsOf syncType; type = lib.types.attrsOf syncType;
description = "The synchronizations to set up."; description = "The synchronizations to set up.";
@ -69,77 +82,79 @@ in {
}; };
}; };
config = config = {
let home.packages = lib.optional cfg.enable pkgs.rclone;
cfg = config.programs.rclone;
in {
home.packages = lib.optional cfg.enable pkgs.rclone;
systemd.user = lib.optionalAttrs cfg.enable { systemd.user = lib.optionalAttrs cfg.enable {
enable = true; enable = true;
services = { services = {
rclone = { rclone = {
Unit = {
Description = "rclone Starter";
Documentation = "man:rclone(1)";
};
Service = {
Type = "simple";
ExecStartPre =
let
script = pkgs.writeShellScriptBin "rclone-pre" ''
sleep 10
'';
in
(lib.getExe script);
ExecStart =
let
script = pkgs.writeShellScriptBin "rclone" ''
systemctl --user start rclone.target
'';
in
(lib.getExe script);
};
};
} // (
lib.attrsets.concatMapAttrs
(name: sync:
let
serviceName = "rclone-sync-${name}";
in {
${serviceName} = {
Unit = {
Description = "${sync.type}";
};
Service = {
Environment = lib.mapAttrsToList
(key: val: (lib.escapeShellArg "${key}=${val}"))
sync.environment;
ExecStart =
let
script = pkgs.writeShellScriptBin serviceName ''
${sync.secretsScript}
bash -c echo hello world
'';
in
(lib.getExe script);
};
};
})
cfg.configs);
targets.rclone = {
Unit = { Unit = {
Description = "rclone Mounts"; Description = "rclone Starter";
Documentation = "man:rclone(1)"; Documentation = "man:rclone(1)";
}; };
Service = {
Type = "simple";
ExecStartPre =
let
script = pkgs.writeShellScriptBin "rclone-pre" ''
sleep 10
'';
in
(lib.getExe script);
ExecStart =
let
script = pkgs.writeShellScriptBin "rclone" ''
systemctl --user start rclone.target
'';
in
(lib.getExe script);
};
};
} // (
lib.attrsets.concatMapAttrs
(name: sync:
let
serviceName = "rclone-sync-${name}";
in {
${serviceName} = {
Unit = {
Description = "${sync.type}";
};
Service = {
Environment = lib.mapAttrsToList
(key: val: (lib.escapeShellArg "${key}=${val}"))
sync.environment;
ExecStart =
let
script = pkgs.writeShellScriptBin serviceName ''
${sync.secretsScript}
bash -c echo hello world
'';
in
(lib.getExe script);
};
Install = {
WantedBy = lib.optional sync.autoStart "${targetName}.target";
After = builtins.concatLists(builtins.attrValues sync.systemdDependencies);
};
};
})
cfg.configs);
targets.${targetName} = {
Unit = {
Description = "rclone Mounts";
Documentation = "man:rclone(1)";
}; };
}; };
}; };
};
} }