blob: 5e74931c579fe5b85bb1ebf6131964d6cedb5a77 (
plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
{
config,
options,
pkgs,
lib,
...
}:
let
cfg = config.services.memos;
opt = options.services.memos;
envFileFormat = pkgs.formats.keyValue { };
in
{
options.services.memos = {
enable = lib.mkEnableOption "Memos note-taking";
package = lib.mkPackageOption pkgs "Memos" {
default = "memos";
};
openFirewall = lib.mkEnableOption "opening the ports in the firewall";
user = lib.mkOption {
type = lib.types.str;
description = ''
The user to run Memos as.
::: {.note}
If changing the default value, **you** are responsible of creating the corresponding user with [{option}`users.users`](#opt-users.users).
:::
'';
default = "memos";
};
group = lib.mkOption {
type = lib.types.str;
description = ''
The group to run Memos as.
::: {.note}
If changing the default value, **you** are responsible of creating the corresponding group with [{option}`users.groups`](#opt-users.groups).
:::
'';
default = "memos";
};
dataDir = lib.mkOption {
default = "/var/lib/memos/";
type = lib.types.path;
description = ''
Specifies the directory where Memos will store its data.
::: {.note}
It will be automatically created with the permissions of [{option}`services.memos.user`](#opt-services.memos.user) and [{option}`services.memos.group`](#opt-services.memos.group).
:::
'';
};
settings = lib.mkOption {
type = envFileFormat.type;
description = ''
The environment variables to configure Memos.
::: {.note}
At time of writing, there is no clear documentation about possible values.
It's possible to convert CLI flags into these variables.
Example : CLI flag "--unix-sock" converts to {env}`MEMOS_UNIX_SOCK`.
:::
'';
default = {
MEMOS_MODE = "prod";
MEMOS_ADDR = "127.0.0.1";
MEMOS_PORT = "5230";
MEMOS_DATA = cfg.dataDir;
MEMOS_DRIVER = "sqlite";
MEMOS_INSTANCE_URL = "http://localhost:5230";
};
defaultText = lib.literalExpression ''
{
MEMOS_MODE = "prod";
MEMOS_ADDR = "127.0.0.1";
MEMOS_PORT = "5230";
MEMOS_DATA = config.${opt.dataDir};
MEMOS_DRIVER = "sqlite";
MEMOS_INSTANCE_URL = "http://localhost:5230";
}
'';
};
environmentFile = lib.mkOption {
type = lib.types.path;
description = ''
The environment file to use when starting Memos.
::: {.note}
By default, generated from [](opt-${opt.settings}).
:::
'';
example = "/var/lib/memos/memos.env";
default = envFileFormat.generate "memos.env" cfg.settings;
defaultText = lib.literalMD ''
generated from {option}`${opt.settings}`
'';
};
};
config = lib.mkIf cfg.enable {
users.users = lib.mkIf (cfg.user == "memos") {
${cfg.user} = {
description = lib.mkDefault "Memos service user";
isSystemUser = true;
group = cfg.group;
};
};
users.groups = lib.mkIf (cfg.group == "memos") {
${cfg.group} = { };
};
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [
cfg.port
];
systemd.tmpfiles.settings."10-memos" = {
"${cfg.dataDir}" = {
d = {
mode = "0750";
user = cfg.user;
group = cfg.group;
};
};
};
systemd.services.memos = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
wants = [ "network.target" ];
description = "Memos, a privacy-first, lightweight note-taking solution";
serviceConfig = {
User = cfg.user;
Group = cfg.group;
Type = "simple";
RestartSec = 60;
LimitNOFILE = 65536;
NoNewPrivileges = true;
LockPersonality = true;
RemoveIPC = true;
ReadWritePaths = [
cfg.dataDir
];
ProtectSystem = "strict";
PrivateUsers = true;
ProtectHome = true;
PrivateTmp = true;
PrivateDevices = true;
ProtectHostname = true;
ProtectClock = true;
UMask = "0077";
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectControlGroups = true;
ProtectProc = "invisible";
SystemCallFilter = [
" " # This is needed to clear the SystemCallFilter existing definitions
"~@reboot"
"~@swap"
"~@obsolete"
"~@mount"
"~@module"
"~@debug"
"~@cpu-emulation"
"~@clock"
"~@raw-io"
"~@privileged"
"~@resources"
];
CapabilityBoundingSet = [
" " # Reset all capabilities to an empty set
];
RestrictAddressFamilies = [
" " # This is needed to clear the RestrictAddressFamilies existing definitions
"none" # Remove all addresses families
"AF_UNIX"
"AF_INET"
"AF_INET6"
];
DevicePolicy = "closed";
ProtectKernelLogs = true;
SystemCallArchitectures = "native";
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
EnvironmentFile = cfg.environmentFile;
ExecStart = lib.getExe cfg.package;
};
};
};
meta.maintainers = [ lib.maintainers.M0ustach3 ];
}
|