-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule.nix
More file actions
162 lines (147 loc) · 4.11 KB
/
Copy pathmodule.nix
File metadata and controls
162 lines (147 loc) · 4.11 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
{
config,
lib,
pkgs,
...
}:
let
inherit (lib)
getExe
literalExpression
mkEnableOption
mkIf
mkOption
types
;
cfg = config.services.custos;
toml = pkgs.formats.toml { };
defaultConfig = {
mode = "enforce";
policy_path = "/etc/custos/policy.toml";
socket_path = "/run/custos/control.sock";
sysfs_root = "/sys";
unsafe_allow_empty_policy = false;
default_action = "block";
controllers = {
authorized_default = "none";
restore_on_shutdown = false;
};
};
defaultPolicy = {
default = "block";
rules = [ ];
};
socketPath = cfg.config.socket_path or defaultConfig.socket_path;
sysfsRoot = cfg.config.sysfs_root or defaultConfig.sysfs_root;
socketDir = dirOf socketPath;
usbDevicesPath = "${sysfsRoot}/bus/usb/devices";
in
{
options.services.custos = {
enable = mkEnableOption "the custos USB authorization daemon";
package = mkOption {
type = types.package;
default = pkgs.callPackage ./package.nix { };
defaultText = literalExpression "pkgs.callPackage ./nix/package.nix { }";
description = "custos package to run.";
};
config = mkOption {
type = toml.type;
default = defaultConfig;
description = ''
Custos daemon configuration written directly to
/etc/custos/config.toml. Use the same snake_case field names as the
TOML file.
'';
example = literalExpression ''
{
mode = "enforce";
policy_path = "/etc/custos/policy.toml";
socket_path = "/run/custos/control.sock";
sysfs_root = "/sys";
unsafe_allow_empty_policy = false;
default_action = "block";
controllers = {
authorized_default = "none";
restore_on_shutdown = false;
};
}
'';
};
policy = mkOption {
type = toml.type;
default = defaultPolicy;
description = ''
Custos policy written directly to /etc/custos/policy.toml. Use the
same snake_case field names as the TOML file.
'';
example = literalExpression ''
{
default = "block";
rules = [
{
name = "built-in keyboard";
action = "allow";
match = {
vendor_id = "feed";
product_id = "1307";
connect_type = "hardwired";
is_hub = false;
interfaces.any = [ "03:*:*" ];
};
}
];
}
'';
};
group = mkOption {
type = types.str;
default = "custos";
description = "Group allowed to connect to the control socket.";
};
controlUsers = mkOption {
type = types.listOf types.str;
default = [ ];
description = ''
User accounts that should be added to the custos control group so they
can run the CLI against the system daemon without root.
'';
example = [
"alice"
];
};
};
config = mkIf cfg.enable {
users.groups.${cfg.group}.members = cfg.controlUsers;
environment.systemPackages = [ cfg.package ];
environment.etc = {
"custos/config.toml".source = toml.generate "custos-config.toml" cfg.config;
"custos/policy.toml".source = toml.generate "custos-policy.toml" cfg.policy;
};
systemd.tmpfiles.rules = [
"d ${socketDir} 2770 root ${cfg.group} - -"
];
systemd.services.custos = {
description = "Custos USB authorization daemon";
wantedBy = [ "multi-user.target" ];
after = [
"systemd-tmpfiles-setup.service"
"systemd-udevd.service"
];
serviceConfig = {
Type = "simple";
ExecStartPre = "${pkgs.coreutils}/bin/install -d -m 2770 -o root -g ${cfg.group} ${socketDir}";
ExecStart = "${getExe cfg.package} --daemon --config /etc/custos/config.toml";
Restart = "on-failure";
RestartSec = "1s";
PrivateTmp = true;
ProtectHome = true;
ProtectSystem = "strict";
ReadWritePaths = [
socketDir
usbDevicesPath
];
};
};
};
}