-
Notifications
You must be signed in to change notification settings - Fork 4
/
modules.nix
161 lines (148 loc) · 6.08 KB
/
modules.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
{ outputs, nixpkgs-matrix, system, ... }:
{
polykey = { config, ... }:
with nixpkgs-matrix.lib; {
options = {
services.polykey = {
enable = mkEnableOption
"Enable the Polykey agent. Users with the `polykey` group or root permissions will be able to manage the agent.";
passwordFilePath = mkOption {
type = with types; uniq str;
description = ''
The path to the Polykey password file. This is required to be set for the module to work, otherwise this module will fail.
'';
};
recoveryCodeFilePath = mkOption {
type = with types; uniq str;
default = "";
description = ''
The path to the Polykey recovery code file. This is not required, but if set will read a recovery code from the provided path to bootstrap a new state with.
'';
};
recoveryCodeOutPath = mkOption {
type = with types; uniq str;
description = ''
The path to the Polykey recovery code file output location.
'';
};
statePath = mkOption {
type = with types; uniq str;
default = "/var/lib/polykey";
description =
"The path to the Polykey node state directory. Will default to `/var/lib/polykey`, but can be overwritten to a custom path.";
};
};
};
config = mkIf config.services.polykey.enable {
users.groups.polykey = { };
environment.systemPackages = [ outputs.packages.${system}.default ];
system.activationScripts.makeAgentPaths = ''
mkdir -p ${config.services.polykey.statePath}
chgrp -R polykey ${config.services.polykey.statePath}
chmod 770 ${config.services.polykey.statePath}
'';
systemd.services.polykey = {
description = "Polykey Agent";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
User = "root";
Group = "polykey";
PermissionsStartOnly = true;
LoadCredential =
[ "password:${config.services.polykey.passwordFilePath}" ];
ExecStartPre = ''
-${outputs.packages.${system}.default}/bin/polykey \
--password-file ''${CREDENTIALS_DIRECTORY}/password \
--node-path ${config.services.polykey.statePath} \
bootstrap ${
optionalString
(config.services.polykey.recoveryCodeFilePath != "")
"-rcf ${config.services.polykey.recoveryCodeFilePath}"
}\
--recovery-code-out-file ${config.services.polykey.recoveryCodeOutPath}
'';
ExecStart = ''
${outputs.packages.${system}.default}/bin/polykey \
--password-file ''${CREDENTIALS_DIRECTORY}/password \
--node-path ${config.services.polykey.statePath} \
agent start \
--recovery-code-out-file ${config.services.polykey.recoveryCodeOutPath}
'';
Restart = "always";
RestartSec = "1s";
StartLimitIntervalSec = "0";
StartLimitBurst = "0";
};
};
};
};
polykey-home = { config, ... }:
with nixpkgs-matrix.lib; {
options = {
programs.polykey = {
enable = mkEnableOption "Enable the user-space Polykey agent.";
passwordFilePath = mkOption {
type = with types; uniq str;
description = ''
The path to the Polykey password file. This is required to be set for the module to work, otherwise this module will fail.
'';
};
recoveryCodeFilePath = mkOption {
type = with types; uniq str;
default = "";
description = ''
The path to the Polykey recovery code file. This is not required, but if set will read a recovery code from the provided path to bootstrap a new state with.
'';
};
recoveryCodeOutPath = mkOption {
type = with types; uniq str;
description = ''
The path to the Polykey recovery code file output location.
'';
};
statePath = mkOption {
type = with types; uniq str;
default = "%h/.local/share/polykey";
description =
"The path to the Polykey node state directory. Will default to `$HOME/.local/share/polykey`, but can be overwritten to a custom path.";
};
};
};
config = mkIf config.programs.polykey.enable {
home.packages = [ outputs.packages.${system}.default ];
systemd.user.services.polykey = {
Unit = {
Description = "Polykey Agent";
After = [ "default.target" "network.target" ];
Wants = [ "network.target" ];
StartLimitIntervalSec = "0";
StartLimitBurst = "0";
};
Service = {
ExecStartPre = ''
-${outputs.packages.${system}.default}/bin/polykey \
--password-file ${config.programs.polykey.passwordFilePath} \
--node-path ${config.programs.polykey.statePath} \
bootstrap ${
optionalString
(config.programs.polykey.recoveryCodeFilePath != "")
"-rcf ${config.programs.polykey.recoveryCodeFilePath}"
}\
--recovery-code-out-file ${config.programs.polykey.recoveryCodeOutPath}
'';
ExecStart = ''
${outputs.packages.${system}.default}/bin/polykey \
--password-file ${config.programs.polykey.passwordFilePath} \
--node-path ${config.programs.polykey.statePath} \
agent start \
--recovery-code-out-file ${config.programs.polykey.recoveryCodeOutPath}
'';
Restart = "always";
RestartSec = "1s";
};
Install = { WantedBy = [ "default.target" ]; };
};
};
};
}