Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
218 lines (201 loc) · 7.71 KB
/
Copy pathflake.nix
File metadata and controls
218 lines (201 loc) · 7.71 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
{
inputs = {
nixpkgs.url = "https://channels.nixos.org/nixpkgs-unstable/nixexprs.tar.xz";
};
outputs =
inputs:
let
version = "0.0.1-alpha";
# Vendored dependency-tree hash. Regenerate after changing dependencies:
# set `vendorHash = lib.fakeHash`, run `nix build`, and take the value the
# build reports, so a wrong hash fails the build rather than pulling network access.
vendorHash = "sha256-zpa8XTHdbJqCJsJYNJK1/ICCCgEvTJ32ecydaf7zEPg=";
nixpkgs = inputs.nixpkgs;
lib = nixpkgs.lib;
systems = [
"x86_64-linux"
"aarch64-linux"
"aarch64-darwin"
];
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system});
commit =
if
(builtins.hasAttr "self" inputs) && inputs.self != null && (builtins.hasAttr "rev" inputs.self)
then
inputs.self.rev
else
"dev";
# Only ship first-party Go code and its module files to the build.
# `builtins.filterSource` is unreliable here.
src =
let
repo = ./.;
in
lib.fileset.toSource {
root = repo;
fileset = lib.fileset.unions [
(lib.fileset.fromSource (repo + "/go.mod"))
(lib.fileset.fromSource (repo + "/go.sum"))
(lib.fileset.fromSource (repo + "/cmd"))
(lib.fileset.fromSource (repo + "/internal"))
];
};
buildPen =
pkgs:
pkgs.buildGoModule {
pname = "pen-bot";
version = version;
src = src;
subPackages = [ "cmd/pen-fun" ];
vendorHash = vendorHash;
ldflags = [
"-s"
"-w"
"-X"
"github.com/Neon-Genesis-Linux/pen-bot/internal/core.Version=${version}"
"-X"
"github.com/Neon-Genesis-Linux/pen-bot/internal/core.Commit=${commit}"
];
};
# NixOS deployment for the same binary as `packages.default`. DB_PATH
# points at a persistent StateDirectory, so the SQLite database stays in
# /var/lib and survives rebuilds, which change only the binary's path in
# the immutable store.
penBotModule =
{
config,
pkgs,
lib,
...
}:
let
cfg = config.services."pen-bot";
in
{
options.services."pen-bot" = {
enable = lib.mkEnableOption "Pen Bot Discord bot service";
botTokenFile = lib.mkOption {
type = lib.types.str;
default = "";
description = ''
Absolute path to a root-owned file containing the Discord bot
token in systemd `EnvironmentFile` format, i.e. a single line
`BOT_TOKEN=<token>`. systemd reads the file as root at
activation time and passes it straight to the service, so the
token never enters the world-readable Nix store. Required when
the service is enabled.
'';
example = "/etc/secrets/pen-bot/token-env";
};
guildID = lib.mkOption {
type = lib.types.str;
default = "";
description = "Discord guild ID to register commands against.";
};
};
config = {
systemd.services."pen-bot" = lib.mkIf cfg.enable (
lib.mkAssert (cfg.botTokenFile != "")
''
services."pen-bot".botTokenFile is required when the service is
enabled. Point it at a root-owned file containing the Discord bot
token in systemd `EnvironmentFile` format, i.e. a single line
`BOT_TOKEN=<token>`.
''
{
description = "Pen Bot Discord service";
wants = [ "network-online.target" ];
after = [
"network-online.target"
"local-fs.target"
];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = lib.getExe' (buildPen pkgs) "pen-fun";
Environment = [
"ENV=production"
# The database lives inside the service's StateDirectory
# (/var/lib/pen-bot): that is the only writable location a
# DynamicUser can reach, so its path is fixed rather than
# configurable.
"DB_PATH=/var/lib/pen-bot/pen-bot.db"
]
++ (if cfg.guildID != "" then [ "GUILD_ID=${cfg.guildID}" ] else [ ]);
# The bot reads BOT_TOKEN at runtime. systemd reads the file as
# root at activation time, so the token never enters the
# world-readable Nix store: pass the path directly rather than
# reading the file's contents into the derivation.
EnvironmentFile = lib.optionals (cfg.botTokenFile != "") [ cfg.botTokenFile ];
DynamicUser = true;
StateDirectory = "pen-bot";
StateDirectoryMode = "0750";
# No separate ReadWritePaths is needed: StateDirectory already
# makes /var/lib/pen-bot writable and owned by the (dynamic)
# user, and that is where the database lives.
Restart = "on-failure";
RestartSec = "5s";
# Sandboxing. DynamicUser already confines the service to an
# ephemeral, unprivileged namespace; these flags tighten it
# further. The qbittorrent service module lists the full set.
PrivateTmp = true;
ProtectSystem = "strict";
ProtectHome = true;
NoNewPrivileges = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
ProtectClock = true;
ProtectHostname = true;
ProtectProc = "invisible";
ProcSubset = "pid";
RestrictNamespaces = true;
RestrictRealtime = true;
RestrictSUIDSGID = true;
LockPersonality = true;
MemoryDenyWriteExecute = true;
# Keep only what a network client needs: IPv4/IPv6 sockets and
# the local sockets systemd/journald use.
RestrictAddressFamilies = [
"AF_INET"
"AF_INET6"
"AF_UNIX"
"AF_NETLINK"
];
# Only allow syscalls for the local architecture.
SystemCallArchitectures = "native";
CapabilityBoundingSet = "";
};
}
);
};
};
in
{
devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
packages = with pkgs; [
go_1_26
golangci-lint
gofumpt
gotestsum
just
hadolint
yamlfmt
dockerfmt
];
shellHook = ''
if [ ! -x ./custom-gcl ]; then
golangci-lint custom
fi
'';
};
});
packages = forAllSystems (pkgs: {
default = buildPen pkgs;
});
nixosModules = {
default = penBotModule;
};
};
}