Repository navigation
Expand file tree
/
Copy pathnginx.nix
More file actions
173 lines (156 loc) · 5.01 KB
/
Copy pathnginx.nix
File metadata and controls
173 lines (156 loc) · 5.01 KB
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
{
lib,
testers,
openssl,
curl,
nginx,
pkgs,
self,
nixpkgs,
pkcs11Module,
extraKeypairOptions,
extraMachineOptions ? { },
}:
let
soPinFile = "/etc/so.pin";
pinFile = "/etc/user.pin";
extraEnv = pkcs11Module.mkEnv { };
storeInitHook = pkgs.writeShellScript "store-init" ''
chown -R nginx:nginx "$NIXPKCS_STORE_DIR" || true
'';
mkNode =
{
name,
extraConfig ? { },
}:
lib.mkMerge [
(
{ config, ... }:
{
imports = [
self.nixosModules.default
];
security.pkcs11 = {
enable = true;
keypairs = {
${name} = lib.recursiveUpdate {
enable = true;
inherit pkcs11Module extraEnv storeInitHook;
id = "deadbeefcafe";
debug = true;
keyOptions = {
algorithm = "RSA";
type = "3072";
usage = [
"sign"
"derive"
];
inherit soPinFile;
};
certOptions = {
serial = "09f91102";
subject = "C=US/ST=California/L=Carlsbad/O=nixpkcs/CN=nixpkcs.local";
extensions = [
"v3_ca"
"keyUsage=critical,nonRepudiation,keyCertSign,digitalSignature,cRLSign"
"subjectAltName=DNS:nixpkcs.local"
];
validityDays = 14;
renewalPeriod = 7;
inherit pinFile;
writeTo = "/etc/keys/nixpkcs.local.crt";
};
} extraKeypairOptions;
};
};
system.activationScripts.initTest.text = ''
if [ ! -f ${lib.escapeShellArg pinFile} ]; then
echo -n 22446688 > ${lib.escapeShellArg pinFile}
chmod 0640 ${lib.escapeShellArg pinFile}
chown nginx:nginx ${lib.escapeShellArg pinFile} || true
fi
if [ ! -f ${lib.escapeShellArg soPinFile} ]; then
# If we are logging in as the user, place the user PIN in the SO PIN file.
${
if config.security.pkcs11.keypairs.${name}.keyOptions.loginAsUser then
''
ln -s ${lib.escapeShellArg pinFile} ${lib.escapeShellArg soPinFile}
''
else
''
echo -n 11335577 > ${lib.escapeShellArg soPinFile}
chmod 0600 ${lib.escapeShellArg soPinFile}
''
}
fi
mkdir -p /etc/keys
'';
services.nginx = {
enable = true;
package = openssl.withPkcs11Module {
inherit pkcs11Module;
package = nginx;
confName = "nginx";
passthru = {
inherit (nginx) modules;
};
};
recommendedOptimisation = true;
recommendedTlsSettings = true;
recommendedProxySettings = true;
recommendedGzipSettings = true;
appendConfig = ''
${lib.concatMapStringsSep "\n" (x: "env ${x};") (lib.attrNames extraEnv)}
'';
virtualHosts."nixpkcs.local" = {
forceSSL = true;
sslCertificate = "/etc/keys/nixpkcs.local.crt";
sslCertificateKey = pkgs.pkcs11-provider.uri2pem config.security.pkcs11.keypairs.${name}.uri;
locations."/" = {
root = ./nginx/root;
};
};
};
networking.hosts = {
"127.0.0.1" = [ "nixpkcs.local" ];
};
environment = {
systemPackages = [
openssl
curl
];
};
systemd.services."nginx" = {
environment = extraEnv;
serviceConfig = {
# For accessing the TPM2 database.
ProtectSystem = lib.mkForce false;
# For accessing the TPM2 device.
PrivateDevices = lib.mkForce false;
};
};
}
)
extraConfig
extraMachineOptions
];
in
testers.runNixOSTest {
name = "nixpkcs-test-nginx";
nodes.nginx = mkNode {
name = "nginx";
};
testScript = ''
nginx.start()
# Wait for the keys to exist.
nginx.wait_until_succeeds('openssl x509 -in /etc/keys/nixpkcs.local.crt -noout -subject | grep -q "nixpkcs.local"')
# It should be in nixpkcs-uri too.
nginx.succeed("nixpkcs-uri | tee /dev/stderr | grep -qE '^nginx[[:space:]]+pkcs11:.*?;?id=%DE%AD%BE%EF%CA%FE(;|$)'")
# Wait for the webserver to come up, and make sure it's reliable thereafter
nginx.succeed('systemctl restart nginx')
cmd = 'curl --cacert /etc/keys/nixpkcs.local.crt https://nixpkcs.local/index.html | tee /dev/stderr | grep "Hello, nixpkcs!"'
nginx.wait_until_succeeds(cmd)
for i in range(100):
nginx.succeed(cmd)
'';
}