diff --git a/flake.nix b/flake.nix index 2e4bcb4..1593c8b 100644 --- a/flake.nix +++ b/flake.nix @@ -42,11 +42,14 @@ let importFiles = files: map (m: import m { + inherit pkgs; inherit (pkgs) lib; }) files; mergeTests = pkgs.lib.lists.foldl pkgs.lib.trivial.mergeAttrs {}; in mergeTests (importFiles [ + ./test/modules/arr.nix + ./test/modules/davfs.nix ./test/modules/postgresql.nix ]); }; diff --git a/modules/arr.nix b/modules/arr.nix index ae4a1f8..3b3afed 100644 --- a/modules/arr.nix +++ b/modules/arr.nix @@ -24,6 +24,7 @@ let settingsFormat = pkgs.formats.json {}; moreOptions = { settings = lib.mkOption { + default = {}; type = lib.types.submodule { freeformType = apps.jackett.settingsFormat.type; options = { @@ -66,6 +67,7 @@ let appOption = name: c: lib.nameValuePair name (lib.mkOption { description = "Configuration for ${name}"; + default = {}; type = lib.types.submodule { options = { enable = lib.mkEnableOption "selfhostblocks.${name}"; @@ -95,7 +97,8 @@ let }; oidcEndpoint = lib.mkOption { - type = lib.types.str; + type = lib.types.nullOr lib.types.str; + default = null; description = "OIDC endpoint for SSO"; example = "https://authelia.example.com"; }; @@ -125,7 +128,7 @@ in enable = true; dataDir = "/var/lib/radarr"; }; - users.users.radarr = { + users.users.radarr = lib.mkIf cfg.radarr.enable { extraGroups = [ "media" ]; }; @@ -134,7 +137,7 @@ in enable = true; dataDir = "/var/lib/sonarr"; }; - users.users.sonarr = { + users.users.sonarr = lib.mkIf cfg.sonarr.enable { extraGroups = [ "media" ]; }; @@ -142,13 +145,16 @@ in enable = true; listenPort = cfg.bazarr.port; }; + users.users.bazarr = lib.mkIf cfg.bazarr.enable { + extraGroups = [ "media" ]; + }; # Listens on port 8787 services.readarr = lib.mkIf cfg.readarr.enable { enable = true; dataDir = "/var/lib/readarr"; }; - users.users.readarr = { + users.users.readarr = lib.mkIf cfg.readarr.enable { extraGroups = [ "media" ]; }; @@ -157,7 +163,7 @@ in enable = true; dataDir = "/var/lib/lidarr"; }; - users.users.lidarr = { + users.users.lidarr = lib.mkIf cfg.lidarr.enable { extraGroups = [ "media" ]; }; @@ -166,12 +172,12 @@ in enable = true; dataDir = "/var/lib/jackett"; }; - shb.arr.jackett.settings = { + shb.arr.jackett.settings = lib.mkIf cfg.jackett.enable { Port = config.shb.arr.jackett.port; AllowExternal = "false"; UpdateDisabled = "true"; }; - users.users.jackett = { + users.users.jackett = lib.mkIf cfg.jackett.enable { extraGroups = [ "media" ]; }; systemd.services.jackett.preStart = @@ -184,14 +190,16 @@ in OmdbApiKey = "%OMDBAPIKEY%"; }; templatedSettings = (removeAttrs s [ "APIKeyFile" "OmdbApiKeyFile" ]) // templatedfileSettings; - in - template (apps.jackett.settingsFormat.generate "jackett.json" templatedSettings) "${config.services.jackett.dataDir}/ServerConfig.json" ( + + t = template (apps.jackett.settingsFormat.generate "jackett.json" templatedSettings) "${config.services.jackett.dataDir}/ServerConfig.json" ( lib.optionalAttrs (!(isNull s.APIKeyFile)) { "%APIKEY%" = "$(cat ${s.APIKeyFile})"; } // lib.optionalAttrs (!(isNull s.OmdbApiKeyFile)) { "%OMDBAPIKEY%" = "$(cat ${s.OmdbApiKeyFile})"; } ); + in + lib.mkIf cfg.jackett.enable t; shb.nginx.autheliaProtect = let @@ -199,7 +207,7 @@ in let c = cfg.${name}; in - { + lib.mkIf (c.oidcEndpoint != null) { inherit (c) subdomain domain oidcEndpoint; upstream = "http://127.0.0.1:${toString c.port}"; autheliaRules = [ @@ -235,7 +243,7 @@ in in lib.mkMerge (lib.mapAttrsToList backupConfig apps); } - ] ++ map (name: { + ] ++ map (name: lib.mkIf cfg.${name}.enable { systemd.tmpfiles.rules = lib.mkIf (lib.hasAttr "dataDir" config.services.${name}) [ "d '${config.services.${name}.dataDir}' 0750 ${config.services.${name}.user} ${config.services.${name}.group} - -" ]; diff --git a/test/modules/arr.nix b/test/modules/arr.nix new file mode 100644 index 0000000..6392780 --- /dev/null +++ b/test/modules/arr.nix @@ -0,0 +1,52 @@ +{ pkgs, lib, ... }: +let + anyOpt = default: lib.mkOption { + type = lib.types.anything; + inherit default; + }; + + testConfig = m: + let + cfg = (lib.evalModules { + specialArgs = { inherit pkgs; }; + modules = [ + { + options = { + systemd = anyOpt {}; + shb.backup = anyOpt {}; + shb.nginx = anyOpt {}; + users = anyOpt {}; + services.bazarr = anyOpt {}; + services.jackett = anyOpt {}; + services.lidarr = anyOpt {}; + services.radarr = anyOpt {}; + services.readarr = anyOpt {}; + services.sonarr = anyOpt {}; + }; + } + ../../modules/arr.nix + m + ]; + }).config; + in { + inherit (cfg) systemd services users; + shb = { inherit (cfg.shb) backup nginx; }; + }; +in +{ + testArrNoOptions = { + expected = { + systemd.services.jackett = {}; + shb.backup = {}; + shb.nginx.autheliaProtect = []; + users.users = {}; + services.bazarr = {}; + services.jackett = {}; + services.lidarr = {}; + services.radarr = {}; + services.readarr = {}; + services.sonarr = {}; + }; + expr = testConfig {}; + }; +} diff --git a/test/modules/davfs.nix b/test/modules/davfs.nix new file mode 100644 index 0000000..bcbb9e8 --- /dev/null +++ b/test/modules/davfs.nix @@ -0,0 +1,35 @@ +{ pkgs, lib, ... }: +let + anyOpt = default: lib.mkOption { + type = lib.types.anything; + inherit default; + }; + + testConfig = m: + let + cfg = (lib.evalModules { + specialArgs = { inherit pkgs; }; + modules = [ + { + options = { + systemd = anyOpt {}; + services = anyOpt {}; + }; + } + ../../modules/davfs.nix + m + ]; + }).config; + in { + inherit (cfg) systemd services; + }; +in +{ + testDavfsNoOptions = { + expected = { + services.davfs2.enable = false; + systemd.mounts = []; + }; + expr = testConfig {}; + }; +} diff --git a/test/modules/postgresql.nix b/test/modules/postgresql.nix index 522d70c..5adb6ad 100644 --- a/test/modules/postgresql.nix +++ b/test/modules/postgresql.nix @@ -1,4 +1,4 @@ -{ lib }: +{ lib, ... }: let anyOpt = default: lib.mkOption { type = lib.types.anything; @@ -274,10 +274,11 @@ in port = 1234; authentication = '' #type database DBuser origin-address auth-method + local all all peer # ipv4 - host all all 127.0.0.1/32 trust + host all all 127.0.0.1/32 trust # ipv6 - host all all ::1/128 trust + host all all ::1/128 trust ''; }; systemd.services.postgresql.postStart = "";