-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmodule.nix
More file actions
117 lines (103 loc) · 3.15 KB
/
module.nix
File metadata and controls
117 lines (103 loc) · 3.15 KB
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
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.penny;
settingsFormat = pkgs.formats.toml { };
configFile = settingsFormat.generate "penny.toml" cfg.settings;
in
{
options.services.penny = {
enable = lib.mkEnableOption "penny, a serverless reverse proxy for personal servers";
package = lib.mkPackageOption pkgs "penny" { };
address = lib.mkOption {
type = lib.types.str;
default = "0.0.0.0:80";
description = "The HTTP address to bind to.";
};
httpsAddress = lib.mkOption {
type = lib.types.str;
default = "0.0.0.0:443";
description = "The HTTPS address to bind to.";
};
environmentFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
Path to an environment file loaded by the service.
Useful for setting secrets like `PENNY_PASSWORD` without
exposing them in the Nix store.
'';
example = "/run/secrets/penny.env";
};
dataDir = lib.mkOption {
type = lib.types.path;
default = "/var/lib/penny";
description = "Directory for penny's state (database, certificates).";
};
settings = lib.mkOption {
type = lib.types.submodule {
freeformType = settingsFormat.type;
options = {
database_url = lib.mkOption {
type = lib.types.str;
default = "sqlite://penny.db";
description = "Database URL for penny's SQLite database.";
};
};
};
default = { };
description = ''
Configuration for penny in Nix attribute set form.
This will be serialized to TOML and written to penny.toml.
See <https://github.com/frectonz/penny> for available options.
'';
};
};
config = lib.mkIf cfg.enable {
systemd.services.penny = {
description = "Penny - Serverless reverse proxy";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
environment = {
RUST_LOG = "tracing=info,penny=info";
};
serviceConfig = {
Type = "exec";
DynamicUser = true;
StateDirectory = "penny";
WorkingDirectory = cfg.dataDir;
ExecStart =
let
args = lib.cli.toCommandLineShellGNU { } {
address = cfg.address;
https-address = cfg.httpsAddress;
};
in
"${lib.getExe cfg.package} serve ${configFile} ${args}";
Restart = "on-failure";
RestartSec = 5;
# Capabilities for binding to privileged ports
AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
# Hardening
NoNewPrivileges = true;
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
PrivateDevices = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectControlGroups = true;
RestrictSUIDSGID = true;
ReadWritePaths = [ cfg.dataDir ];
}
// lib.optionalAttrs (cfg.environmentFile != null) {
EnvironmentFile = cfg.environmentFile;
};
};
};
}