2014-12-10 01:41:25 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.uwsgi;
|
|
|
|
|
2020-11-06 08:58:49 +00:00
|
|
|
isEmperor = cfg.instance.type == "emperor";
|
|
|
|
|
|
|
|
imperialPowers =
|
|
|
|
[
|
|
|
|
# spawn other user processes
|
|
|
|
"CAP_SETUID" "CAP_SETGID"
|
|
|
|
"CAP_SYS_CHROOT"
|
|
|
|
# transfer capabilities
|
|
|
|
"CAP_SETPCAP"
|
|
|
|
# create other user sockets
|
|
|
|
"CAP_CHOWN"
|
|
|
|
];
|
|
|
|
|
2016-02-19 14:02:14 +00:00
|
|
|
buildCfg = name: c:
|
|
|
|
let
|
2020-04-12 17:25:35 +00:00
|
|
|
plugins' =
|
2016-02-19 14:02:14 +00:00
|
|
|
if any (n: !any (m: m == n) cfg.plugins) (c.plugins or [])
|
2020-11-06 08:58:49 +00:00
|
|
|
then throw "`plugins` attribute in uWSGI configuration contains plugins not in config.services.uwsgi.plugins"
|
2016-02-19 14:02:14 +00:00
|
|
|
else c.plugins or cfg.plugins;
|
2020-04-12 17:25:35 +00:00
|
|
|
plugins = unique plugins';
|
2016-02-19 14:02:14 +00:00
|
|
|
|
|
|
|
hasPython = v: filter (n: n == "python${v}") plugins != [];
|
|
|
|
hasPython2 = hasPython "2";
|
|
|
|
hasPython3 = hasPython "3";
|
|
|
|
|
|
|
|
python =
|
|
|
|
if hasPython2 && hasPython3 then
|
2020-11-06 08:58:49 +00:00
|
|
|
throw "`plugins` attribute in uWSGI configuration shouldn't contain both python2 and python3"
|
2019-09-16 15:08:32 +00:00
|
|
|
else if hasPython2 then cfg.package.python2
|
|
|
|
else if hasPython3 then cfg.package.python3
|
2016-02-19 14:02:14 +00:00
|
|
|
else null;
|
|
|
|
|
2018-06-04 07:43:23 +00:00
|
|
|
pythonEnv = python.withPackages (c.pythonPackages or (self: []));
|
2016-03-21 15:46:26 +00:00
|
|
|
|
|
|
|
uwsgiCfg = {
|
2016-02-19 14:02:14 +00:00
|
|
|
uwsgi =
|
|
|
|
if c.type == "normal"
|
|
|
|
then {
|
|
|
|
inherit plugins;
|
|
|
|
} // removeAttrs c [ "type" "pythonPackages" ]
|
|
|
|
// optionalAttrs (python != null) {
|
2020-03-04 19:20:32 +00:00
|
|
|
pyhome = "${pythonEnv}";
|
2016-03-21 15:46:26 +00:00
|
|
|
env =
|
|
|
|
# Argh, uwsgi expects list of key-values there instead of a dictionary.
|
2022-01-05 11:13:37 +00:00
|
|
|
let envs = partition (hasPrefix "PATH=") (c.env or []);
|
|
|
|
oldPaths = map (x: substring (stringLength "PATH=") (stringLength x) x) envs.right;
|
|
|
|
paths = oldPaths ++ [ "${pythonEnv}/bin" ];
|
|
|
|
in [ "PATH=${concatStringsSep ":" paths}" ] ++ envs.wrong;
|
2016-02-19 14:02:14 +00:00
|
|
|
}
|
2020-11-06 08:58:49 +00:00
|
|
|
else if isEmperor
|
2016-02-19 14:02:14 +00:00
|
|
|
then {
|
|
|
|
emperor = if builtins.typeOf c.vassals != "set" then c.vassals
|
|
|
|
else pkgs.buildEnv {
|
|
|
|
name = "vassals";
|
|
|
|
paths = mapAttrsToList buildCfg c.vassals;
|
|
|
|
};
|
|
|
|
} // removeAttrs c [ "type" "vassals" ]
|
2020-11-06 08:58:49 +00:00
|
|
|
else throw "`type` attribute in uWSGI configuration should be either 'normal' or 'emperor'";
|
2016-02-19 14:02:14 +00:00
|
|
|
};
|
2014-12-10 01:41:25 +00:00
|
|
|
|
2016-03-21 15:46:26 +00:00
|
|
|
in pkgs.writeTextDir "${name}.json" (builtins.toJSON uwsgiCfg);
|
2014-12-10 01:41:25 +00:00
|
|
|
|
|
|
|
in {
|
|
|
|
|
|
|
|
options = {
|
|
|
|
services.uwsgi = {
|
2015-06-24 14:42:43 +00:00
|
|
|
|
2014-12-10 01:41:25 +00:00
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Enable uWSGI";
|
2014-12-10 01:41:25 +00:00
|
|
|
};
|
|
|
|
|
2015-06-24 14:42:43 +00:00
|
|
|
runDir = mkOption {
|
2019-08-08 20:48:27 +00:00
|
|
|
type = types.path;
|
2015-06-24 14:42:43 +00:00
|
|
|
default = "/run/uwsgi";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Where uWSGI communication sockets can live";
|
2015-06-24 14:42:43 +00:00
|
|
|
};
|
|
|
|
|
2019-09-16 15:08:32 +00:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
internal = true;
|
|
|
|
};
|
|
|
|
|
2014-12-10 01:41:25 +00:00
|
|
|
instance = mkOption {
|
2020-11-06 08:58:49 +00:00
|
|
|
type = with types; let
|
2020-05-09 16:24:59 +00:00
|
|
|
valueType = nullOr (oneOf [
|
|
|
|
bool
|
|
|
|
int
|
|
|
|
float
|
|
|
|
str
|
|
|
|
(lazyAttrsOf valueType)
|
|
|
|
(listOf valueType)
|
|
|
|
(mkOptionType {
|
|
|
|
name = "function";
|
|
|
|
description = "function";
|
|
|
|
check = x: isFunction x;
|
|
|
|
merge = mergeOneOption;
|
|
|
|
})
|
|
|
|
]) // {
|
|
|
|
description = "Json value or lambda";
|
|
|
|
emptyValue.value = {};
|
|
|
|
};
|
|
|
|
in valueType;
|
2014-12-10 01:41:25 +00:00
|
|
|
default = {
|
|
|
|
type = "normal";
|
|
|
|
};
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2014-12-10 01:41:25 +00:00
|
|
|
{
|
|
|
|
type = "emperor";
|
|
|
|
vassals = {
|
|
|
|
moin = {
|
|
|
|
type = "normal";
|
2016-02-19 14:10:05 +00:00
|
|
|
pythonPackages = self: with self; [ moinmoin ];
|
2021-11-26 00:16:07 +00:00
|
|
|
socket = "''${config.services.uwsgi.runDir}/uwsgi.sock";
|
2014-12-10 01:41:25 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
'';
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
uWSGI configuration. It awaits an attribute `type` inside which can be either
|
|
|
|
`normal` or `emperor`.
|
2016-02-19 14:10:05 +00:00
|
|
|
|
2022-07-28 21:19:15 +00:00
|
|
|
For `normal` mode you can specify `pythonPackages` as a function
|
|
|
|
from libraries set into a list of libraries. `pythonpath` will be set accordingly.
|
2014-12-10 01:41:25 +00:00
|
|
|
|
2022-07-28 21:19:15 +00:00
|
|
|
For `emperor` mode, you should use `vassals` attribute
|
2014-12-10 01:41:25 +00:00
|
|
|
which should be either a set of names and configurations or a path to a directory.
|
2016-02-19 14:10:05 +00:00
|
|
|
|
|
|
|
Other attributes will be used in configuration file as-is. Notice that you can redefine
|
2022-07-28 21:19:15 +00:00
|
|
|
`plugins` setting here.
|
2014-12-10 01:41:25 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
plugins = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Plugins used with uWSGI";
|
2014-12-10 01:41:25 +00:00
|
|
|
};
|
|
|
|
|
2015-06-24 14:42:43 +00:00
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "uwsgi";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "User account under which uWSGI runs.";
|
2015-06-24 14:42:43 +00:00
|
|
|
};
|
2014-12-10 01:41:25 +00:00
|
|
|
|
2015-06-24 14:42:43 +00:00
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "uwsgi";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc "Group account under which uWSGI runs.";
|
2020-11-06 08:58:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
capabilities = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
apply = caps: caps ++ optionals isEmperor imperialPowers;
|
|
|
|
default = [ ];
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2020-11-06 08:58:49 +00:00
|
|
|
[
|
|
|
|
"CAP_NET_BIND_SERVICE" # bind on ports <1024
|
|
|
|
"CAP_NET_RAW" # open raw sockets
|
|
|
|
]
|
|
|
|
'';
|
2022-08-30 00:30:04 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-11-06 08:58:49 +00:00
|
|
|
Grant capabilities to the uWSGI instance. See the
|
2022-08-30 00:30:04 +00:00
|
|
|
`capabilities(7)` for available values.
|
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
uWSGI runs as an unprivileged user (even as Emperor) with the minimal
|
|
|
|
capabilities required. This option can be used to add fine-grained
|
|
|
|
permissions without running the service as root.
|
|
|
|
|
|
|
|
When in Emperor mode, any capability to be inherited by a vassal must
|
|
|
|
be specified again in the vassal configuration using `cap`.
|
|
|
|
See the uWSGI [docs](https://uwsgi-docs.readthedocs.io/en/latest/Capabilities.html)
|
|
|
|
for more information.
|
|
|
|
:::
|
2020-11-06 08:58:49 +00:00
|
|
|
'';
|
2015-06-24 14:42:43 +00:00
|
|
|
};
|
|
|
|
};
|
2014-12-10 01:41:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2020-11-06 08:58:49 +00:00
|
|
|
systemd.tmpfiles.rules = optional (cfg.runDir != "/run/uwsgi") ''
|
|
|
|
d ${cfg.runDir} 775 ${cfg.user} ${cfg.group}
|
|
|
|
'';
|
|
|
|
|
2014-12-10 01:41:25 +00:00
|
|
|
systemd.services.uwsgi = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
2020-11-06 08:58:49 +00:00
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
2014-12-10 01:41:25 +00:00
|
|
|
Type = "notify";
|
2020-11-06 08:58:49 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/uwsgi --json ${buildCfg "server" cfg.instance}/server.json";
|
2014-12-10 01:41:25 +00:00
|
|
|
ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
|
|
|
|
ExecStop = "${pkgs.coreutils}/bin/kill -INT $MAINPID";
|
|
|
|
NotifyAccess = "main";
|
|
|
|
KillSignal = "SIGQUIT";
|
2020-11-06 08:58:49 +00:00
|
|
|
AmbientCapabilities = cfg.capabilities;
|
|
|
|
CapabilityBoundingSet = cfg.capabilities;
|
2021-01-25 11:13:34 +00:00
|
|
|
RuntimeDirectory = mkIf (cfg.runDir == "/run/uwsgi") "uwsgi";
|
2014-12-10 01:41:25 +00:00
|
|
|
};
|
|
|
|
};
|
2015-06-24 14:42:43 +00:00
|
|
|
|
2019-09-14 17:51:29 +00:00
|
|
|
users.users = optionalAttrs (cfg.user == "uwsgi") {
|
|
|
|
uwsgi = {
|
2015-06-24 14:42:43 +00:00
|
|
|
group = cfg.group;
|
|
|
|
uid = config.ids.uids.uwsgi;
|
2019-09-14 17:51:29 +00:00
|
|
|
};
|
|
|
|
};
|
2015-06-24 14:42:43 +00:00
|
|
|
|
2019-09-14 17:51:29 +00:00
|
|
|
users.groups = optionalAttrs (cfg.group == "uwsgi") {
|
|
|
|
uwsgi.gid = config.ids.gids.uwsgi;
|
|
|
|
};
|
2019-09-16 15:08:32 +00:00
|
|
|
|
|
|
|
services.uwsgi.package = pkgs.uwsgi.override {
|
2020-04-12 17:25:35 +00:00
|
|
|
plugins = unique cfg.plugins;
|
2019-09-16 15:08:32 +00:00
|
|
|
};
|
2014-12-10 01:41:25 +00:00
|
|
|
};
|
|
|
|
}
|