2019-10-09 00:55:35 +02:00
|
|
|
{ config, pkgs, lib, ...}:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.seafile-server;
|
2020-11-30 02:21:10 +01:00
|
|
|
seafileConfigFile = pkgs.writeText "seafile.conf"
|
|
|
|
(generators.toINI {} cfg.seafileSettings);
|
2019-10-09 00:55:35 +02:00
|
|
|
# fix permissions at start
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.seafile-server = {
|
|
|
|
enable = mkEnableOption "Seafile server";
|
|
|
|
storagePath = mkOption {
|
|
|
|
type = types.path;
|
|
|
|
default = "/srv/seafile";
|
|
|
|
description = "where to store uploaded file data";
|
|
|
|
};
|
2020-11-30 02:21:10 +01:00
|
|
|
seafileSettings = mkOption {
|
|
|
|
type = with types; attrsOf (attrsOf (oneOf [ bool int str ]));
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
all possible seafile.conf settings
|
|
|
|
'';
|
|
|
|
};
|
2019-10-09 00:55:35 +02:00
|
|
|
autorun = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = "enable the seafile-server service to get started automatically";
|
|
|
|
};
|
|
|
|
db = {
|
|
|
|
type = mkOption {
|
|
|
|
type = types.enum ["sqlite" "mysql"];
|
|
|
|
default = "sqlite";
|
|
|
|
description = "database backend type";
|
|
|
|
};
|
|
|
|
user = mkOption {
|
2019-10-17 14:45:07 +02:00
|
|
|
type = types.nullOr types.str;
|
2019-10-09 00:55:35 +02:00
|
|
|
default = "seafile";
|
|
|
|
description = "Database user name. Not required for sqlite.";
|
|
|
|
};
|
|
|
|
dbname = mkOption {
|
2019-10-17 14:45:07 +02:00
|
|
|
type = types.nullOr types.str;
|
2019-10-09 00:55:35 +02:00
|
|
|
default = "seafile";
|
|
|
|
description = "Database name. Not required for sqlite.";
|
|
|
|
};
|
2020-11-30 02:21:10 +01:00
|
|
|
passwordFile = mkOption {
|
2019-10-09 00:55:35 +02:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
The full path to a file that contains the database password.
|
2020-11-30 02:21:10 +01:00
|
|
|
Not required for sqlite.
|
2019-10-09 00:55:35 +02:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
host = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = "localhost";
|
|
|
|
description = "Database host.";
|
|
|
|
};
|
|
|
|
dbport = mkOption {
|
|
|
|
type = with types; nullOr (either int str);
|
2019-10-17 14:45:07 +02:00
|
|
|
default = 3306;
|
2019-10-09 00:55:35 +02:00
|
|
|
description = "Database port. Not required for sqlite.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "seafile";
|
|
|
|
description = "User account under which the Seafile server runs.";
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "seafile";
|
|
|
|
description = "Group account under which the Seafile server runs.";
|
|
|
|
};
|
|
|
|
|
2019-12-30 02:58:53 +01:00
|
|
|
# name = mkOption {
|
|
|
|
# type = types.str;
|
|
|
|
# default = "Seafile";
|
|
|
|
# description = "name of the Seafile instance, will show up in client and web interface";
|
|
|
|
# };
|
2019-10-16 17:31:52 +02:00
|
|
|
|
2019-10-09 00:55:35 +02:00
|
|
|
domainName = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = "full domain name of the seafile instance";
|
|
|
|
};
|
2019-10-16 17:31:52 +02:00
|
|
|
|
2019-12-30 02:58:53 +01:00
|
|
|
# ccnetPort = mkOption {
|
|
|
|
# type = types.int;
|
|
|
|
# default = 10001;
|
|
|
|
# description = "listening port for ccnet server";
|
|
|
|
# };
|
2019-10-16 17:31:52 +02:00
|
|
|
|
|
|
|
seafilePort = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 12001;
|
|
|
|
description = "listening port for Seafile server";
|
|
|
|
};
|
|
|
|
|
2019-10-17 14:45:07 +02:00
|
|
|
fileserverPort = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 8082;
|
|
|
|
description = "listening port for Seafile's fileserver component";
|
|
|
|
};
|
|
|
|
|
2019-10-16 17:31:52 +02:00
|
|
|
seahubPort = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 443;
|
|
|
|
description = "listening http port for Seahub web interface";
|
|
|
|
};
|
|
|
|
|
2019-10-16 23:30:00 +02:00
|
|
|
externalPort = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 443;
|
|
|
|
description = "external port under which Seahub is reachable from the outside. Possibly the external port of a reverse proxy like nginx.";
|
|
|
|
};
|
|
|
|
|
|
|
|
enableTLS = mkEnableOption {
|
|
|
|
default = true;
|
|
|
|
description = "whether to use TLS and HTTPS for communication";
|
|
|
|
};
|
|
|
|
|
2019-10-16 17:31:52 +02:00
|
|
|
openFirewall = mkEnableOption {
|
|
|
|
default = true;
|
|
|
|
description = "whether to open up the firewall ports for ccnet, seafile-server and seahub";
|
|
|
|
};
|
2019-10-17 14:45:07 +02:00
|
|
|
|
|
|
|
defaultQuota = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
|
|
|
description = "default quota to be set per user, in GB";
|
|
|
|
};
|
|
|
|
|
|
|
|
trashExpirationTime = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 30;
|
|
|
|
description = "default time for automatic library trash cleanup";
|
|
|
|
};
|
|
|
|
|
|
|
|
fileRevisionHistoryDays = mkOption {
|
|
|
|
type = types.nullOr types.int;
|
|
|
|
default = null;
|
|
|
|
description = "default history length of file revisions to keep in days. <literal>null</literal> means keeping all revisions.";
|
|
|
|
};
|
|
|
|
|
|
|
|
fileserverBindAddress = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "0.0.0.0";
|
|
|
|
description = "bind address for fileserver, binds to all addresses by default";
|
|
|
|
};
|
|
|
|
|
|
|
|
fileserverWorkers = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 10;
|
|
|
|
description = "number of worker threads to server http requests";
|
|
|
|
};
|
|
|
|
|
|
|
|
fileserverIndexers = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 1;
|
|
|
|
description = "number of threads used to sequentially divide uploaded files into blocks for storage";
|
|
|
|
};
|
|
|
|
|
|
|
|
fileserverBlockSize = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 1;
|
|
|
|
description = "size of blocks that uploaded files are divided into, in MB";
|
|
|
|
};
|
2019-10-09 00:55:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
config = let
|
|
|
|
directoriesToManage = [ cfg.storagePath ];
|
|
|
|
in
|
2019-10-16 17:31:52 +02:00
|
|
|
mkIf cfg.enable {
|
2020-11-30 02:21:10 +01:00
|
|
|
services.seafile-server.seafileSettings = {
|
|
|
|
library_trash.expire_days = cfg.trashExpirationTime;
|
|
|
|
fileserver = {
|
|
|
|
host = cfg.fileserverBindAddress;
|
|
|
|
port = cfg.fileserverPort;
|
|
|
|
worker_threads = cfg.fileserverWorkers;
|
|
|
|
max_indexing_threads = cfg.fileserverIndexers;
|
|
|
|
fixed_block_size = cfg.fileserverBlockSize;
|
|
|
|
};
|
|
|
|
quota = mkIf (! isNull cfg.defaultQuota) {
|
|
|
|
default = cfg.defaultQuota;
|
|
|
|
};
|
|
|
|
history = mkIf (! isNull cfg.fileRevisionHistoryDays) {
|
|
|
|
keep_days = cfg.fileRevisionHistoryDays;
|
|
|
|
};
|
|
|
|
database = mkMerge [
|
|
|
|
{
|
|
|
|
type = cfg.db.type;
|
|
|
|
}
|
|
|
|
# while just using the cfg.db set directly might be possible and
|
|
|
|
# save lines of code, I prefer hand-picking options
|
|
|
|
(mkIf (cfg.db.type == "mysql") {
|
|
|
|
host = cfg.db.host;
|
|
|
|
port = cfg.db.port;
|
|
|
|
user = cfg.db.user;
|
|
|
|
connection_charset = "utf8";
|
|
|
|
db_name = cfg.db.dbname;
|
|
|
|
max_connections = 100;
|
|
|
|
password = "#dbpass#";
|
|
|
|
})
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2019-10-15 20:09:37 +02:00
|
|
|
systemd = {
|
|
|
|
# state directory permissions managed by systemd
|
|
|
|
tmpfiles.rules = [
|
|
|
|
"d ${cfg.storagePath} 0750 ${cfg.user} ${cfg.group} -"
|
2019-10-17 14:45:07 +02:00
|
|
|
"d ${cfg.storagePath}/conf 0750 ${cfg.user} ${cfg.group} -"
|
2019-10-16 17:31:52 +02:00
|
|
|
"d ${cfg.storagePath}/home 0710 ${cfg.user} ${cfg.group} -"
|
2019-10-15 20:09:37 +02:00
|
|
|
];
|
|
|
|
services.seafile-server = {
|
2019-10-09 00:55:35 +02:00
|
|
|
|
2020-11-27 22:26:09 +01:00
|
|
|
path = with pkgs; [ seafile-server.seafile-server-core ];
|
2019-10-15 20:09:37 +02:00
|
|
|
script = ''
|
2019-10-16 17:31:52 +02:00
|
|
|
./seafile-server/seafile-server-latest/bin/seafile-admin start
|
2019-10-15 20:09:37 +02:00
|
|
|
'';
|
2019-10-09 00:55:35 +02:00
|
|
|
serviceConfig = {
|
2019-10-15 20:09:37 +02:00
|
|
|
ExecStartPre = [
|
|
|
|
("+${pkgs.writeScript "seafile-server-preStart-privileged" ''
|
|
|
|
#!${pkgs.runtimeShell}
|
|
|
|
# stuff run as root
|
|
|
|
''}")
|
|
|
|
("${pkgs.writeShellScript "seafile-server-preStart-unprivileged" ''
|
|
|
|
# stuff run as seafile user
|
2019-10-16 17:31:52 +02:00
|
|
|
|
|
|
|
# ccnet-init must only be run once per installation, as it also generates stateful key and ID
|
|
|
|
# solution: invoke it once, use result as template
|
2019-10-17 14:45:07 +02:00
|
|
|
if [ ! -e ./ccnet/mykey.peer ]; then
|
2020-11-27 22:26:09 +01:00
|
|
|
#$ {pkgs.seafile-server.ccnet-server}/bin/ccnet-init -c ./ccnet -H 'TEMPLATEHOST'
|
2019-10-17 14:45:07 +02:00
|
|
|
mv ./ccnet/ccnet.conf{,.template}
|
2019-10-16 17:31:52 +02:00
|
|
|
fi
|
2019-10-16 23:30:00 +02:00
|
|
|
|
|
|
|
# generate actual ccnet config file
|
|
|
|
echo "[General]" > ./conf/ccnet.conf
|
2019-10-17 14:45:07 +02:00
|
|
|
grep "^ID =" ./ccnet/ccnet.conf.template >> ./conf/ccnet.conf
|
2019-10-16 23:30:00 +02:00
|
|
|
# outside URL
|
|
|
|
SERVICE_URL = http${if cfg.enableTLS then "s" else ""}://${cfg.domainName}:${toString cfg.externalPort}
|
|
|
|
|
2019-10-17 14:45:07 +02:00
|
|
|
# seafile.conf generation
|
|
|
|
|
2020-11-30 02:21:10 +01:00
|
|
|
# move seafile.conf template from nix store
|
|
|
|
cp ${seafileConfigFile} ./conf/seafile.conf
|
|
|
|
# replace placeholder secrets with real secret read from file
|
|
|
|
${if (isNull cfg.db.passwordFile) then ''
|
|
|
|
DBPASS="$(head -n1 ${toString cfg.db.passwordFile})"
|
|
|
|
sed -e "s,#dbpass#,$DBPASS,g" -i ./conf/seafile.conf
|
|
|
|
''
|
|
|
|
else ""
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-16 17:31:52 +02:00
|
|
|
ln -s ${pkgs.seafile-server} seafile-server
|
|
|
|
./seafile-server/seafile-server-latest/bin/seafile-admin setup
|
2019-10-15 20:09:37 +02:00
|
|
|
''}")
|
|
|
|
];
|
2019-10-09 00:55:35 +02:00
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
|
|
|
Type = "oneshot";
|
|
|
|
WorkingDirectory = cfg.storagePath;
|
|
|
|
};
|
|
|
|
enable = cfg.autorun;
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
};
|
2019-10-15 20:09:37 +02:00
|
|
|
};
|
2019-10-09 00:55:35 +02:00
|
|
|
|
|
|
|
users.users.${cfg.user} = {
|
2019-10-15 20:09:37 +02:00
|
|
|
home = "${cfg.storagePath}/home";
|
2019-10-09 00:55:35 +02:00
|
|
|
group = cfg.group;
|
2019-10-15 20:09:37 +02:00
|
|
|
# don't make NixOS create the home directory as otherwise the permissions for /srv might be 0700,
|
|
|
|
# making it impossible to cd into the storagePath
|
|
|
|
createHome = false;
|
2019-10-09 00:55:35 +02:00
|
|
|
isNormalUser = false;
|
|
|
|
};
|
|
|
|
users.groups.${cfg.group}.members = [ cfg.user ];
|
2019-10-16 17:31:52 +02:00
|
|
|
|
2019-12-30 02:58:53 +01:00
|
|
|
# ToDo: make sure ccnet is reachable
|
|
|
|
networking.firewall.allowedTCPPorts = with cfg; if openFirewall then [ seafilePort fileserverPort ] else [];
|
2019-10-09 00:55:35 +02:00
|
|
|
};
|
|
|
|
}
|