@@ -0,0 +1,67 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.mame;
7+ mame = "mame${lib.optionalString pkgs.stdenv.is64bit "64"}";
8+in
9+{
10+ options = {
11+ services.mame = {
12+ enable = mkOption {
13+ type = types.bool;
14+ default = false;
15+ description = ''
16+ Whether to setup TUN/TAP Ethernet interface for MAME emulator.
17+ '';
18+ };
19+ user = mkOption {
20+ type = types.str;
21+ description = ''
22+ User from which you run MAME binary.
23+ '';
24+ };
25+ hostAddr = mkOption {
26+ type = types.str;
27+ description = ''
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 = ''
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</option>.
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+ source = "${pkgs.mame}/bin/${mame}";
49+ capabilities = "cap_net_admin,cap_net_raw+eip";
50+ };
51+
52+ systemd.services.mame = {
53+ description = "MAME TUN/TAP Ethernet interface";
54+ after = [ "network.target" ];
55+ wantedBy = [ "multi-user.target" ];
56+ path = [ pkgs.iproute ];
57+ serviceConfig = {
58+ Type = "oneshot";
59+ RemainAfterExit = true;
60+ ExecStart = "${pkgs.mame}/bin/taputil.sh -c ${cfg.user} ${cfg.emuAddr} ${cfg.hostAddr} -";
61+ ExecStop = "${pkgs.mame}/bin/taputil.sh -d ${cfg.user}";
62+ };
63+ };
64+ };
65+
66+ meta.maintainers = with lib.maintainers; [ gnidorah ];
67+}