Skip to content

Commit bf23802

Browse files
author
Brynley Llewellyn-Roux
committed
feat: home-manager module implementation
1 parent ccedaf2 commit bf23802

3 files changed

Lines changed: 161 additions & 163 deletions

File tree

flake.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 10 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -141,164 +141,14 @@
141141
default = shell { ci = false; };
142142
ci = shell { ci = true; };
143143
};
144-
}) // {
145-
nixosModules.default = { config, ... }:
146-
with nixpkgs-matrix.lib; {
147-
options = {
148-
services.polykey = {
149-
enable = mkEnableOption
150-
"Enable the Polykey agent. Users with the `polykey` group or root permissions will be able to manage the agent.";
151-
152-
passwordFilePath = mkOption {
153-
type = with types; uniq str;
154-
description = ''
155-
The path to the Polykey password file. This is required to be set for the module to work, otherwise this module will fail.
156-
'';
157-
};
158-
159-
recoveryCodeFilePath = mkOption {
160-
type = with types; uniq str;
161-
default = "";
162-
description = ''
163-
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.
164-
'';
165-
};
166-
167-
recoveryCodeOutPath = mkOption {
168-
type = with types; uniq str;
169-
description = ''
170-
The path to the Polykey recovery code file output location.
171-
'';
172-
};
173-
174-
statePath = mkOption {
175-
type = with types; uniq str;
176-
default = "/var/lib/polykey";
177-
description =
178-
"The path to the Polykey node state directory. Will default to `/var/lib/polykey`, but can be overwritten to a custom path.";
179-
};
180-
};
181-
programs.polykey = {
182-
enable = mkEnableOption "Enable the per-user Polykey agent.";
183-
184-
passwordFilePath = mkOption {
185-
type = with types; uniq str;
186-
description = ''
187-
The path to the Polykey password file. This is required to be set for the module to work, otherwise this module will fail.
188-
'';
189-
};
190-
191-
recoveryCodeFilePath = mkOption {
192-
type = with types; uniq str;
193-
default = "";
194-
description = ''
195-
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.
196-
'';
197-
};
198-
199-
recoveryCodeOutPath = mkOption {
200-
type = with types; uniq str;
201-
description = ''
202-
The path to the Polykey recovery code file output location.
203-
'';
204-
};
205-
206-
statePath = mkOption {
207-
type = with types; uniq str;
208-
default = "%h/.local/share/polykey";
209-
description =
210-
"The path to the Polykey node state directory. Will default to `$HOME/.local/share/polykey`, but can be overwritten to a custom path.";
211-
};
212-
};
213-
};
214-
config = mkMerge [
215-
(mkIf config.services.polykey.enable {
216-
users.groups.polykey = { };
217-
218-
environment.systemPackages =
219-
[ self.outputs.packages.${buildSystem}.default ];
220-
221-
system.activationScripts.makeAgentPaths = ''
222-
mkdir -p ${config.services.polykey.statePath}
223-
chgrp -R polykey ${config.services.polykey.statePath}
224-
chmod 770 ${config.services.polykey.statePath}
225-
'';
226-
227-
systemd.services.polykey = {
228-
description = "Polykey Agent";
229-
wantedBy = [ "multi-user.target" ];
230-
after = [ "network.target" ];
231-
serviceConfig = {
232-
User = "root";
233-
Group = "polykey";
234-
PermissionsStartOnly = true;
235-
LoadCredential = [
236-
"password:${config.services.polykey.passwordFilePath}"
237-
];
238-
ExecStartPre = ''
239-
-${
240-
self.outputs.packages.${buildSystem}.default
241-
}/bin/polykey \
242-
--password-file ''${CREDENTIALS_DIRECTORY}/password \
243-
--node-path ${config.services.polykey.statePath} \
244-
bootstrap ${
245-
lib.optionalString
246-
(config.services.polykey.recoveryCodeFilePath != "")
247-
"-rcf ${config.services.polykey.recoveryCodeFilePath}"
248-
}\
249-
--recovery-code-out-file ${config.services.polykey.recoveryCodeOutPath}
250-
'';
251-
ExecStart = ''
252-
${
253-
self.outputs.packages.${buildSystem}.default
254-
}/bin/polykey \
255-
--password-file ''${CREDENTIALS_DIRECTORY}/password \
256-
--node-path ${config.services.polykey.statePath} \
257-
agent start \
258-
--recovery-code-out-file ${config.services.polykey.recoveryCodeOutPath}
259-
'';
260-
};
261-
};
262-
})
263-
(mkIf config.programs.polykey.enable {
264-
environment.systemPackages =
265-
[ self.outputs.packages.${buildSystem}.default ];
266-
267-
system.activationScripts.makeUserAgentPaths = ''
268-
mkdir -p ${config.programs.polykey.statePath}
269-
'';
270-
271-
systemd.user.services.polykey = {
272-
description = "Polykey Agent";
273-
wantedBy = [ "default.target" ];
274-
after = [ "network.target" ];
275-
serviceConfig = {
276-
ExecStartPre = ''
277-
-${
278-
self.outputs.packages.${buildSystem}.default
279-
}/bin/polykey \
280-
--password-file ${config.programs.polykey.passwordFilePath} \
281-
--node-path ${config.programs.polykey.statePath} \
282-
bootstrap ${
283-
lib.optionalString
284-
(config.programs.polykey.recoveryCodeFilePath != "")
285-
"-rcf ${config.programs.polykey.recoveryCodeFilePath}"
286-
}\
287-
--recovery-code-out-file ${config.programs.polykey.recoveryCodeOutPath}
288-
'';
289-
ExecStart = ''
290-
${
291-
self.outputs.packages.${buildSystem}.default
292-
}/bin/polykey \
293-
--password-file ${config.programs.polykey.passwordFilePath} \
294-
--node-path ${config.programs.polykey.statePath} \
295-
agent start \
296-
--recovery-code-out-file ${config.programs.polykey.recoveryCodeOutPath}
297-
'';
298-
};
299-
};
300-
})
301-
];
302-
};
303-
};
144+
}) // (let
145+
modules = import ./modules.nix {
146+
inherit nixpkgs-matrix;
147+
outputs = self.outputs;
148+
system = "x86_64-linux";
149+
};
150+
in {
151+
nixosModules.default = modules.polykey;
152+
homeModules.default = modules.polykey-home;
153+
});
304154
}

modules.nix

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
{ outputs, nixpkgs-matrix, system, ... }:
2+
3+
{
4+
polykey = { config, ... }:
5+
with nixpkgs-matrix.lib.${system}; {
6+
options = {
7+
services.polykey = {
8+
enable = mkEnableOption
9+
"Enable the Polykey agent. Users with the `polykey` group or root permissions will be able to manage the agent.";
10+
11+
passwordFilePath = mkOption {
12+
type = with types; uniq str;
13+
description = ''
14+
The path to the Polykey password file. This is required to be set for the module to work, otherwise this module will fail.
15+
'';
16+
};
17+
18+
recoveryCodeFilePath = mkOption {
19+
type = with types; uniq str;
20+
default = "";
21+
description = ''
22+
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.
23+
'';
24+
};
25+
26+
recoveryCodeOutPath = mkOption {
27+
type = with types; uniq str;
28+
description = ''
29+
The path to the Polykey recovery code file output location.
30+
'';
31+
};
32+
33+
statePath = mkOption {
34+
type = with types; uniq str;
35+
default = "/var/lib/polykey";
36+
description =
37+
"The path to the Polykey node state directory. Will default to `/var/lib/polykey`, but can be overwritten to a custom path.";
38+
};
39+
};
40+
};
41+
config = mkIf config.services.polykey.enable {
42+
users.groups.polykey = { };
43+
44+
environment.systemPackages = [ outputs.packages.${system}.default ];
45+
46+
system.activationScripts.makeAgentPaths = ''
47+
mkdir -p ${config.services.polykey.statePath}
48+
chgrp -R polykey ${config.services.polykey.statePath}
49+
chmod 770 ${config.services.polykey.statePath}
50+
'';
51+
52+
systemd.services.polykey = {
53+
description = "Polykey Agent";
54+
wantedBy = [ "multi-user.target" ];
55+
after = [ "network.target" ];
56+
serviceConfig = {
57+
User = "root";
58+
Group = "polykey";
59+
PermissionsStartOnly = true;
60+
LoadCredential =
61+
[ "password:${config.services.polykey.passwordFilePath}" ];
62+
ExecStartPre = ''
63+
-${outputs.packages.${system}.default}/bin/polykey \
64+
--password-file ''${CREDENTIALS_DIRECTORY}/password \
65+
--node-path ${config.services.polykey.statePath} \
66+
bootstrap ${
67+
optionalString
68+
(config.services.polykey.recoveryCodeFilePath != "")
69+
"-rcf ${config.services.polykey.recoveryCodeFilePath}"
70+
}\
71+
--recovery-code-out-file ${config.services.polykey.recoveryCodeOutPath}
72+
'';
73+
ExecStart = ''
74+
${outputs.packages.${system}.default}/bin/polykey \
75+
--password-file ''${CREDENTIALS_DIRECTORY}/password \
76+
--node-path ${config.services.polykey.statePath} \
77+
agent start \
78+
--recovery-code-out-file ${config.services.polykey.recoveryCodeOutPath}
79+
'';
80+
};
81+
};
82+
};
83+
};
84+
polykey-home = { config, ... }:
85+
with nixpkgs-matrix.lib.${system}; {
86+
options = {
87+
programs.polykey = {
88+
enable = mkEnableOption "Enable the user-space Polykey agent.";
89+
90+
passwordFilePath = mkOption {
91+
type = with types; uniq str;
92+
description = ''
93+
The path to the Polykey password file. This is required to be set for the module to work, otherwise this module will fail.
94+
'';
95+
};
96+
97+
recoveryCodeFilePath = mkOption {
98+
type = with types; uniq str;
99+
default = "";
100+
description = ''
101+
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.
102+
'';
103+
};
104+
105+
recoveryCodeOutPath = mkOption {
106+
type = with types; uniq str;
107+
description = ''
108+
The path to the Polykey recovery code file output location.
109+
'';
110+
};
111+
112+
statePath = mkOption {
113+
type = with types; uniq str;
114+
default = "%h/.local/share/polykey";
115+
description =
116+
"The path to the Polykey node state directory. Will default to `$HOME/.local/share/polykey`, but can be overwritten to a custom path.";
117+
};
118+
};
119+
};
120+
config = mkIf config.programs.polykey.enable {
121+
home.packages = [ outputs.packages.${system}.default ];
122+
123+
systemd.user.services.polykey = {
124+
Unit = { Description = "Polykey Agent"; };
125+
Service = {
126+
ExecStartPre = ''
127+
-${outputs.packages.${system}.default}/bin/polykey \
128+
--password-file ${config.programs.polykey.passwordFilePath} \
129+
--node-path ${config.programs.polykey.statePath} \
130+
bootstrap ${
131+
optionalString
132+
(config.programs.polykey.recoveryCodeFilePath != "")
133+
"-rcf ${config.programs.polykey.recoveryCodeFilePath}"
134+
}\
135+
--recovery-code-out-file ${config.programs.polykey.recoveryCodeOutPath}
136+
'';
137+
ExecStart = ''
138+
${outputs.packages.${system}.default}/bin/polykey \
139+
--password-file ${config.programs.polykey.passwordFilePath} \
140+
--node-path ${config.programs.polykey.statePath} \
141+
agent start \
142+
--recovery-code-out-file ${config.programs.polykey.recoveryCodeOutPath}
143+
'';
144+
};
145+
};
146+
};
147+
};
148+
}

0 commit comments

Comments
 (0)