1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.mame;
7 mame = "mame${lib.optionalString pkgs.stdenv.is64bit "64"}";
8in
9{
10 options = {
11 services.mame = {
12 enable = mkOption {
13 type = types.bool;
14 default = false;
15 description = lib.mdDoc ''
16 Whether to setup TUN/TAP Ethernet interface for MAME emulator.
17 '';
18 };
19 user = mkOption {
20 type = types.str;
21 description = lib.mdDoc ''
22 User from which you run MAME binary.
23 '';
24 };
25 hostAddr = mkOption {
26 type = types.str;
27 description = lib.mdDoc ''
28 IP address of the host system. Usually an address of the main network
29 adapter or the adapter through which you get an internet connection.
30 '';
31 example = "192.168.31.156";
32 };
33 emuAddr = mkOption {
34 type = types.str;
35 description = lib.mdDoc ''
36 IP address of the guest system. The same you set inside guest OS under
37 MAME. Should be on the same subnet as {option}`services.mame.hostAddr`.
38 '';
39 example = "192.168.31.155";
40 };
41 };
42 };
43
44 config = mkIf cfg.enable {
45 environment.systemPackages = [ pkgs.mame ];
46
47 security.wrappers."${mame}" = {
48 owner = "root";
49 group = "root";
50 capabilities = "cap_net_admin,cap_net_raw+eip";
51 source = "${pkgs.mame}/bin/${mame}";
52 };
53
54 systemd.services.mame = {
55 description = "MAME TUN/TAP Ethernet interface";
56 after = [ "network.target" ];
57 wantedBy = [ "multi-user.target" ];
58 path = [ pkgs.iproute2 ];
59 serviceConfig = {
60 Type = "oneshot";
61 RemainAfterExit = true;
62 ExecStart = "${pkgs.mame}/bin/taputil.sh -c ${cfg.user} ${cfg.emuAddr} ${cfg.hostAddr} -";
63 ExecStop = "${pkgs.mame}/bin/taputil.sh -d ${cfg.user}";
64 };
65 };
66 };
67
68 meta.maintainers = with lib.maintainers; [ ];
69}