-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
174 lines (146 loc) · 5.12 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{
description = "Self-hosted file sharing cloud for you and your friends";
inputs = {
nixpkgs.url = github:nixos/nixpkgs/nixos-unstable;
flake-utils.url = github:numtide/flake-utils;
};
outputs = { self, nixpkgs, flake-utils }: {
nixosModule = { config, ... }:
with nixpkgs.lib;
let
system = config.nixpkgs.localSystem.system;
python = nixpkgs.legacyPackages.${system}.python3Packages.python;
flask = nixpkgs.legacyPackages.${system}.python3Packages.flask;
gunicorn = nixpkgs.legacyPackages.${system}.python3Packages.gunicorn;
raincloud = self.packages.${system}.raincloud;
cfg = config.services.raincloud;
in
{
options.services.raincloud = {
enable = mkEnableOption "Enable raincloud WSGI server";
address = mkOption {
type = types.str;
default = "127.0.0.1";
example = "0.0.0.0";
description = "Bind address of the server";
};
port = mkOption {
type = types.int;
default = 8000;
example = 4000;
description = "Port on which the server listens";
};
user = mkOption {
type = types.str;
default = "raincloud";
description = "User under which the server runs";
};
group = mkOption {
type = types.str;
default = "raincloud";
description = "Group under which the server runs";
};
cloudName = mkOption {
type = types.str;
default = "raincloud";
description = "Name of the raincloud";
};
basePath = mkOption {
type = types.str;
example = "/var/lib/raincloud";
description = "Base path of the raincloud";
};
secretKeyPath = mkOption {
type = types.str;
example = "/var/lib/raincloud/secret_key";
description = "Path to file containing Flask secret key";
};
redisUrl = mkOption {
type = types.str;
default = "redis://127.0.0.1:6379/0";
example = "unix:/run/redis-raincloud/redis.sock";
description = "URL of Redis database";
};
numWorkers = mkOption {
type = types.int;
default = 5;
example = 17;
description = "Number of Gunicorn workers (recommendation is: 2 x #CPUs + 1)";
};
workerTimeout = mkOption {
type = types.int;
default = 300;
example = 360;
description = "Gunicorn worker timeout";
};
};
config = mkIf cfg.enable {
systemd.services.raincloud = {
description = "Enable raincloud WSGI server";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
restartIfChanged = true;
environment =
let
penv = python.buildEnv.override {
extraLibs = [ flask raincloud ];
};
in
{
PYTHONPATH = "${penv}/${python.sitePackages}/";
};
serviceConfig = {
Type = "simple";
User = cfg.user;
Group = cfg.group;
Restart = "always";
RestartSec = "5s";
PermissionsStartOnly = true;
ExecStart = ''
${gunicorn}/bin/gunicorn "raincloud:create_app('${cfg.basePath}', '${cfg.secretKeyPath}', '${cfg.redisUrl}', '${cfg.cloudName}')" \
--bind=${cfg.address}:${toString cfg.port} \
--workers ${toString cfg.numWorkers} \
--timeout ${toString cfg.workerTimeout}
'';
};
};
users.users = mkIf (cfg.user == "raincloud") {
raincloud = {
group = cfg.group;
isSystemUser = true;
};
};
users.groups = mkIf (cfg.group == "raincloud") {
raincloud = { };
};
};
};
} // flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
# Package
packages.raincloud =
pkgs.python3Packages.buildPythonPackage rec {
name = "raincloud";
src = self;
propagatedBuildInputs = with pkgs; [
python3Packages.flask
python3Packages.redis
];
};
defaultPackage = self.packages.${system}.raincloud;
# Development shell
devShell = pkgs.mkShell {
buildInputs = with pkgs; [
python3
python3Packages.flask
python3Packages.gunicorn
python3Packages.redis
];
};
}
);
}