-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflake.nix
More file actions
236 lines (203 loc) · 7.96 KB
/
flake.nix
File metadata and controls
236 lines (203 loc) · 7.96 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
{
description = "Meshstellar - A Meshtastic MQTT client and web interface";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
};
outputs = { self, nixpkgs }:
let
systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
forAllSystems = f: nixpkgs.lib.genAttrs systems (system: f nixpkgs.legacyPackages.${system});
in
{
packages = forAllSystems (pkgs:
let
protobufs = pkgs.fetchgit {
url = "https://github.com/meshtastic/protobufs.git";
rev = "4c4427c4a73c86fed7dc8632188bb8be95349d81";
hash = "sha256-t4LmJI4+sPD6t4gfNy0gqVnehpvim0rbqVARcMCpgAQ=";
};
in {
default = pkgs.rustPlatform.buildRustPackage {
pname = "meshstellar";
version = "0.1.0";
src = pkgs.lib.cleanSource ./.;
cargoLock.lockFile = ./Cargo.lock;
nativeBuildInputs = [ pkgs.protobuf pkgs.git pkgs.sqlx-cli ];
postUnpack = ''
rm -rf $sourceRoot/protobufs
cp -r ${protobufs} $sourceRoot/protobufs
'';
preBuild = ''
export DATABASE_URL="sqlite:$TMPDIR/meshstellar.db?mode=rwc"
sqlx database create
sqlx migrate run
cargo sqlx prepare
'';
env = {
PROTOC = "${pkgs.protobuf}/bin/protoc";
SQLX_OFFLINE = "true";
};
};
});
devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
buildInputs = [
pkgs.cargo
pkgs.rustc
pkgs.protobuf
pkgs.sqlx-cli
];
PROTOC = "${pkgs.protobuf}/bin/protoc";
SQLX_OFFLINE = "true";
};
});
} // {
nixosModules.default = { config, lib, pkgs, ... }:
let
cfg = config.services.meshstellar;
settingsFormat = pkgs.formats.toml {};
configFile = settingsFormat.generate "meshstellar.toml" cfg.settings;
in {
options.services.meshstellar = {
enable = lib.mkEnableOption "Meshstellar Meshtastic web interface";
package = lib.mkOption {
type = lib.types.package;
default = self.packages.${pkgs.system}.default;
description = "The meshstellar package to use";
};
settings = lib.mkOption {
type = lib.types.submodule {
freeformType = settingsFormat.type;
options = {
http_addr = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1:3000";
description = "HTTP server address and port";
};
mqtt_auth = lib.mkOption {
type = lib.types.bool;
default = true;
description = "Enable MQTT authentication";
};
mqtt_username = lib.mkOption {
type = lib.types.str;
default = "username";
description = "MQTT username";
};
mqtt_password = lib.mkOption {
type = lib.types.str;
default = "password";
description = "MQTT password. Warning: visible in nix store. Prefer using a secrets manager.";
};
mqtt_host = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1";
description = "MQTT broker host";
};
mqtt_port = lib.mkOption {
type = lib.types.port;
default = 1883;
description = "MQTT broker port";
};
mqtt_keep_alive = lib.mkOption {
type = lib.types.int;
default = 15;
description = "MQTT keep-alive interval in seconds";
};
mqtt_client_id = lib.mkOption {
type = lib.types.str;
default = "meshstellar";
description = "MQTT client ID";
};
mqtt_topic = lib.mkOption {
type = lib.types.str;
default = "meshtastic/#";
description = "MQTT topic to subscribe to";
};
database_url = lib.mkOption {
type = lib.types.str;
default = "sqlite:///var/lib/meshstellar/meshstellar.db?mode=rwc";
description = "SQLite database URL";
};
map_glyphs_url = lib.mkOption {
type = lib.types.str;
default = "https://protomaps.github.io/basemaps-assets/fonts/{fontstack}/{range}.pbf";
description = "URL for map font glyphs";
};
open_browser = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Open browser on startup";
};
hide_private_messages = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Hide private messages from the interface";
};
};
};
default = {};
description = "Meshstellar configuration options. See meshstellar.toml.example for reference.";
};
dataDir = lib.mkOption {
type = lib.types.path;
default = "/var/lib/meshstellar";
description = "Directory to store database and data files";
};
user = lib.mkOption {
type = lib.types.str;
default = "meshstellar";
description = "User to run the service as";
};
group = lib.mkOption {
type = lib.types.str;
default = "meshstellar";
description = "Group to run the service as";
};
environmentFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
description = ''
Environment file to pass secrets to the service.
Variables with MESHSTELLAR_ prefix override config settings.
Example: MESHSTELLAR_MQTT_PASSWORD=secret
'';
};
};
config = lib.mkIf cfg.enable {
users.users.${cfg.user} = {
isSystemUser = true;
group = cfg.group;
home = cfg.dataDir;
createHome = true;
};
users.groups.${cfg.group} = {};
# Place config in /etc/meshstellar/meshstellar.toml
environment.etc."meshstellar/meshstellar.toml".source = configFile;
systemd.services.meshstellar = {
description = "Meshstellar Meshtastic Web Interface";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
restartTriggers = [ configFile ] ++ lib.optional (cfg.environmentFile != null) cfg.environmentFile;
serviceConfig = {
Type = "simple";
User = cfg.user;
Group = cfg.group;
WorkingDirectory = cfg.dataDir;
ExecStart = "${cfg.package}/bin/meshstellar";
Restart = "always";
RestartSec = 5;
# Hardening
NoNewPrivileges = true;
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
ReadWritePaths = [ cfg.dataDir ];
} // lib.optionalAttrs (cfg.environmentFile != null) {
EnvironmentFile = cfg.environmentFile;
};
};
};
};
};
}