|
|
|
@ -1,9 +1,9 @@
|
|
|
|
|
{
|
|
|
|
|
lib,
|
|
|
|
|
config,
|
|
|
|
|
pkgs,
|
|
|
|
|
...
|
|
|
|
|
}: let
|
|
|
|
|
{ lib
|
|
|
|
|
, config
|
|
|
|
|
, pkgs
|
|
|
|
|
, ...
|
|
|
|
|
}:
|
|
|
|
|
let
|
|
|
|
|
secretType = serviceName:
|
|
|
|
|
lib.types.submodule ({ config, ... }: {
|
|
|
|
|
options = {
|
|
|
|
@ -59,19 +59,22 @@
|
|
|
|
|
|
|
|
|
|
vaultTemplates = config:
|
|
|
|
|
(lib.mapAttrsToList
|
|
|
|
|
(serviceName: service:
|
|
|
|
|
(serviceName: _service:
|
|
|
|
|
getSecretTemplate serviceName services.${serviceName}.vault)
|
|
|
|
|
(lib.filterAttrs (n: v: v.vault.secrets != {} && v.vault.agent == config._module.args.name) services))
|
|
|
|
|
(lib.filterAttrs (_n: v: v.vault.secrets != { } && v.vault.agent == config._module.args.name) services))
|
|
|
|
|
++ (lib.mapAttrsToList
|
|
|
|
|
(serviceName: service:
|
|
|
|
|
(serviceName: _service:
|
|
|
|
|
getEnvironmentTemplate serviceName services.${serviceName}.vault)
|
|
|
|
|
(lib.filterAttrs (n: v: v.vault.environmentTemplate != null && v.vault.agent == config._module.args.name) services));
|
|
|
|
|
in {
|
|
|
|
|
(lib.filterAttrs (_n: v: v.vault.environmentTemplate != null && v.vault.agent == config._module.args.name) services));
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
options = {
|
|
|
|
|
systemd.services = lib.mkOption {
|
|
|
|
|
type = lib.types.attrsOf (lib.types.submodule ({config, ...}: let
|
|
|
|
|
type = lib.types.attrsOf (lib.types.submodule ({ config, ... }:
|
|
|
|
|
let
|
|
|
|
|
serviceName = config._module.args.name;
|
|
|
|
|
in {
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
options.vault = {
|
|
|
|
|
changeAction = lib.mkOption {
|
|
|
|
|
description = ''
|
|
|
|
@ -117,9 +120,11 @@ in {
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
config = let
|
|
|
|
|
config =
|
|
|
|
|
let
|
|
|
|
|
mkIfHasEnv = lib.mkIf (config.vault.environmentTemplate != null);
|
|
|
|
|
in {
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
after = mkIfHasEnv [ "${serviceName}-envfile.service" ];
|
|
|
|
|
bindsTo = mkIfHasEnv [ "${serviceName}-envfile.service" ];
|
|
|
|
|
|
|
|
|
@ -140,14 +145,17 @@ in {
|
|
|
|
|
|
|
|
|
|
config = {
|
|
|
|
|
# we cannot use `systemd.services` here since this would create infinite recursion
|
|
|
|
|
systemd.packages = let
|
|
|
|
|
servicesWithEnv = builtins.attrNames (lib.filterAttrs (n: v: v.vault.environmentTemplate != null) services);
|
|
|
|
|
in [
|
|
|
|
|
systemd.packages =
|
|
|
|
|
let
|
|
|
|
|
servicesWithEnv = builtins.attrNames (lib.filterAttrs (_n: v: v.vault.environmentTemplate != null) services);
|
|
|
|
|
in
|
|
|
|
|
[
|
|
|
|
|
(pkgs.runCommand "env-services" { }
|
|
|
|
|
(''
|
|
|
|
|
mkdir -p $out/lib/systemd/system
|
|
|
|
|
''
|
|
|
|
|
+ (lib.concatMapStringsSep "\n" (service: ''
|
|
|
|
|
+ (lib.concatMapStringsSep "\n"
|
|
|
|
|
(service: ''
|
|
|
|
|
cat > $out/lib/systemd/system/${service}-envfile.service <<EOF
|
|
|
|
|
[Unit]
|
|
|
|
|
Before=${service}.service
|
|
|
|
|