-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathmodule.nix
107 lines (105 loc) · 2.28 KB
/
module.nix
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
{
pkgs,
config,
lib,
...
}:
let
service_name = "fauxmo";
cfg = config.services.${service_name};
in
with lib;
{
options.services.${service_name} = {
enable = mkEnableOption service_name;
user = mkOption {
type = types.str;
description = "User that fauxmo will run as";
default = "fauxmo";
};
configFile = mkOption {
type = types.str;
description = "Path to config.json";
};
verbosity = mkOption {
type = types.enum [
0
1
2
3
];
default = 0;
};
openFirewall = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc "Whether to open TCP ports in the firewall";
};
tcpPortRange = mkOption {
description = "TCP range to open for fauxmo";
type =
with types;
submodule {
options = {
from = mkOption {
type = int;
};
to = mkOption {
type = int;
};
};
};
};
};
config = mkIf cfg.enable {
networking.firewall = mkIf cfg.openFirewall {
allowedUDPPorts = [
1900
];
allowedTCPPortRanges = [
{
inherit (cfg.tcpPortRange) to from;
}
];
};
systemd.services.${service_name} =
let
pythonWithFauxmo = pkgs.python3.withPackages (
ps: with ps; [
fauxmo
uvloop
]
);
after = [ "network-online.target" ];
in
{
description = service_name;
inherit after;
requires = after;
script =
let
verbosity =
if cfg.verbosity == 0 then
""
else
"-" + lib.concatStrings (builtins.genList (_: "v") cfg.verbosity);
in
''${pythonWithFauxmo}/bin/python -m fauxmo.cli ${verbosity} -c "${cfg.configFile}"'';
serviceConfig = {
User = cfg.user;
Restart = "on-failure";
RestartSec = 30;
};
wantedBy = [ "multi-user.target" ];
};
users = mkIf (cfg.user == "fauxmo") {
users = {
fauxmo = {
isSystemUser = true;
group = "fauxmo";
};
};
groups.fauxmo = { };
};
};
}