Skip to content

Commit

Permalink
refactor(modules/hatsu): update
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaa committed Sep 30, 2024
1 parent 7318c99 commit d3db266
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 58 deletions.
6 changes: 4 additions & 2 deletions modules/services/hatsu.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ the minimum configuration to start hatsu server would look like this:
{
services.hatsu = {
enable = true;
domain = "hatsu.local";
primaryAccount = "example.com";
settings = {
HATSU_DOMAIN = "hatsu.local";
HATSU_PRIMARY_ACCOUNT = "example.com";
};
};
}
```
Expand Down
116 changes: 60 additions & 56 deletions modules/services/hatsu.nix
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
{ lib, pkgs, config, ... }:
{ lib
, pkgs
, config
, ...
}:
let
cfg = config.services.hatsu;
env = {
HATSU_DATABASE_URL = cfg.database.url;
HATSU_DOMAIN = cfg.domain;
HATSU_LISTEN_HOST = cfg.host;
HATSU_LISTEN_PORT = toString cfg.port;
HATSU_PRIMARY_ACCOUNT = cfg.primaryAccount;
} // (
lib.mapAttrs (_: toString) cfg.settings
);
in
{
meta.doc = ./hatsu.md;
Expand All @@ -26,39 +21,49 @@ in
description = "Data directory for hatsu.";
};

database = {
url = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = "sqlite://${cfg.dataDir}/hatsu.sqlite?mode=rwc";
example = "postgres://username:password@host/database";
description = "Database URL.";
};
};

domain = lib.mkOption {
type = lib.types.str;
description = "The domain name of your instance (eg 'hatsu.local').";
};
settings = lib.mkOption {
type = lib.types.submodule {
freeformType =
with lib.types;
attrsOf (
nullOr (oneOf [
str
path
package
])
);
options = {
HATSU_DATABASE_URL = lib.mkOption {
type = lib.types.str;
default = "sqlite://${cfg.dataDir}/hatsu.sqlite?mode=rwc";
defaultText = "sqlite://\${config.services.hatsu.dataDir}/hatsu.sqlite?mode=rwc";
example = "postgres://username:password@host/database";
description = "Database URL.";
};

host = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1";
description = "Host where hatsu should listen for incoming requests.";
};
HATSU_DOMAIN = lib.mkOption {
type = lib.types.str;
description = "The domain name of your instance (eg 'hatsu.local').";
};

port = lib.mkOption {
type = lib.types.port;
default = 3939;
description = "Port where hatsu should listen for incoming requests.";
};
HATSU_LISTEN_HOST = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1";
description = "Host where hatsu should listen for incoming requests.";
};

primaryAccount = lib.mkOption {
type = lib.types.str;
description = "The primary account of your instance (eg 'example.com').";
};
HATSU_LISTEN_PORT = lib.mkOption {
type = lib.types.port;
default = 3939;
description = "Port where hatsu should listen for incoming requests.";
};

settings = lib.mkOption {
type = lib.types.attrsOf lib.types.str;
HATSU_PRIMARY_ACCOUNT = lib.mkOption {
type = lib.types.str;
description = "The primary account of your instance (eg 'example.com').";
};
};
};
default = { };
description = ''
See [Environments](https://hatsu.cli.rs/admins/environments.html) for available settings.
Expand All @@ -70,27 +75,26 @@ in
};
};

config = lib.mkIf cfg.enable
{
systemd.services.hatsu = {
environment = env;
config = lib.mkIf cfg.enable {
systemd.services.hatsu = {
environment = cfg.settings;

description = "Hatsu server";
documentation = [ "https://hatsu.cli.rs/" ];
description = "Hatsu server";
documentation = [ "https://hatsu.cli.rs/" ];

after = [ "network-online.target" ];
wants = [ "network-online.target" ];
after = [ "network-online.target" ];
wants = [ "network-online.target" ];

wantedBy = [ "multi-user.target" ];
wantedBy = [ "multi-user.target" ];

serviceConfig = {
DynamicUser = true;
ExecStart = "${lib.getExe cfg.package}";
Restart = "on-failure";
StateDirectory = lib.mkIf (cfg.dataDir == "/var/lib/hatsu") "hatsu";
Type = "simple";
WorkingDirectory = cfg.dataDir;
};
serviceConfig = {
DynamicUser = true;
ExecStart = "${lib.getExe cfg.package}";
Restart = "on-failure";
StateDirectory = lib.mkIf (cfg.dataDir == "/var/lib/hatsu") "hatsu";
Type = "simple";
WorkingDirectory = cfg.dataDir;
};
};
};
}

0 comments on commit d3db266

Please sign in to comment.