1
0
Fork 0
puter/modules/filebrowser.nix

169 lines
4.6 KiB
Nix
Raw Normal View History

2025-05-19 17:43:46 +02:00
{
config,
pkgs,
lib,
2025-05-20 11:34:35 +02:00
utils,
2025-05-19 17:43:46 +02:00
...
}:
let
cfg = config.services.filebrowser;
inherit (lib) types;
format = pkgs.formats.json { };
in
{
options = {
services.filebrowser = {
enable = lib.mkEnableOption "FileBrowser";
package = lib.mkPackageOption pkgs "filebrowser" { };
openFirewall = lib.mkOption {
default = false;
description = ''
Whether to automatically open the ports for FileBrowser in the firewall.
'';
type = types.bool;
};
2025-05-19 17:58:10 +02:00
stateDir = lib.mkOption {
2025-05-19 22:25:14 +02:00
default = "filebrowser";
2025-05-19 17:43:46 +02:00
description = ''
2025-05-19 22:25:14 +02:00
The directory below `/var/lib` where FileBrowser stores its state.
2025-05-19 17:43:46 +02:00
'';
2025-05-19 22:25:14 +02:00
type = types.str;
};
cacheDir = lib.mkOption {
2025-05-20 11:30:53 +02:00
default = "filebrowser";
2025-05-19 22:25:14 +02:00
description = ''
The directory below `/var/cache` where FileBrowser stores its cache.
'';
type = types.nullOr types.str;
2025-05-19 17:43:46 +02:00
};
settings = lib.mkOption {
default = { };
description = ''
Settings for FileBrowser.
Refer to <https://filebrowser.org/cli/filebrowser#options> for all supported values.
'';
type = types.submodule {
freeformType = format.type;
2025-05-20 11:30:53 +02:00
2025-05-19 17:43:46 +02:00
options = {
address = lib.mkOption {
default = "localhost";
description = ''
The address to listen on.
'';
type = types.str;
};
port = lib.mkOption {
default = 8080;
description = ''
The port to listen on.
'';
type = types.port;
};
root = lib.mkOption {
2025-05-19 22:25:14 +02:00
default = "/var/lib/${cfg.stateDir}/data";
2025-05-19 17:58:10 +02:00
defaultText = lib.literalExpression ''
2025-05-19 22:25:14 +02:00
"/var/lib/''${config.services.filebrowser.stateDir}/data"
2025-05-19 17:58:10 +02:00
'';
2025-05-19 17:43:46 +02:00
description = ''
2025-05-19 17:58:10 +02:00
The directory where FileBrowser stores files.
2025-05-19 17:43:46 +02:00
'';
type = types.path;
};
database = lib.mkOption {
2025-05-19 22:25:14 +02:00
default = "/var/lib/${cfg.stateDir}/database.db";
2025-05-19 17:43:46 +02:00
defaultText = lib.literalExpression ''
2025-05-19 22:25:14 +02:00
"/var/lib/''${config.services.filebrowser.stateDir}/database.db"
2025-05-19 17:43:46 +02:00
'';
description = ''
2025-05-19 17:58:10 +02:00
The path to FileBrowser's Bolt database.
2025-05-19 17:43:46 +02:00
'';
type = types.path;
};
cache-dir = lib.mkOption {
2025-05-20 11:47:25 +02:00
default = if cfg.cacheDir != null then "/var/cache/${cfg.cacheDir}" else null;
2025-05-19 17:58:10 +02:00
defaultText = lib.literalExpression ''
2025-05-20 11:47:25 +02:00
if config.services.filebrowser.cacheDir != null then "/var/cache/''${config.services.filebrowser.cacheDir}" else null
2025-05-19 17:58:10 +02:00
'';
2025-05-19 17:43:46 +02:00
description = ''
The directory where FileBrowser stores its cache.
'';
type = types.nullOr types.path;
2025-05-19 22:25:14 +02:00
readOnly = true;
2025-05-19 17:43:46 +02:00
};
};
};
};
};
};
config = lib.mkIf cfg.enable {
systemd = {
services.filebrowser = {
after = [ "network.target" ];
description = "FileBrowser";
wantedBy = [ "multi-user.target" ];
serviceConfig = {
2025-05-20 11:34:35 +02:00
ExecStart =
let
args = [
(lib.getExe cfg.package)
"--config"
(format.generate "config.json" cfg.settings)
];
in
utils.escapeSystemdExecArgs args;
2025-05-19 17:58:10 +02:00
StateDirectory = cfg.stateDir;
2025-05-19 22:25:14 +02:00
CacheDirectory = lib.mkIf (cfg.cacheDir != null) cfg.cacheDir;
2025-05-20 11:34:35 +02:00
WorkingDirectory = cfg.settings.root;
2025-05-19 17:43:46 +02:00
2025-05-19 22:25:14 +02:00
DynamicUser = true;
2025-05-19 17:43:46 +02:00
NoNewPrivileges = true;
PrivateDevices = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectControlGroups = true;
MemoryDenyWriteExecute = true;
LockPersonality = true;
RestrictAddressFamilies = [
"AF_UNIX"
"AF_INET"
"AF_INET6"
];
DevicePolicy = "closed";
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
};
};
2025-05-20 11:30:53 +02:00
tmpfiles.settings.filebrowser =
lib.genAttrs
[
cfg.settings.root
(builtins.dirOf cfg.settings.database)
]
(_: {
d.mode = "0700";
});
2025-05-19 17:43:46 +02:00
};
2025-05-19 22:25:14 +02:00
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.settings.port ];
2025-05-19 17:43:46 +02:00
};
meta.maintainers = [
lib.maintainers.lukaswrz
];
}