7 cfg = config.services.matter-server;
8 storageDir = "matter-server";
9 storagePath = "/var/lib/${storageDir}";
10 vendorId = "4939"; # home-assistant vendor ID
14 meta.maintainers = with lib.maintainers; [ leonm1 ];
16 options.services.matter-server = with lib.types; {
17 enable = lib.mkEnableOption "Matter-server";
19 package = lib.mkPackageOption pkgs "python-matter-server" { };
22 type = lib.types.port;
24 description = "Port to expose the matter-server service on.";
27 logLevel = lib.mkOption {
28 type = lib.types.enum [ "critical" "error" "warning" "info" "debug" ];
30 description = "Verbosity of logs from the matter-server";
33 extraArgs = lib.mkOption {
37 Extra arguments to pass to the matter-server executable.
38 See https://github.com/home-assistant-libs/python-matter-server?tab=readme-ov-file#running-the-development-server for options.
43 config = lib.mkIf cfg.enable {
44 systemd.services.matter-server = {
45 after = [ "network-online.target" ];
46 before = [ "home-assistant.service" ];
47 wants = [ "network-online.target" ];
48 wantedBy = [ "multi-user.target" ];
49 description = "Matter Server";
50 environment.HOME = storagePath;
52 ExecStart = (lib.concatStringsSep " " [
53 "${cfg.package}/bin/matter-server"
54 "--port" (toString cfg.port)
56 "--storage-path" storagePath
57 "--log-level" "${cfg.logLevel}"
58 "${lib.escapeShellArgs cfg.extraArgs}"
60 # Start with a clean root filesystem, and allowlist what the container
61 # is permitted to access.
62 TemporaryFileSystem = "/";
63 # Allowlist /nix/store (to allow the binary to find its dependencies)
65 ReadOnlyPaths = "/nix/store /run/dbus";
66 # Let systemd manage `/var/lib/matter-server` for us inside the
67 # ephemeral TemporaryFileSystem.
68 StateDirectory = storageDir;
69 # `python-matter-server` writes to /data even when a storage-path is
70 # specified. This bind-mount points /data at the systemd-managed
71 # /var/lib/matter-server, so all files get dropped into the state
73 BindPaths = "${storagePath}:/data";
76 AmbientCapabilities = "";
77 CapabilityBoundingSet = "";
78 DevicePolicy = "closed";
80 LockPersonality = true;
81 MemoryDenyWriteExecute = true;
82 NoNewPrivileges = true;
83 PrivateDevices = true;
88 ProtectControlGroups = true;
90 ProtectHostname = true;
91 ProtectKernelLogs = true;
92 ProtectKernelModules = true;
93 ProtectKernelTunables = true;
94 ProtectProc = "invisible";
95 RestrictAddressFamilies = [
100 RestrictNamespaces = true;
101 RestrictRealtime = true;
102 RestrictSUIDSGID = true;
103 SystemCallFilter = lib.concatStringsSep " " [