-
Notifications
You must be signed in to change notification settings - Fork 10
/
secrets.nix
executable file
·197 lines (181 loc) · 5.59 KB
/
secrets.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
{ pkgs, config, lib, inputs, ... }:
with lib;
with types;
let
password-store = config.secretsConfig.password-store;
secret = { name, ... }: {
options = {
encrypted = mkOption {
type = path;
default = "${password-store}/${name}.gpg";
};
decrypted = mkOption {
type = path;
default = "/var/secrets/${name}";
};
decrypt = mkOption {
default = pkgs.writeShellScript "gpg-decrypt" ''
set -euo pipefail
export GPG_TTY="$(tty)"
${pkgs.gnupg}/bin/gpg-connect-agent updatestartuptty /bye 1>&2
${pkgs.gnupg}/bin/gpg --batch --no-tty --decrypt
'';
};
user = mkOption {
type = str;
default = "balsoft";
};
owner = mkOption {
type = str;
default = "root:root";
};
permissions = mkOption {
type = lib.types.addCheck lib.types.str
(perm: !isNull (builtins.match "[0-7]{3}" perm));
default = "400";
};
services = mkOption {
type = listOf str;
default = [ "${name}" ];
};
__toString = mkOption {
readOnly = true;
default = s: s.decrypted;
};
};
};
activate-secrets = pkgs.writeShellApplication {
name = "activate-secrets";
text = ''
set -euo pipefail
# Make sure card is available and unlocked
echo fetch | gpg --card-edit --no-tty --command-fd=0
gpg --card-status
SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
export SSH_AUTH_SOCK
if [ -d "${password-store}/.git" ]; then
cd "${password-store}"; git pull
else
git clone ${
lib.escapeShellArg config.secretsConfig.repo
} "${password-store}"
fi
gpg --decrypt < ${password-store}/email/[email protected] > /dev/null
/run/wrappers/bin/sudo systemctl restart ${allServices}
'';
runtimeInputs = [ pkgs.gnupg pkgs.git pkgs.systemd pkgs.openssh ];
};
decrypt = name: cfg:
with cfg; {
"${name}-secrets" = rec {
wantedBy = [ "multi-user.target" ];
requires = [ "[email protected]" ];
after = requires;
preStart = ''
stat '${encrypted}'
mkdir -p '${builtins.dirOf decrypted}'
'';
script = ''
if cat '${encrypted}' | /run/wrappers/bin/sudo -u ${user} ${cfg.decrypt} > '${decrypted}.tmp'; then
mv -f '${decrypted}.tmp' '${decrypted}'
chown '${owner}' '${decrypted}'
chmod '${permissions}' '${decrypted}'
else
echo "Failed to decrypt the secret"
rm '${decrypted}.tmp'
if [[ -f '${decrypted}' ]]; then
echo "The decrypted file exists anyways, not failing"
exit 0
else
exit 1
fi
fi
'';
serviceConfig = {
Type = "oneshot";
RemainAfterExit = "yes";
};
};
};
addDependencies = name: cfg:
with cfg;
genAttrs services (service: rec {
requires = [ "${name}-secrets.service" ];
after = requires;
bindsTo = requires;
});
mkServices = name: cfg: [ (decrypt name cfg) (addDependencies name cfg) ];
allServices = toString (map (name: "${name}-envsubst.service")
(builtins.attrNames config.secrets-envsubst)
++ map (name: "${name}-secrets.service")
(builtins.attrNames config.secrets));
in {
options = {
secrets = lib.mkOption {
type = attrsOf (submodule secret);
default = { };
};
secretsConfig = {
password-store = lib.mkOption {
type = lib.types.path;
default = "/home/balsoft/.local/share/password-store";
};
repo = lib.mkOption {
type = str;
default = "ssh://[email protected]/balsoft/pass";
};
};
};
config = {
systemd.services =
mkMerge (concatLists (mapAttrsToList mkServices config.secrets));
security.sudo.extraRules = [{
users = [ "balsoft" ];
commands = [{
command = "/run/current-system/sw/bin/systemctl restart ${allServices}";
options = [ "NOPASSWD" ];
}];
}];
persist.derivative.directories = [ "/var/secrets" password-store ];
home-manager.users.balsoft = {
systemd.user.services.activate-secrets = {
Service = {
ExecStart = "${activate-secrets}/bin/activate-secrets";
Type = "oneshot";
};
Unit = { PartOf = [ "graphical-session-pre.target" ]; };
Install.WantedBy = [ "graphical-session-pre.target" ];
};
systemd.user.services.pass-store-sync = {
Service = {
Environment = [
"PASSWORD_STORE_DIR=${password-store}"
"PATH=${
lib.makeBinPath [ pkgs.pass pkgs.inotify-tools pkgs.gnupg ]
}"
];
ExecStart = toString (pkgs.writeShellScript "pass-store-sync" ''
export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)"
while inotifywait "$PASSWORD_STORE_DIR" -r -e move -e close_write -e create -e delete --exclude .git; do
sleep 0.1
pass git add --all
pass git commit -m "Change"
pass git pull --rebase
pass git push
done
'');
};
Unit = rec {
After = [ "activate-secrets.service" ];
Wants = After;
};
Install.WantedBy = [ "graphical-session-pre.target" ];
};
programs.password-store = {
enable = true;
package = pkgs.pass-wayland;
settings.PASSWORD_STORE_DIR = password-store;
};
};
};
}