From fc7889bdf10f04bddd815f6004fbcc039d71bc29 Mon Sep 17 00:00:00 2001 From: Andre Date: Sun, 8 Sep 2024 23:47:53 -0400 Subject: [PATCH] Services: function-ize domain parsing --- modules/common.nix | 19 +++++++++++++++++-- modules/services/airsonic.nix | 9 +-------- modules/services/deluge.nix | 16 +++++++--------- modules/services/forgejo.nix | 9 +-------- modules/services/home-assistant.nix | 16 +++++++--------- modules/services/jellyfin.nix | 10 ++-------- modules/services/netdata.nix | 9 +-------- 7 files changed, 36 insertions(+), 52 deletions(-) diff --git a/modules/common.nix b/modules/common.nix index ef5feb2..e7ef71d 100644 --- a/modules/common.nix +++ b/modules/common.nix @@ -1,8 +1,9 @@ # Modules common to all systems { - pkgs, config, inputs, + lib, + pkgs, ... }: @@ -15,13 +16,27 @@ zellij # Terminal multiplexer ]; - # Allow packages from the unstable repo by using 'pkgs.unstable' nixpkgs.overlays = [ (final: _prev: { + # Allow packages from the unstable repo by using 'pkgs.unstable' unstable = import inputs.nixpkgs-unstable { system = final.system; config.allowUnfree = true; }; + + # Define custom functions using 'pkgs.util' + util = { + # Parses the domain from a URL + getDomainFromURL = + url: + let + parsedURL = (lib.strings.splitString "." url); + in + builtins.concatStringsSep "." [ + (builtins.elemAt parsedURL 1) + (builtins.elemAt parsedURL 2) + ]; + }; }) ]; diff --git a/modules/services/airsonic.nix b/modules/services/airsonic.nix index fd45802..31af51f 100644 --- a/modules/services/airsonic.nix +++ b/modules/services/airsonic.nix @@ -31,14 +31,7 @@ in services = { nginx.virtualHosts."${cfg.url}" = { - useACMEHost = - let - parsedURL = (lib.strings.splitString "." cfg.url); - in - builtins.concatStringsSep "." [ - (builtins.elemAt parsedURL 1) - (builtins.elemAt parsedURL 2) - ]; + useACMEHost = pkgs.util.getDomainFromURL cfg.url; forceSSL = true; locations."/" = { proxyPass = "http://127.0.0.1:4040"; diff --git a/modules/services/deluge.nix b/modules/services/deluge.nix index 26555b8..03e2043 100644 --- a/modules/services/deluge.nix +++ b/modules/services/deluge.nix @@ -1,4 +1,9 @@ -{ config, lib, ... }: +{ + config, + lib, + pkgs, + ... +}: let cfg = config.aux.system.services.deluge; @@ -25,14 +30,7 @@ in config = lib.mkIf cfg.enable { services = { nginx.virtualHosts."${cfg.url}" = { - useACMEHost = - let - parsedURL = (lib.strings.splitString "." cfg.url); - in - builtins.concatStringsSep "." [ - (builtins.elemAt parsedURL 1) - (builtins.elemAt parsedURL 2) - ]; + useACMEHost = pkgs.util.getDomainFromURL cfg.url; forceSSL = true; locations."/" = { proxyPass = "http://127.0.0.1:8112"; diff --git a/modules/services/forgejo.nix b/modules/services/forgejo.nix index 03b2cb5..2cffb0f 100644 --- a/modules/services/forgejo.nix +++ b/modules/services/forgejo.nix @@ -72,14 +72,7 @@ in } // lib.optionalAttrs (cfg.home != null) { stateDir = cfg.home; }; nginx.virtualHosts."${cfg.url}" = { - useACMEHost = - let - parsedURL = (lib.strings.splitString "." cfg.url); - in - builtins.concatStringsSep "." [ - (builtins.elemAt parsedURL 1) - (builtins.elemAt parsedURL 2) - ]; + useACMEHost = pkgs.util.getDomainFromURL cfg.url; forceSSL = true; locations."/" = { proxyPass = "http://127.0.0.1:3000"; diff --git a/modules/services/home-assistant.nix b/modules/services/home-assistant.nix index 86ade25..03cd5e8 100644 --- a/modules/services/home-assistant.nix +++ b/modules/services/home-assistant.nix @@ -1,4 +1,9 @@ -{ config, lib, ... }: +{ + config, + lib, + pkgs, + ... +}: let cfg = config.aux.system.services.home-assistant; @@ -49,14 +54,7 @@ in }; }; nginx.virtualHosts."${cfg.url}" = { - useACMEHost = - let - parsedURL = (lib.strings.splitString "." cfg.url); - in - builtins.concatStringsSep "." [ - (builtins.elemAt parsedURL 1) - (builtins.elemAt parsedURL 2) - ]; + useACMEHost = pkgs.util.getDomainFromURL cfg.url; forceSSL = true; locations."/" = { proxyPass = "http://[::1]:8123"; diff --git a/modules/services/jellyfin.nix b/modules/services/jellyfin.nix index 672ca4f..718229e 100644 --- a/modules/services/jellyfin.nix +++ b/modules/services/jellyfin.nix @@ -2,6 +2,7 @@ pkgs, config, lib, + self, ... }: let @@ -34,14 +35,7 @@ in services = { nginx.virtualHosts."${cfg.url}" = { - useACMEHost = - let - parsedURL = (lib.strings.splitString "." cfg.url); - in - builtins.concatStringsSep "." [ - (builtins.elemAt parsedURL 1) - (builtins.elemAt parsedURL 2) - ]; + useACMEHost = pkgs.util.getDomainFromURL cfg.url; forceSSL = true; locations."/" = { proxyPass = "http://127.0.0.1:8096"; diff --git a/modules/services/netdata.nix b/modules/services/netdata.nix index 9d1997c..f35ab60 100644 --- a/modules/services/netdata.nix +++ b/modules/services/netdata.nix @@ -50,14 +50,7 @@ in (lib.mkIf (cfg.enable && cfg.type == "parent") { services = { nginx.virtualHosts."${cfg.url}" = { - useACMEHost = - let - parsedURL = (lib.strings.splitString "." cfg.url); - in - builtins.concatStringsSep "." [ - (builtins.elemAt parsedURL 1) - (builtins.elemAt parsedURL 2) - ]; + useACMEHost = pkgs.util.getDomainFromURL cfg.url; forceSSL = true; basicAuth = { "${cfg.auth.user}" = cfg.auth.password;