1
0
Fork 0

move template function into lib

This commit is contained in:
ibizaman 2024-02-09 20:56:26 -08:00 committed by Pierre Penninckx
parent 90075e220c
commit 059bfea86a
6 changed files with 25 additions and 65 deletions

13
lib/default.nix Normal file
View file

@ -0,0 +1,13 @@
{ lib }:
{
template = file: newPath: replacements:
let
templatePath = newPath + ".template";
sedPatterns = lib.strings.concatStringsSep " " (lib.attrsets.mapAttrsToList (from: to: "-e \"s|${from}|${to}|\"") replacements);
in
''
ln -fs ${file} ${templatePath}
rm ${newPath} || :
sed ${sedPatterns} ${templatePath} > ${newPath}
'';
}

View file

@ -4,22 +4,12 @@ let
cfg = config.shb.authelia; cfg = config.shb.authelia;
contracts = pkgs.callPackage ../contracts {}; contracts = pkgs.callPackage ../contracts {};
shblib = pkgs.callPackage ../../lib {};
fqdn = "${cfg.subdomain}.${cfg.domain}"; fqdn = "${cfg.subdomain}.${cfg.domain}";
fqdnWithPort = if isNull cfg.port then fqdn else "${fqdn}:${toString cfg.port}"; fqdnWithPort = if isNull cfg.port then fqdn else "${fqdn}:${toString cfg.port}";
autheliaCfg = config.services.authelia.instances.${fqdn}; autheliaCfg = config.services.authelia.instances.${fqdn};
template = file: newPath: replacements:
let
templatePath = newPath + ".template";
sedPatterns = lib.strings.concatStringsSep ";" (lib.attrsets.mapAttrsToList (from: to: "s|${from}|${to}|") replacements);
in
''
ln -fs ${file} ${templatePath}
rm ${newPath} || :
sed "${sedPatterns}" ${templatePath} > ${newPath}
'';
in in
{ {
options.shb.authelia = { options.shb.authelia = {
@ -307,7 +297,7 @@ in
replace = client: {"%SECRET_${client.id}%" = "$(cat ${toString client.secretFile})";}; replace = client: {"%SECRET_${client.id}%" = "$(cat ${toString client.secretFile})";};
replacements = lib.foldl (container: client: container // (replace client) ) {} clients; replacements = lib.foldl (container: client: container // (replace client) ) {} clients;
in in
template tmplFile "/var/lib/authelia-${fqdn}/oidc_clients.yaml" replacements; shblib.template tmplFile "/var/lib/authelia-${fqdn}/oidc_clients.yaml" replacements;
in in
lib.mkBefore (mkCfg cfg.oidcClients); lib.mkBefore (mkCfg cfg.oidcClients);

View file

@ -3,17 +3,7 @@
let let
cfg = config.shb.davfs; cfg = config.shb.davfs;
template = file: newPath: replacements: shblib = pkgs.callPackage ../../lib {};
let
templatePath = newPath + ".template";
sedPatterns = lib.strings.concatStringsSep " " (lib.attrsets.mapAttrsToList (from: to: "\"s|${from}|${to}|\"") replacements);
in
''
ln -fs ${file} ${templatePath}
rm ${newPath} || :
sed ${sedPatterns} ${templatePath} > ${newPath}
'';
in in
{ {
options.shb.davfs = { options.shb.davfs = {

View file

@ -4,6 +4,7 @@ let
cfg = config.shb.arr; cfg = config.shb.arr;
contracts = pkgs.callPackage ../contracts {}; contracts = pkgs.callPackage ../contracts {};
shblib = pkgs.callPackage ../../lib {};
apps = { apps = {
radarr = { radarr = {
@ -185,18 +186,6 @@ let
} // (c.moreOptions or {}); } // (c.moreOptions or {});
}; };
}); });
template = file: newPath: replacements:
let
templatePath = newPath + ".template";
sedPatterns = lib.strings.concatStringsSep " " (lib.attrsets.mapAttrsToList (from: to: "-e \"s|${from}|${to}|\"") replacements);
in
''
ln -fs ${file} ${templatePath}
rm ${newPath} || :
sed ${sedPatterns} ${templatePath} > ${newPath}
'';
in in
{ {
options.shb.arr = lib.listToAttrs (lib.mapAttrsToList appOption apps); options.shb.arr = lib.listToAttrs (lib.mapAttrsToList appOption apps);
@ -227,7 +216,7 @@ in
}; };
templatedSettings = (removeAttrs s [ "APIKeyFile" ]) // templatedfileSettings; templatedSettings = (removeAttrs s [ "APIKeyFile" ]) // templatedfileSettings;
t = template (apps.radarr.settingsFormat.generate " t = shblib.template (apps.radarr.settingsFormat.generate "
config.xml" templatedSettings) "${config.services.radarr.dataDir}/config.xml" ( config.xml" templatedSettings) "${config.services.radarr.dataDir}/config.xml" (
lib.optionalAttrs (!(isNull s.APIKeyFile)) { lib.optionalAttrs (!(isNull s.APIKeyFile)) {
"%APIKEY%" = "$(cat ${s.APIKeyFile})"; "%APIKEY%" = "$(cat ${s.APIKeyFile})";
@ -295,7 +284,7 @@ config.xml" templatedSettings) "${config.services.radarr.dataDir}/config.xml" (
}; };
templatedSettings = (removeAttrs s [ "APIKeyFile" "OmdbApiKeyFile" ]) // templatedfileSettings; templatedSettings = (removeAttrs s [ "APIKeyFile" "OmdbApiKeyFile" ]) // templatedfileSettings;
t = template (apps.jackett.settingsFormat.generate "jackett.json" templatedSettings) "${config.services.jackett.dataDir}/ServerConfig.json" ( t = shblib.template (apps.jackett.settingsFormat.generate "jackett.json" templatedSettings) "${config.services.jackett.dataDir}/ServerConfig.json" (
lib.optionalAttrs (!(isNull s.APIKeyFile)) { lib.optionalAttrs (!(isNull s.APIKeyFile)) {
"%APIKEY%" = "$(cat ${s.APIKeyFile})"; "%APIKEY%" = "$(cat ${s.APIKeyFile})";
} // lib.optionalAttrs (!(isNull s.OmdbApiKeyFile)) { } // lib.optionalAttrs (!(isNull s.OmdbApiKeyFile)) {

View file

@ -4,20 +4,9 @@ let
cfg = config.shb.jellyfin; cfg = config.shb.jellyfin;
contracts = pkgs.callPackage ../contracts {}; contracts = pkgs.callPackage ../contracts {};
shblib = pkgs.callPackage ../../lib {};
fqdn = "${cfg.subdomain}.${cfg.domain}"; fqdn = "${cfg.subdomain}.${cfg.domain}";
template = file: newPath: replacements:
let
templatePath = newPath + ".template";
sedPatterns = lib.strings.concatStringsSep " " (lib.attrsets.mapAttrsToList (from: to: "\"s|${from}|${to}|\"") replacements);
in
''
ln -fs ${file} ${templatePath}
rm ${newPath} || :
sed ${sedPatterns} ${templatePath} > ${newPath}
'';
in in
{ {
options.shb.jellyfin = { options.shb.jellyfin = {
@ -359,13 +348,13 @@ in
</BrandingOptions> </BrandingOptions>
''; '';
in in
template ldapConfig "/var/lib/jellyfin/plugins/configurations/LDAP-Auth.xml" { shblib.template ldapConfig "/var/lib/jellyfin/plugins/configurations/LDAP-Auth.xml" {
"%LDAP_PASSWORD%" = "$(cat ${cfg.ldapPasswordFile})"; "%LDAP_PASSWORD%" = "$(cat ${cfg.ldapPasswordFile})";
} }
+ template ssoConfig "/var/lib/jellyfin/plugins/configurations/SSO-Auth.xml" { + shblib.template ssoConfig "/var/lib/jellyfin/plugins/configurations/SSO-Auth.xml" {
"%SSO_SECRET%" = "$(cat ${cfg.ssoSecretFile})"; "%SSO_SECRET%" = "$(cat ${cfg.ssoSecretFile})";
} }
+ template brandingConfig "/var/lib/jellyfin/config/branding.xml" {"%a%" = "%a%";}; + shblib.template brandingConfig "/var/lib/jellyfin/config/branding.xml" {"%a%" = "%a%";};
shb.authelia.oidcClients = [ shb.authelia.oidcClients = [
{ {

View file

@ -4,20 +4,9 @@ let
cfg = config.shb.vaultwarden; cfg = config.shb.vaultwarden;
contracts = pkgs.callPackage ../contracts {}; contracts = pkgs.callPackage ../contracts {};
shblib = pkgs.callPackage ../../lib {};
fqdn = "${cfg.subdomain}.${cfg.domain}"; fqdn = "${cfg.subdomain}.${cfg.domain}";
template = file: newPath: replacements:
let
templatePath = newPath + ".template";
sedPatterns = lib.strings.concatStringsSep " " (lib.attrsets.mapAttrsToList (from: to: "-e \"s|${from}|${to}|\"") replacements);
in
''
ln -fs ${file} ${templatePath}
rm ${newPath} || :
sed ${sedPatterns} ${templatePath} > ${newPath}
'';
in in
{ {
options.shb.vaultwarden = { options.shb.vaultwarden = {
@ -165,7 +154,7 @@ in
SMTP_PASSWORD=%SMTP_PASSWORD% SMTP_PASSWORD=%SMTP_PASSWORD%
''; '';
in in
template envFile "/var/lib/bitwarden_rs/vaultwarden.env" { shblib.template envFile "/var/lib/bitwarden_rs/vaultwarden.env" {
"%DB_PASSWORD%" = "$(cat ${cfg.databasePasswordFile})"; "%DB_PASSWORD%" = "$(cat ${cfg.databasePasswordFile})";
"%SMTP_PASSWORD%" = "$(cat ${cfg.smtp.passwordFile})"; "%SMTP_PASSWORD%" = "$(cat ${cfg.smtp.passwordFile})";
}; };