summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/networking/meshtasticd.nix
blob: 3afa29a0c0b87e3b82c7bbfaa686eb19e2d0ad70 (plain)
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
{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.services.meshtasticd;
  format = pkgs.formats.yaml { };
  configFile = format.generate "config.yaml" cfg.settings;
in
{
  options.services.meshtasticd = {
    enable = lib.mkEnableOption "Meshtastic daemon";
    package = lib.mkPackageOption pkgs "meshtasticd" { };

    user = lib.mkOption {
      default = "meshtasticd";
      description = "User meshtasticd runs as.";
      type = lib.types.str;
    };

    group = lib.mkOption {
      default = "meshtasticd";
      description = "Group meshtasticd runs as.";
      type = lib.types.str;
    };

    port = lib.mkOption {
      type = lib.types.port;
      default = 4403;
      description = "Port to listen on";
    };

    settings = lib.mkOption {
      type = format.type;
      example = lib.literalExpression ''
        Lora = {
          Module = "auto";
        };
        Webserver = {
          Port = 9443;
          RootPath = pkgs.meshtastic-web;
        };
        General = {
          MaxNodes = 200;
          MaxMessageQueue = 100;
          MACAddressSource = "eth0";
        };
      '';
      description = ''
        The Meshtastic configuration file.

        An example of configuration can be found at <https://github.com/meshtastic/firmware/blob/develop/bin/config-dist.yaml>
      '';
    };

    dataDir = lib.mkOption {
      default = "/var/lib/meshtasticd";
      type = lib.types.path;
      description = ''
        The data directory.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    # Creation of the `meshtasticd` privilege user.
    users = {
      users = lib.mkIf (cfg.user == "meshtasticd") {
        meshtasticd = {
          home = cfg.dataDir;
          description = "meshtasticd-daemon privilege user";
          group = cfg.group;
          isSystemUser = true;
          extraGroups = [
            "spi"
            "gpio"
          ];
        };
      };
      groups = lib.mkIf (cfg.group == "meshtasticd") {
        meshtasticd = { };
        # These groups are required for udev rules to work properly.
        spi = { };
        gpio = { };
      };
    };

    # The `meshtasticd` package provides udev rules.
    services.udev.packages = [
      cfg.package
    ];

    # Creation of the `meshtasticd` service.
    # Based on the official meshtasticd service file: https://github.com/meshtastic/firmware/blob/develop/bin/meshtasticd.service
    systemd.services.meshtasticd = {
      description = "Meshtastic Native Daemon";
      after = [
        "network-online.target"
        "network.target"
      ];
      wants = [
        "network-online.target"
        "network.target"
      ];
      wantedBy = [ "multi-user.target" ];

      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
        Type = "simple";
        StateDirectory = "meshtasticd";
        AmbientCapabilities = [
          "CAP_NET_BIND_SERVICE"
        ];
        ExecStart = "${lib.getExe cfg.package} --port=${toString cfg.port} --fsdir=${cfg.dataDir} --config=${configFile} --verbose";
        Restart = "always";
        RestartSec = "3";
      };
    };
  };
}