2021-10-28 06:52:43 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.seafile;
|
|
|
|
settingsFormat = pkgs.formats.ini { };
|
|
|
|
|
|
|
|
ccnetConf = settingsFormat.generate "ccnet.conf" cfg.ccnetSettings;
|
|
|
|
|
|
|
|
seafileConf = settingsFormat.generate "seafile.conf" cfg.seafileSettings;
|
|
|
|
|
|
|
|
seahubSettings = pkgs.writeText "seahub_settings.py" ''
|
|
|
|
FILE_SERVER_ROOT = '${cfg.ccnetSettings.General.SERVICE_URL}/seafhttp'
|
|
|
|
DATABASES = {
|
|
|
|
'default': {
|
|
|
|
'ENGINE': 'django.db.backends.sqlite3',
|
|
|
|
'NAME': '${seahubDir}/seahub.db',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MEDIA_ROOT = '${seahubDir}/media/'
|
|
|
|
THUMBNAIL_ROOT = '${seahubDir}/thumbnail/'
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
SERVICE_URL = '${cfg.ccnetSettings.General.SERVICE_URL}'
|
|
|
|
|
2021-10-28 06:52:43 +00:00
|
|
|
with open('${seafRoot}/.seahubSecret') as f:
|
|
|
|
SECRET_KEY = f.readline().rstrip()
|
|
|
|
|
|
|
|
${cfg.seahubExtraConf}
|
|
|
|
'';
|
|
|
|
|
|
|
|
seafRoot = "/var/lib/seafile"; # hardcode it due to dynamicuser
|
|
|
|
ccnetDir = "${seafRoot}/ccnet";
|
|
|
|
dataDir = "${seafRoot}/data";
|
|
|
|
seahubDir = "${seafRoot}/seahub";
|
|
|
|
|
|
|
|
in {
|
|
|
|
|
|
|
|
###### Interface
|
|
|
|
|
|
|
|
options.services.seafile = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "Seafile server");
|
2021-10-28 06:52:43 +00:00
|
|
|
|
|
|
|
ccnetSettings = mkOption {
|
|
|
|
type = types.submodule {
|
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
|
|
|
|
options = {
|
|
|
|
General = {
|
|
|
|
SERVICE_URL = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "https://www.example.com";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-10-28 06:52:43 +00:00
|
|
|
Seahub public URL.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
default = { };
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-10-28 06:52:43 +00:00
|
|
|
Configuration for ccnet, see
|
2022-08-12 12:06:08 +00:00
|
|
|
<https://manual.seafile.com/config/ccnet-conf/>
|
2021-10-28 06:52:43 +00:00
|
|
|
for supported values.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
seafileSettings = mkOption {
|
|
|
|
type = types.submodule {
|
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
|
|
|
|
options = {
|
|
|
|
fileserver = {
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
|
|
|
default = 8082;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-10-28 06:52:43 +00:00
|
|
|
The tcp port used by seafile fileserver.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
host = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "127.0.0.1";
|
|
|
|
example = "0.0.0.0";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-10-28 06:52:43 +00:00
|
|
|
The binding address used by seafile fileserver.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
default = { };
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-10-28 06:52:43 +00:00
|
|
|
Configuration for seafile-server, see
|
2022-08-12 12:06:08 +00:00
|
|
|
<https://manual.seafile.com/config/seafile-conf/>
|
2021-10-28 06:52:43 +00:00
|
|
|
for supported values.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
workers = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 4;
|
|
|
|
example = 10;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-10-28 06:52:43 +00:00
|
|
|
The number of gunicorn worker processes for handling requests.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
adminEmail = mkOption {
|
|
|
|
example = "john@example.com";
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-10-28 06:52:43 +00:00
|
|
|
Seafile Seahub Admin Account Email.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
initialAdminPassword = mkOption {
|
|
|
|
example = "someStrongPass";
|
|
|
|
type = types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-10-28 06:52:43 +00:00
|
|
|
Seafile Seahub Admin Account initial password.
|
|
|
|
Should be change via Seahub web front-end.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
seafilePackage = mkPackageOption pkgs "seafile-server" { };
|
2021-10-28 06:52:43 +00:00
|
|
|
|
|
|
|
seahubExtraConf = mkOption {
|
|
|
|
default = "";
|
|
|
|
type = types.lines;
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-10-28 06:52:43 +00:00
|
|
|
Extra config to append to `seahub_settings.py` file.
|
2022-09-09 14:08:57 +00:00
|
|
|
Refer to <https://manual.seafile.com/config/seahub_settings_py/>
|
2021-10-28 06:52:43 +00:00
|
|
|
for all available options.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
###### Implementation
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
|
|
|
|
environment.etc."seafile/ccnet.conf".source = ccnetConf;
|
|
|
|
environment.etc."seafile/seafile.conf".source = seafileConf;
|
|
|
|
environment.etc."seafile/seahub_settings.py".source = seahubSettings;
|
|
|
|
|
|
|
|
systemd.targets.seafile = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
description = "Seafile components";
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services = let
|
|
|
|
securityOptions = {
|
|
|
|
ProtectHome = true;
|
|
|
|
PrivateUsers = true;
|
|
|
|
PrivateDevices = true;
|
|
|
|
ProtectClock = true;
|
|
|
|
ProtectHostname = true;
|
|
|
|
ProtectProc = "invisible";
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
ProtectKernelLogs = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
RestrictNamespaces = true;
|
|
|
|
LockPersonality = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
RestrictAddressFamilies = [ "AF_UNIX" "AF_INET" ];
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
seaf-server = {
|
|
|
|
description = "Seafile server";
|
|
|
|
partOf = [ "seafile.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
wantedBy = [ "seafile.target" ];
|
|
|
|
restartTriggers = [ ccnetConf seafileConf ];
|
2022-08-12 12:06:08 +00:00
|
|
|
path = [ pkgs.sqlite ];
|
2021-10-28 06:52:43 +00:00
|
|
|
serviceConfig = securityOptions // {
|
|
|
|
User = "seafile";
|
|
|
|
Group = "seafile";
|
|
|
|
DynamicUser = true;
|
|
|
|
StateDirectory = "seafile";
|
|
|
|
RuntimeDirectory = "seafile";
|
|
|
|
LogsDirectory = "seafile";
|
|
|
|
ConfigurationDirectory = "seafile";
|
|
|
|
ExecStart = ''
|
|
|
|
${cfg.seafilePackage}/bin/seaf-server \
|
|
|
|
--foreground \
|
|
|
|
-F /etc/seafile \
|
|
|
|
-c ${ccnetDir} \
|
|
|
|
-d ${dataDir} \
|
|
|
|
-l /var/log/seafile/server.log \
|
|
|
|
-P /run/seafile/server.pid \
|
|
|
|
-p /run/seafile
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
preStart = ''
|
|
|
|
if [ ! -f "${seafRoot}/server-setup" ]; then
|
|
|
|
mkdir -p ${dataDir}/library-template
|
|
|
|
mkdir -p ${ccnetDir}/{GroupMgr,misc,OrgMgr,PeerMgr}
|
2022-08-12 12:06:08 +00:00
|
|
|
sqlite3 ${ccnetDir}/GroupMgr/groupmgr.db ".read ${cfg.seafilePackage}/share/seafile/sql/sqlite/groupmgr.sql"
|
|
|
|
sqlite3 ${ccnetDir}/misc/config.db ".read ${cfg.seafilePackage}/share/seafile/sql/sqlite/config.sql"
|
|
|
|
sqlite3 ${ccnetDir}/OrgMgr/orgmgr.db ".read ${cfg.seafilePackage}/share/seafile/sql/sqlite/org.sql"
|
|
|
|
sqlite3 ${ccnetDir}/PeerMgr/usermgr.db ".read ${cfg.seafilePackage}/share/seafile/sql/sqlite/user.sql"
|
|
|
|
sqlite3 ${dataDir}/seafile.db ".read ${cfg.seafilePackage}/share/seafile/sql/sqlite/seafile.sql"
|
2021-10-28 06:52:43 +00:00
|
|
|
echo "${cfg.seafilePackage.version}-sqlite" > "${seafRoot}"/server-setup
|
|
|
|
fi
|
|
|
|
# checking for upgrades and handling them
|
|
|
|
# WARNING: needs to be extended to actually handle major version migrations
|
|
|
|
installedMajor=$(cat "${seafRoot}/server-setup" | cut -d"-" -f1 | cut -d"." -f1)
|
|
|
|
installedMinor=$(cat "${seafRoot}/server-setup" | cut -d"-" -f1 | cut -d"." -f2)
|
|
|
|
pkgMajor=$(echo "${cfg.seafilePackage.version}" | cut -d"." -f1)
|
|
|
|
pkgMinor=$(echo "${cfg.seafilePackage.version}" | cut -d"." -f2)
|
2022-08-12 12:06:08 +00:00
|
|
|
|
|
|
|
if [[ $installedMajor == $pkgMajor && $installedMinor == $pkgMinor ]]; then
|
|
|
|
:
|
|
|
|
elif [[ $installedMajor == 8 && $installedMinor == 0 && $pkgMajor == 9 && $pkgMinor == 0 ]]; then
|
|
|
|
# Upgrade from 8.0 to 9.0
|
|
|
|
sqlite3 ${dataDir}/seafile.db ".read ${pkgs.seahub}/scripts/upgrade/sql/9.0.0/sqlite3/seafile.sql"
|
|
|
|
echo "${cfg.seafilePackage.version}-sqlite" > "${seafRoot}"/server-setup
|
|
|
|
else
|
2021-10-28 06:52:43 +00:00
|
|
|
echo "Unsupported upgrade" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-02-10 20:34:41 +00:00
|
|
|
seahub = {
|
2021-10-28 06:52:43 +00:00
|
|
|
description = "Seafile Server Web Frontend";
|
|
|
|
wantedBy = [ "seafile.target" ];
|
|
|
|
partOf = [ "seafile.target" ];
|
|
|
|
after = [ "network.target" "seaf-server.service" ];
|
|
|
|
requires = [ "seaf-server.service" ];
|
|
|
|
restartTriggers = [ seahubSettings ];
|
|
|
|
environment = {
|
2022-02-10 20:34:41 +00:00
|
|
|
PYTHONPATH = "${pkgs.seahub.pythonPath}:${pkgs.seahub}/thirdpart:${pkgs.seahub}";
|
2021-10-28 06:52:43 +00:00
|
|
|
DJANGO_SETTINGS_MODULE = "seahub.settings";
|
|
|
|
CCNET_CONF_DIR = ccnetDir;
|
|
|
|
SEAFILE_CONF_DIR = dataDir;
|
|
|
|
SEAFILE_CENTRAL_CONF_DIR = "/etc/seafile";
|
|
|
|
SEAFILE_RPC_PIPE_PATH = "/run/seafile";
|
|
|
|
SEAHUB_LOG_DIR = "/var/log/seafile";
|
|
|
|
};
|
|
|
|
serviceConfig = securityOptions // {
|
|
|
|
User = "seafile";
|
|
|
|
Group = "seafile";
|
|
|
|
DynamicUser = true;
|
|
|
|
RuntimeDirectory = "seahub";
|
|
|
|
StateDirectory = "seafile";
|
|
|
|
LogsDirectory = "seafile";
|
|
|
|
ConfigurationDirectory = "seafile";
|
|
|
|
ExecStart = ''
|
2022-02-10 20:34:41 +00:00
|
|
|
${pkgs.seahub.python.pkgs.gunicorn}/bin/gunicorn seahub.wsgi:application \
|
2021-10-28 06:52:43 +00:00
|
|
|
--name seahub \
|
|
|
|
--workers ${toString cfg.workers} \
|
|
|
|
--log-level=info \
|
|
|
|
--preload \
|
|
|
|
--timeout=1200 \
|
|
|
|
--limit-request-line=8190 \
|
|
|
|
--bind unix:/run/seahub/gunicorn.sock
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
preStart = ''
|
|
|
|
mkdir -p ${seahubDir}/media
|
|
|
|
# Link all media except avatars
|
2022-02-10 20:34:41 +00:00
|
|
|
for m in `find ${pkgs.seahub}/media/ -maxdepth 1 -not -name "avatars"`; do
|
2021-10-28 06:52:43 +00:00
|
|
|
ln -sf $m ${seahubDir}/media/
|
|
|
|
done
|
|
|
|
if [ ! -e "${seafRoot}/.seahubSecret" ]; then
|
2022-02-10 20:34:41 +00:00
|
|
|
${pkgs.seahub.python}/bin/python ${pkgs.seahub}/tools/secret_key_generator.py > ${seafRoot}/.seahubSecret
|
2021-10-28 06:52:43 +00:00
|
|
|
chmod 400 ${seafRoot}/.seahubSecret
|
|
|
|
fi
|
|
|
|
if [ ! -f "${seafRoot}/seahub-setup" ]; then
|
|
|
|
# avatars directory should be writable
|
2022-02-10 20:34:41 +00:00
|
|
|
install -D -t ${seahubDir}/media/avatars/ ${pkgs.seahub}/media/avatars/default.png
|
|
|
|
install -D -t ${seahubDir}/media/avatars/groups ${pkgs.seahub}/media/avatars/groups/default.png
|
2021-10-28 06:52:43 +00:00
|
|
|
# init database
|
2022-02-10 20:34:41 +00:00
|
|
|
${pkgs.seahub}/manage.py migrate
|
2021-10-28 06:52:43 +00:00
|
|
|
# create admin account
|
2022-02-10 20:34:41 +00:00
|
|
|
${pkgs.expect}/bin/expect -c 'spawn ${pkgs.seahub}/manage.py createsuperuser --email=${cfg.adminEmail}; expect "Password: "; send "${cfg.initialAdminPassword}\r"; expect "Password (again): "; send "${cfg.initialAdminPassword}\r"; expect "Superuser created successfully."'
|
|
|
|
echo "${pkgs.seahub.version}-sqlite" > "${seafRoot}/seahub-setup"
|
2021-10-28 06:52:43 +00:00
|
|
|
fi
|
2022-02-10 20:34:41 +00:00
|
|
|
if [ $(cat "${seafRoot}/seahub-setup" | cut -d"-" -f1) != "${pkgs.seahub.version}" ]; then
|
2021-10-28 06:52:43 +00:00
|
|
|
# update database
|
2022-02-10 20:34:41 +00:00
|
|
|
${pkgs.seahub}/manage.py migrate
|
|
|
|
echo "${pkgs.seahub.version}-sqlite" > "${seafRoot}/seahub-setup"
|
2021-10-28 06:52:43 +00:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|