-
Notifications
You must be signed in to change notification settings - Fork 13
/
module.nix
165 lines (146 loc) · 4.17 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
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
163
164
165
{ lib, config, pkgs, ... }:
let
cfg = config.services.peerix;
in
{
options = with lib; {
services.peerix = {
enable = lib.mkEnableOption "peerix";
openFirewall = lib.mkOption {
type = types.bool;
default = true;
description = ''
Defines whether or not firewall ports should be opened for it.
'';
};
privateKeyFile = lib.mkOption {
type = types.nullOr types.path;
default = null;
description = ''
File containing the private key to sign the derivations with.
'';
};
publicKeyFile = lib.mkOption {
type = types.nullOr types.path;
default = null;
description = ''
File containing the public key to sign the derivations with.
'';
};
publicKey = lib.mkOption {
type = types.nullOr types.str;
default = null;
description = ''
The public key to sign the derivations with.
'';
};
user = lib.mkOption {
type = with types; oneOf [ str int ];
default = "nobody";
description = ''
The user the service will use.
'';
};
group = lib.mkOption {
type = with types; oneOf [ str int ];
default = "nobody";
description = ''
The user the service will use.
'';
};
globalCacheTTL = lib.mkOption {
type = types.nullOr types.int;
default = null;
description = ''
How long should nix store narinfo files.
If not defined, the module will not reconfigure the entry.
If it is defined, this will define how many seconds a cache entry will
be stored.
By default not given, as it affects the UX of the nix installation.
'';
};
package = mkOption {
type = types.package;
default = (import ./default.nix).default or pkgs.peerix;
defaultText = literalExpression "pkgs.peerix";
description = "The package to use for peerix";
};
};
};
config = lib.mkIf (cfg.enable) {
systemd.services.peerix = {
enable = true;
description = "Local p2p nix caching daemon";
wantedBy = ["multi-user.target"];
serviceConfig = {
Type = "simple";
User = cfg.user;
Group = cfg.group;
PrivateMounts = true;
PrivateDevices = true;
PrivateTmp = true;
PrivateIPC = true;
PrivateUsers = true;
SystemCallFilters = [
"@aio"
"@basic-io"
"@file-system"
"@io-event"
"@process"
"@network-io"
"@timer"
"@signal"
"@alarm"
];
SystemCallErrorNumber = "EPERM";
ProtectSystem = "full";
ProtectHome = true;
ProtectHostname = true;
ProtectClock = true;
ProtectKernelTunables = true;
ProtectKernelModules = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
RestrictNamespaces = "";
NoNewPrivileges = true;
ReadOnlyPaths = lib.mkMerge [
([
"/nix/var"
"/nix/store"
])
(lib.mkIf (cfg.privateKeyFile != null) [
cfg.privateKeyFile
])
];
ExecPaths = [
"/nix/store"
];
Environment = lib.mkIf (cfg.privateKeyFile != null) [
"NIX_SECRET_KEY_FILE=${cfg.privateKeyFile}"
];
};
script = ''
exec ${cfg.package}/bin/peerix
'';
};
nix = {
settings = {
substituters = [
"http://127.0.0.1:12304/"
];
trusted-public-keys = [
(lib.mkIf (cfg.publicKeyFile != null) (builtins.readFile cfg.publicKeyFile))
(lib.mkIf (cfg.publicKey != null) cfg.publicKey)
];
};
extraOptions = lib.mkIf (cfg.globalCacheTTL != null) ''
narinfo-cache-negative-ttl = ${toString cfg.globalCacheTTL}
narinfo-cache-positive-ttl = ${toString cfg.globalCacheTTL}
'';
};
networking.firewall = lib.mkIf (cfg.openFirewall) {
allowedTCPPorts = [ 12304 ];
allowedUDPPorts = [ 12304 ];
};
};
}