Skip to content

Commit

Permalink
nixos/omnom: review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
eljamm committed Nov 21, 2024
1 parent 0a5c1db commit ceba0b0
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions nixos/modules/services/misc/omnom.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ in
{
options = {
services.omnom = {
enable = lib.mkEnableOption "the omnom webpage bookmarking and snapshotting service.";
debug = lib.mkEnableOption "the omnom debug mode.";
enable = lib.mkEnableOption "Omnom, a webpage bookmarking and snapshotting service";
debug = lib.mkEnableOption "debug mode";
package = lib.mkPackageOption pkgs "omnom" { };

dataDir = lib.mkOption {
type = lib.types.str;
default = "/var/lib/omnom";
description = "The directory where omnom stores its data files.";
description = "The directory where Omnom stores its data files.";
};

port = lib.mkOption {
type = lib.types.port;
default = 7331;
description = "The omnom service port.";
description = "The Omnom service port.";
};

openFirewall = lib.mkOption {
Expand All @@ -39,13 +39,13 @@ in
user = lib.mkOption {
type = lib.types.nonEmptyStr;
default = "omnom";
description = "The omnom service user.";
description = "The Omnom service user.";
};

group = lib.mkOption {
type = lib.types.nonEmptyStr;
default = "omnom";
description = "The omnom service group.";
description = "The Omnom service group.";
};

settings = lib.mkOption {
Expand All @@ -56,8 +56,8 @@ in
freeformType = settingsFormat.type;
options = {
app = {
debug = lib.mkEnableOption "the omnom debug mode.";
disable_signup = lib.mkEnableOption "restricting user creation.";
debug = lib.mkEnableOption "debug mode";
disable_signup = lib.mkEnableOption "restricting user creation";
results_per_page = lib.mkOption {
type = lib.types.int;
default = 20;
Expand All @@ -82,8 +82,11 @@ in
server = {
address = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1:7331";
default = "127.0.0.1:${toString cfg.port}";
description = "Server address.";
defaultText = lib.literalExpression ''
"127.0.0.1:''${config.services.omnom.port}"
'';
};
secure_cookie = lib.mkOption {
type = lib.types.bool;
Expand All @@ -109,7 +112,7 @@ in
};

config = lib.mkIf cfg.enable {
environment.etc."omnom/config.yml".source = pkgs.writers.writeYAML "omnom-config.yml" cfg.settings;
environment.etc."omnom/config.yml".source = settingsFormat.generate "omnom-config.yml" cfg.settings;

services.omnom.settings = {
app.debug = lib.mkDefault cfg.debug;
Expand All @@ -123,14 +126,11 @@ in
DynamicUser = isDynamicUser;
User = cfg.user;
Group = cfg.group;
ExecStart =
let
args = lib.cli.toGNUCommandLineShell { } {
config = "/etc/omnom/config.yml";
d = cfg.debug;
};
in
"${lib.getExe cfg.package} listen ${args}";
ExecStart = ''
${lib.getExe cfg.package} listen \
--config /etc/omnom/config.yml \
${lib.optionalString cfg.debug "-d"}
'';
StateDirectory = lib.mkIf (cfg.dataDir == "/var/lib/omnom") "omnom";
StateDirectoryMode = lib.mkIf (cfg.dataDir == "/var/lib/omnom") "0750";
WorkingDirectory = cfg.dataDir;
Expand All @@ -146,27 +146,31 @@ in
# These need to be present in the working directory
preStart = ''
for dir in templates; do
if [ ! -e "${cfg.dataDir}"/$dir ]; then
rsync -a --chmod=u=rwX,go=rX "${cfg.package}/share/$dir" "${cfg.dataDir}/"
fi
rsync -a --chmod=u=rwX,go=rX "${cfg.package}/share/$dir" "${cfg.dataDir}/"
done
'';
};

# If the service user is not dynamic, a normal group must exist
users.groups = lib.mkIf ((!isDynamicUser) && (cfg.group == "omnom")) { omnom = { }; };

systemd.tmpfiles.rules = lib.mkIf (cfg.dataDir != "/var/lib/omnom") (
[
"d ${cfg.dataDir} 0750 ${cfg.user} ${cfg.group} - -"
"d ${cfg.dataDir}/static 0750 ${cfg.user} ${cfg.group} - -"
"d ${cfg.dataDir}/static/data 0750 ${cfg.user} ${cfg.group} - -"
"d ${cfg.dataDir}/static/data/resources 0750 ${cfg.user} ${cfg.group} - -"
"d ${cfg.dataDir}/static/data/snapshots 0750 ${cfg.user} ${cfg.group} - -"
]
++ lib.optionals (cfg.settings.db.type == "sqlite") [
"f ${cfg.dataDir}/db.sqlite3 0644 ${cfg.user} ${cfg.group} - -"
]
systemd.tmpfiles.settings."10-omnom" = lib.mkIf (cfg.dataDir != "/var/lib/omnom") (
{
"${cfg.settings.storage.root}/resources"."d" = {
inherit (cfg) user group;
mode = "0750";
};
"${cfg.settings.storage.root}/snapshots"."d" = {
inherit (cfg) user group;
mode = "0750";
};
}
// lib.optionalAttrs (cfg.settings.db.type == "sqlite") {
"${cfg.dataDir}/db.sqlite3"."f" = {
inherit (cfg) user group;
mode = "0644";
};
}
);

networking.firewall = lib.mkIf cfg.openFirewall {
Expand Down

0 comments on commit ceba0b0

Please sign in to comment.