Refactored NixOS module

* Admin account configuration
* Proxy server configuration
This commit is contained in:
Andrew Bryant 2024-10-16 09:30:39 -04:00
parent 6b016669ec
commit 25b54cd352

View File

@ -147,29 +147,38 @@ in
''; '';
}; };
adminEmail = lib.mkOption { adminAccount = {
createAdmin = lib.mkOption {
type = lib.types.bool;
default = true;
description = ''
When set to `true`, an admin account will be created for Invoice Ninja. If set to `false`
Invoice Ninja will run a setup wizard on first use.
'';
};
email = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = "example@email.com"; default = "example@email.com";
description = "Email address of the first (admin) account for this Invoice Ninja installation"; description = "Email address of the first (admin) account for this Invoice Ninja installation";
}; };
password = lib.mkOption {
adminPassword = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = "example"; default = "example";
description = "Password of the first (admin) account for this Invoice Ninja installation"; description = "Password of the first (admin) account for this Invoice Ninja installation";
}; };
};
database = { database = {
createLocally = lib.mkOption { createLocally = lib.mkOption {
type = lib.types.bool; type = lib.types.bool;
default = true; default = true;
description = "A local database using UNIX socket authentication"; description = "Installs a local MariaDB server to use with Invoice Ninja.";
}; };
name = lib.mkOption { name = lib.mkOption {
type = lib.types.str; type = lib.types.str;
default = "invoiceninja"; default = "invoiceninja";
description = "Database name for Invoice Ninja."; description = "Name of the database to use for Invoice Ninja.";
}; };
}; };
@ -202,15 +211,17 @@ in
''; '';
}; };
webserver = { proxy = {
caddy = { server = lib.mkOption {
enable = lib.mkOption { type = lib.types.enum [ "caddy" "nginx" "none" ];
type = lib.types.bool; default = "nginx";
default = false; example = "caddy";
description = "Whether to enable the Caddy server to serve Invoice Ninja."; description = ''
Choose the proxy server to serve Invoice Ninja. Setting this to
`none` results in no proxy server being installed.
'';
}; };
caddyConfig = lib.mkOption {
config = lib.mkOption {
type = lib.types.submodule ( type = lib.types.submodule (
(import (modulesPath + "/services/web-servers/caddy/vhost-options.nix") { cfg = config.services.caddy; }) { (import (modulesPath + "/services/web-servers/caddy/vhost-options.nix") { cfg = config.services.caddy; }) {
inherit lib; config = cfg; name = (if (cfg.hostName == "localhost") then ":80" else cfg.hostName); inherit lib; config = cfg; name = (if (cfg.hostName == "localhost") then ":80" else cfg.hostName);
@ -222,16 +233,7 @@ in
Set to `{ }` to use the default configuration Set to `{ }` to use the default configuration
''; '';
}; };
}; nginxConfig = lib.mkOption {
nginx = {
enable = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Whether to enable Nginx server to serve Invoice Ninja.";
};
config = lib.mkOption {
type = lib.types.submodule ( type = lib.types.submodule (
(import (modulesPath + "/services/web-servers/nginx/vhost-options.nix") { inherit config lib; }) (import (modulesPath + "/services/web-servers/nginx/vhost-options.nix") { inherit config lib; })
); );
@ -243,21 +245,8 @@ in
}; };
}; };
}; };
};
config = lib.mkIf cfg.enable { config = lib.mkIf cfg.enable {
# FIXME Caddy and Nginx should be mutually exclusive
assertions = [
{
assertion = ((cfg.webserver.nginx.enable -> !cfg.webserver.caddy.enable)
&& (cfg.webserver.caddy.enable -> !cfg.webserver.nginx.enable));
message = ''
Both Nginx and Caddy webservers cannot be enable together. Check your configuration
and ensure you only enabled one.
'';
}
];
users.users.invoiceninja = lib.mkIf (cfg.user == "invoiceninja") { users.users.invoiceninja = lib.mkIf (cfg.user == "invoiceninja") {
isSystemUser = true; isSystemUser = true;
home = cfg.dataDir; home = cfg.dataDir;
@ -286,7 +275,7 @@ in
chromium = lib.lists.findSingle (x: x == pkgs.chromium) "none" "multiple" extraPrograms; chromium = lib.lists.findSingle (x: x == pkgs.chromium) "none" "multiple" extraPrograms;
in in
lib.mkMerge [ lib.mkMerge [
(rec { ({
APP_NAME = lib.mkDefault "\"Invoice Ninja\""; APP_NAME = lib.mkDefault "\"Invoice Ninja\"";
APP_ENV = lib.mkDefault "production"; APP_ENV = lib.mkDefault "production";
APP_DEBUG = lib.mkDefault false; APP_DEBUG = lib.mkDefault false;
@ -347,9 +336,9 @@ in
''; '';
}; };
users.users."${config.services.nginx.user}" = lib.mkIf (cfg.webserver.nginx.enable == true) { extraGroups = [ cfg.group ]; }; users.users."${config.services.nginx.user}" = lib.mkIf (cfg.proxy.server == "nginx") { extraGroups = [ cfg.group ]; };
services.nginx = lib.mkIf (cfg.webserver.nginx.enable == true) { services.nginx = lib.mkIf (cfg.proxy.server == "nginx") {
inherit (cfg.webserver.nginx) enable; enable = true;
recommendedTlsSettings = true; recommendedTlsSettings = true;
recommendedGzipSettings = true; recommendedGzipSettings = true;
@ -359,7 +348,7 @@ in
clientMaxBodySize = cfg.maxUploadSize; clientMaxBodySize = cfg.maxUploadSize;
virtualHosts."${cfg.hostName}" = lib.mkMerge [ virtualHosts."${cfg.hostName}" = lib.mkMerge [
cfg.webserver.nginx.config cfg.proxy.nginxConfig
{ {
root = lib.mkForce "${invoice-ninja}/public"; root = lib.mkForce "${invoice-ninja}/public";
locations = { locations = {
@ -387,16 +376,16 @@ in
]; ];
}; };
users.users."${config.services.caddy.user}" = lib.mkIf (cfg.webserver.caddy.enable == true) { extraGroups = [ cfg.group ]; }; users.users."${config.services.caddy.user}" = lib.mkIf (cfg.proxy.server == "caddy") { extraGroups = [ cfg.group ]; };
services.caddy = lib.mkIf (cfg.webserver.caddy.enable == true) { services.caddy = lib.mkIf (cfg.proxy.server == "caddy") {
inherit (cfg.webserver.caddy) enable; enable = true;
globalConfig = lib.mkIf (cfg.hostName == "localhost") '' globalConfig = lib.mkIf (cfg.hostName == "localhost") ''
auto_https disable_redirects auto_https disable_redirects
''; '';
virtualHosts."${cfg.hostName}" = lib.mkMerge [ virtualHosts."${cfg.hostName}" = lib.mkMerge [
cfg.webserver.caddy.config cfg.proxy.caddyConfig
{ {
extraConfig = '' extraConfig = ''
encode zstd gzip encode zstd gzip
@ -487,8 +476,8 @@ in
[[ ! -f ${cfg.dataDir}/.db-seeded ]] && invoice-ninja-manage db:seed --force && touch ${cfg.dataDir}/.db-seeded [[ ! -f ${cfg.dataDir}/.db-seeded ]] && invoice-ninja-manage db:seed --force && touch ${cfg.dataDir}/.db-seeded
# Create Invoice Ninja admin account # Create Invoice Ninja admin account
[[ ! -f ${cfg.dataDir}/.admin-created ]] \ [[ (! -f ${cfg.dataDir}/.admin-created) && (${if cfg.adminAccount.createAdmin then "true" else "false"} == "true") ]] \
&& invoice-ninja-manage ninja:create-account --email=${cfg.adminEmail} --password=${cfg.adminPassword} \ && invoice-ninja-manage ninja:create-account --email=${cfg.adminAccount.email} --password=${cfg.adminAccount.password} \
&& touch ${cfg.dataDir}/.admin-created && touch ${cfg.dataDir}/.admin-created
invoice-ninja-manage route:cache invoice-ninja-manage route:cache