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,10 +82,7 @@ in {
}; };
}; };
config = config = {
let
cfg = config.programs.rclone;
in {
home.packages = lib.optional cfg.enable pkgs.rclone; home.packages = lib.optional cfg.enable pkgs.rclone;
systemd.user = lib.optionalAttrs cfg.enable { systemd.user = lib.optionalAttrs cfg.enable {
@ -130,11 +140,16 @@ in {
in in
(lib.getExe script); (lib.getExe script);
}; };
Install = {
WantedBy = lib.optional sync.autoStart "${targetName}.target";
After = builtins.concatLists(builtins.attrValues sync.systemdDependencies);
};
}; };
}) })
cfg.configs); cfg.configs);
targets.rclone = { targets.${targetName} = {
Unit = { Unit = {
Description = "rclone Mounts"; Description = "rclone Mounts";
Documentation = "man:rclone(1)"; Documentation = "man:rclone(1)";