|
| 1 | +{ |
| 2 | + config, |
| 3 | + lib, |
| 4 | + pkgs, |
| 5 | + ... |
| 6 | +}: |
| 7 | + |
| 8 | +let |
| 9 | + cfg = config.services.meshtasticd; |
| 10 | + format = pkgs.formats.yaml { }; |
| 11 | + configFile = format.generate "config.yaml" cfg.settings; |
| 12 | +in |
| 13 | +{ |
| 14 | + options.services.meshtasticd = { |
| 15 | + enable = lib.mkEnableOption "Meshtastic daemon"; |
| 16 | + package = lib.mkPackageOption pkgs "meshtasticd" { }; |
| 17 | + |
| 18 | + user = lib.mkOption { |
| 19 | + default = "meshtasticd"; |
| 20 | + description = "User meshtasticd runs as."; |
| 21 | + type = lib.types.str; |
| 22 | + }; |
| 23 | + |
| 24 | + group = lib.mkOption { |
| 25 | + default = "meshtasticd"; |
| 26 | + description = "Group meshtasticd runs as."; |
| 27 | + type = lib.types.str; |
| 28 | + }; |
| 29 | + |
| 30 | + port = lib.mkOption { |
| 31 | + type = lib.types.port; |
| 32 | + default = 4403; |
| 33 | + description = "Port to listen on"; |
| 34 | + }; |
| 35 | + |
| 36 | + settings = lib.mkOption { |
| 37 | + type = format.type; |
| 38 | + example = lib.literalExpression '' |
| 39 | + Lora = { |
| 40 | + Module = "auto"; |
| 41 | + }; |
| 42 | + Webserver = { |
| 43 | + Port = 9443; |
| 44 | + RootPath = pkgs.meshtastic-web; |
| 45 | + }; |
| 46 | + General = { |
| 47 | + MaxNodes = 200; |
| 48 | + MaxMessageQueue = 100; |
| 49 | + MACAddressSource = "eth0"; |
| 50 | + }; |
| 51 | + ''; |
| 52 | + description = '' |
| 53 | + The Meshtastic configuration file. |
| 54 | +
|
| 55 | + An example of configuration can be found at <https://github.com/meshtastic/firmware/blob/develop/bin/config-dist.yaml> |
| 56 | + ''; |
| 57 | + }; |
| 58 | + |
| 59 | + dataDir = lib.mkOption { |
| 60 | + default = "/var/lib/meshtasticd"; |
| 61 | + type = lib.types.path; |
| 62 | + description = '' |
| 63 | + The data directory. |
| 64 | + ''; |
| 65 | + }; |
| 66 | + }; |
| 67 | + |
| 68 | + config = lib.mkIf cfg.enable { |
| 69 | + # Creation of the `meshtasticd` privilege user. |
| 70 | + users = { |
| 71 | + users = lib.mkIf (cfg.user == "meshtasticd") { |
| 72 | + meshtasticd = { |
| 73 | + home = cfg.dataDir; |
| 74 | + description = "meshtasticd-daemon privilege user"; |
| 75 | + group = cfg.group; |
| 76 | + isSystemUser = true; |
| 77 | + extraGroups = [ |
| 78 | + "spi" |
| 79 | + "gpio" |
| 80 | + ]; |
| 81 | + }; |
| 82 | + }; |
| 83 | + groups = lib.mkIf (cfg.group == "meshtasticd") { |
| 84 | + meshtasticd = { }; |
| 85 | + # These groups are required for udev rules to work properly. |
| 86 | + spi = { }; |
| 87 | + gpio = { }; |
| 88 | + }; |
| 89 | + }; |
| 90 | + |
| 91 | + # The `meshtasticd` package provides udev rules. |
| 92 | + services.udev.packages = [ |
| 93 | + cfg.package |
| 94 | + ]; |
| 95 | + |
| 96 | + # Creation of the `meshtasticd` service. |
| 97 | + # Based on the official meshtasticd service file: https://github.com/meshtastic/firmware/blob/develop/bin/meshtasticd.service |
| 98 | + systemd.services.meshtasticd = { |
| 99 | + description = "Meshtastic Native Daemon"; |
| 100 | + after = [ |
| 101 | + "network-online.target" |
| 102 | + "network.target" |
| 103 | + ]; |
| 104 | + wants = [ |
| 105 | + "network-online.target" |
| 106 | + "network.target" |
| 107 | + ]; |
| 108 | + wantedBy = [ "multi-user.target" ]; |
| 109 | + |
| 110 | + serviceConfig = { |
| 111 | + User = cfg.user; |
| 112 | + Group = cfg.group; |
| 113 | + Type = "simple"; |
| 114 | + StateDirectory = "meshtasticd"; |
| 115 | + AmbientCapabilities = [ |
| 116 | + "CAP_NET_BIND_SERVICE" |
| 117 | + ]; |
| 118 | + ExecStart = "${lib.getExe cfg.package} --port=${builtins.toString cfg.port} --fsdir=${cfg.dataDir} --config=${configFile} --verbose"; |
| 119 | + Restart = "always"; |
| 120 | + RestartSec = "3"; |
| 121 | + }; |
| 122 | + }; |
| 123 | + }; |
| 124 | +} |
0 commit comments