2015-10-21 13:45:27 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
2019-03-07 03:18:59 +00:00
|
|
|
gunicorn = pkgs.python3Packages.gunicorn;
|
2017-08-25 16:51:18 +01:00
|
|
|
bepasty = pkgs.bepasty;
|
2019-03-07 03:18:59 +00:00
|
|
|
gevent = pkgs.python3Packages.gevent;
|
|
|
|
python = pkgs.python3Packages.python;
|
2015-10-21 13:45:27 +01:00
|
|
|
cfg = config.services.bepasty;
|
|
|
|
user = "bepasty";
|
|
|
|
group = "bepasty";
|
|
|
|
default_home = "/var/lib/bepasty";
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.bepasty = {
|
|
|
|
enable = mkEnableOption "Bepasty servers";
|
|
|
|
|
|
|
|
servers = mkOption {
|
|
|
|
default = {};
|
|
|
|
description = ''
|
|
|
|
configure a number of bepasty servers which will be started with
|
|
|
|
gunicorn.
|
|
|
|
'';
|
2017-04-08 18:32:19 +01:00
|
|
|
type = with types ; attrsOf (submodule ({ config, ... } : {
|
2015-10-21 13:45:27 +01:00
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
bind = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
Bind address to be used for this server.
|
|
|
|
'';
|
|
|
|
example = "0.0.0.0:8000";
|
|
|
|
default = "127.0.0.1:8000";
|
|
|
|
};
|
|
|
|
|
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
Path to the directory where the pastes will be saved to
|
|
|
|
'';
|
|
|
|
default = default_home+"/data";
|
|
|
|
};
|
|
|
|
|
|
|
|
defaultPermissions = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
default permissions for all unauthenticated accesses.
|
|
|
|
'';
|
|
|
|
example = "read,create,delete";
|
|
|
|
default = "read";
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
2016-10-23 18:33:41 +01:00
|
|
|
type = types.lines;
|
2015-10-21 13:45:27 +01:00
|
|
|
description = ''
|
|
|
|
Extra configuration for bepasty server to be appended on the
|
|
|
|
configuration.
|
|
|
|
see https://bepasty-server.readthedocs.org/en/latest/quickstart.html#configuring-bepasty
|
|
|
|
for all options.
|
|
|
|
'';
|
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
PERMISSIONS = {
|
|
|
|
'myadminsecret': 'admin,list,create,read,delete',
|
|
|
|
}
|
|
|
|
MAX_ALLOWED_FILE_SIZE = 5 * 1000 * 1000
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
secretKey = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
server secret for safe session cookies, must be set.
|
2017-04-08 18:32:19 +01:00
|
|
|
|
|
|
|
Warning: this secret is stored in the WORLD-READABLE Nix store!
|
|
|
|
|
|
|
|
It's recommended to use <option>secretKeyFile</option>
|
|
|
|
which takes precedence over <option>secretKey</option>.
|
2015-10-21 13:45:27 +01:00
|
|
|
'';
|
|
|
|
default = "";
|
|
|
|
};
|
|
|
|
|
2017-04-08 18:32:19 +01:00
|
|
|
secretKeyFile = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
description = ''
|
|
|
|
A file that contains the server secret for safe session cookies, must be set.
|
|
|
|
|
|
|
|
<option>secretKeyFile</option> takes precedence over <option>secretKey</option>.
|
|
|
|
|
|
|
|
Warning: when <option>secretKey</option> is non-empty <option>secretKeyFile</option>
|
|
|
|
defaults to a file in the WORLD-READABLE Nix store containing that secret.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2015-10-21 13:45:27 +01:00
|
|
|
workDir = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
description = ''
|
|
|
|
Path to the working directory (used for config and pidfile).
|
|
|
|
Defaults to the users home directory.
|
|
|
|
'';
|
|
|
|
default = default_home;
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
2017-04-08 18:32:19 +01:00
|
|
|
config = {
|
|
|
|
secretKeyFile = mkDefault (
|
|
|
|
if config.secretKey != ""
|
|
|
|
then toString (pkgs.writeTextFile {
|
|
|
|
name = "bepasty-secret-key";
|
|
|
|
text = config.secretKey;
|
|
|
|
})
|
|
|
|
else null
|
|
|
|
);
|
|
|
|
};
|
2015-10-21 13:45:27 +01:00
|
|
|
}));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2017-04-08 18:32:19 +01:00
|
|
|
|
2015-10-21 13:45:27 +01:00
|
|
|
environment.systemPackages = [ bepasty ];
|
|
|
|
|
|
|
|
# creates gunicorn systemd service for each configured server
|
|
|
|
systemd.services = mapAttrs' (name: server:
|
|
|
|
nameValuePair ("bepasty-server-${name}-gunicorn")
|
|
|
|
({
|
|
|
|
description = "Bepasty Server ${name}";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
restartIfChanged = true;
|
|
|
|
|
2016-02-28 00:35:56 +00:00
|
|
|
environment = let
|
|
|
|
penv = python.buildEnv.override {
|
|
|
|
extraLibs = [ bepasty gevent ];
|
|
|
|
};
|
|
|
|
in {
|
2015-10-21 13:45:27 +01:00
|
|
|
BEPASTY_CONFIG = "${server.workDir}/bepasty-${name}.conf";
|
2016-02-28 00:35:56 +00:00
|
|
|
PYTHONPATH= "${penv}/${python.sitePackages}/";
|
2015-10-21 13:45:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
|
|
|
PrivateTmp = true;
|
2019-04-24 04:48:22 +01:00
|
|
|
ExecStartPre = assert server.secretKeyFile != null; pkgs.writeScript "bepasty-server.${name}-init" ''
|
2015-10-21 13:45:27 +01:00
|
|
|
#!/bin/sh
|
|
|
|
mkdir -p "${server.workDir}"
|
|
|
|
mkdir -p "${server.dataDir}"
|
|
|
|
chown ${user}:${group} "${server.workDir}" "${server.dataDir}"
|
|
|
|
cat > ${server.workDir}/bepasty-${name}.conf <<EOF
|
|
|
|
SITENAME="${name}"
|
|
|
|
STORAGE_FILESYSTEM_DIRECTORY="${server.dataDir}"
|
2017-04-08 18:32:19 +01:00
|
|
|
SECRET_KEY="$(cat "${server.secretKeyFile}")"
|
2015-10-21 13:45:27 +01:00
|
|
|
DEFAULT_PERMISSIONS="${server.defaultPermissions}"
|
|
|
|
${server.extraConfig}
|
|
|
|
EOF
|
|
|
|
'';
|
|
|
|
ExecStart = ''${gunicorn}/bin/gunicorn bepasty.wsgi --name ${name} \
|
|
|
|
-u ${user} \
|
|
|
|
-g ${group} \
|
|
|
|
--workers 3 --log-level=info \
|
|
|
|
--bind=${server.bind} \
|
|
|
|
--pid ${server.workDir}/gunicorn-${name}.pid \
|
|
|
|
-k gevent
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
})
|
|
|
|
) cfg.servers;
|
|
|
|
|
2019-09-14 18:51:29 +01:00
|
|
|
users.users.${user} =
|
|
|
|
{ uid = config.ids.uids.bepasty;
|
|
|
|
group = group;
|
|
|
|
home = default_home;
|
|
|
|
};
|
|
|
|
|
|
|
|
users.groups.${group}.gid = config.ids.gids.bepasty;
|
2015-10-21 13:45:27 +01:00
|
|
|
};
|
|
|
|
}
|