8 cfg = config.services.bitmagnet;
23 inherit (lib.generators) toYAML;
25 freeformType = (pkgs.formats.yaml { }).type;
28 options.services.bitmagnet = {
29 enable = mkEnableOption "Bitmagnet service";
30 useLocalPostgresDB = mkOption {
31 description = "Use a local postgresql database, create user and database";
36 description = "Bitmagnet configuration (https://bitmagnet.io/setup/configuration.html).";
41 http_server = mkOption {
43 description = "HTTP server settings";
50 description = "HTTP server listen port";
55 dht_server = mkOption {
57 description = "DHT server settings";
64 description = "DHT listen port";
71 description = "PostgreSQL database configuration";
78 description = "Address, hostname or Unix socket path of the database server";
82 default = "bitmagnet";
83 description = "Database name to connect to";
88 description = "User to connect as";
93 description = "Password for database user";
101 package = mkPackageOption pkgs "bitmagnet" { };
103 description = "User running bitmagnet";
105 default = "bitmagnet";
108 description = "Group of user running bitmagnet";
110 default = "bitmagnet";
112 openFirewall = mkOption {
113 description = "Open DHT ports in firewall";
118 config = mkIf cfg.enable {
119 environment.etc."xdg/bitmagnet/config.yml" = {
120 text = toYAML { } cfg.settings;
125 systemd.services.bitmagnet = {
127 wantedBy = [ "multi-user.target" ];
130 ] ++ optional cfg.useLocalPostgresDB "postgresql.service";
131 requires = optional cfg.useLocalPostgresDB "postgresql.service";
137 ExecStart = "${cfg.package}/bin/bitmagnet worker run --all";
138 Restart = "on-failure";
139 WorkingDirectory = "/var/lib/bitmagnet";
140 StateDirectory = "bitmagnet";
142 # Sandboxing (sorted by occurrence in https://www.freedesktop.org/software/systemd/man/systemd.exec.html)
143 ProtectSystem = "strict";
146 PrivateDevices = true;
147 ProtectHostname = true;
149 ProtectKernelTunables = true;
150 ProtectKernelModules = true;
151 ProtectKernelLogs = true;
152 ProtectControlGroups = true;
153 RestrictAddressFamilies = [
158 RestrictNamespaces = true;
159 LockPersonality = true;
160 MemoryDenyWriteExecute = true;
161 RestrictRealtime = true;
162 RestrictSUIDSGID = true;
164 PrivateMounts = true;
167 users.users = mkIf (cfg.user == "bitmagnet") {
173 users.groups = mkIf (cfg.group == "bitmagnet") { bitmagnet = { }; };
174 networking.firewall = mkIf cfg.openFirewall {
175 allowedTCPPorts = [ cfg.settings.dht_server.port ];
176 allowedUDPPorts = [ cfg.settings.dht_server.port ];
178 services.postgresql = mkIf cfg.useLocalPostgresDB {
181 cfg.settings.postgres.name
182 (if (cfg.settings.postgres.user == "") then cfg.user else cfg.settings.postgres.user)
186 name = if (cfg.settings.postgres.user == "") then cfg.user else cfg.settings.postgres.user;
187 ensureDBOwnership = true;
193 meta.maintainers = with lib.maintainers; [ gileri ];