-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.nix
152 lines (145 loc) · 4.74 KB
/
service.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
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
{ nixless-agent, system-switch-tracker }:
{ lib, config, ... }:
let
cfg = config.services.nixless-agent;
in
{
options = {
services.nixless-agent = {
enable = lib.mkOption {
description = ''
Whether to enable nixless-agent.
'';
type = lib.types.bool;
default = false;
};
package = lib.mkOption {
description = ''
The package to use.
'';
type = lib.types.package;
default = nixless-agent;
};
user = lib.mkOption {
description = ''
The user under which nixless-agent runs.
'';
type = lib.types.str;
default = "nixless-agent";
};
group = lib.mkOption {
description = ''
The group under which attic runs.
'';
type = lib.types.str;
default = "nixless-agent";
};
port = lib.mkOption {
description = ''
The port on which nixless-agent will listen for requests.
'';
type = lib.types.port;
default = 45567;
};
telemetryPort = lib.mkOption {
description = ''
The port on which nixless-agent will listen for telemetry requests.
'';
type = lib.types.port;
default = 56678;
};
cacheUrl = lib.mkOption {
description = ''
The URL of the binary cache to use when downloading a system configuration.
'';
type = lib.types.str;
};
cachePublicKey = lib.mkOption {
description = ''
The public key of the binary cache.
'';
type = lib.types.str;
};
updatePublicKey = lib.mkOption {
description = ''
The public key to use when verifying requests made to update the system.
'';
type = lib.types.str;
};
maxSystemHistoryCount = lib.mkOption {
description = ''
How many configurations the agent will keep in the machine (for rollbacks, for example).
'';
type = lib.types.ints.positive;
default = 3;
};
};
};
config = lib.mkIf (cfg.enable)
{
assertions = [ ];
security.polkit = {
enable = true;
extraConfig = ''
polkit.addRule(function(action, subject) {
if (action.id == "org.freedesktop.systemd1.manage-units" && subject.user == "nixless-agent") {
if (action.lookup("unit") === undefined && action.lookup("verb") === undefined) {
return polkit.Result.YES;
}
}
});
'';
};
users.users = lib.optionalAttrs (cfg.user == "nixless-agent") {
nixless-agent = {
group = cfg.group;
isSystemUser = true;
};
};
users.groups = lib.optionalAttrs (cfg.group == "nixless-agent") {
nixless-agent.members = [ cfg.user ];
};
systemd.services.nixless-agent = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
environment = {
NIXLESS_AGENT_LISTEN_PORT = builtins.toString cfg.port;
NIXLESS_AGENT_TELEMETRY_LISTEN_PORT = builtins.toString cfg.telemetryPort;
NIXLESS_AGENT_TEMP_DOWNLOAD_PATH = "/var/lib/nixless-agent/downloads";
NIXLESS_AGENT_CACHE_URL = cfg.cacheUrl;
NIXLESS_AGENT_ABSOLUTE_ACTIVATION_TRACKER_COMMAND = lib.getExe system-switch-tracker;
NIXLESS_AGENT_CACHE_PUBLIC_KEY = cfg.cachePublicKey;
NIXLESS_AGENT_UPDATE_PUBLIC_KEY = cfg.updatePublicKey;
NIXLESS_MAX_SYSTEM_HISTORY_COUNT = builtins.toString cfg.maxSystemHistoryCount;
RUST_BACKTRACE = "full";
};
serviceConfig = {
Type = "notify";
NotifyAccess = "main";
ExecStart = lib.getExe cfg.package;
CapabilityBoundingSet = "CAP_SYS_ADMIN CAP_CHOWN CAP_SETPCAP CAP_FOWNER";
AmbientCapabilities = "CAP_SYS_ADMIN CAP_CHOWN CAP_SETPCAP CAP_FOWNER";
StateDirectory = "nixless-agent";
DynamicUser = false;
User = cfg.user;
Group = cfg.group;
ProtectHome = true;
ProtectHostname = true;
ProtectKernelLogs = true;
ProtectKernelModules = true;
ProtectKernelTunables = true;
ProtectProc = "default"; # Required so nixless-agent can check whether the nix daemon is running.
ProcSubset = "pid";
ProtectSystem = "strict";
ReadWritePaths = "/nix";
# Restart = "on-failure";
Restart = "no";
RestartSec = 10;
RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ]; # AF_UNIX is used by D-Bus.
RestrictNamespaces = "mnt";
RestrictRealtime = true;
RestrictSUIDSGID = true;
};
};
};
}