-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathmkcert.nix
More file actions
113 lines (98 loc) · 3.12 KB
/
mkcert.nix
File metadata and controls
113 lines (98 loc) · 3.12 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
{ lib, pkgs, config, ... }:
with lib;
let
cfg = config.mkcert;
maybeReadFile = obj:
if (builtins.typeOf obj == "path") then
builtins.readFile obj
else
obj
;
# These are all the options available for specifying a certificate/key file.
rootCAOption = {
key = mkOption {
description = "Text or file of the root CA key to install";
default = null;
type = types.nullOr (types.either types.str types.path);
};
cert = mkOption {
description = "Text or file of the root CA certificate to install";
default = null;
type = types.nullOr (types.either types.str types.path);
};
};
# A collection of development certificate authority files
rootCADir =
let
adHoc = pkgs.runCommandLocal "rootCA" {} ''
export CAROOT=$out
${pkgs.mkcert}/bin/mkcert 2>/dev/null
'';
isPreconfigured =
assert assertMsg (
cfg.root-ca != {} && cfg.root-ca.cert != null && cfg.root-ca.key != null
) "[mkcert]: either set, both, root CA key and certificate or none.";
(cfg.root-ca.key != null && cfg.root-ca.cert != null);
key = pkgs.writeText "rootCA-key.pem" (maybeReadFile cfg.root-ca.key);
cert = pkgs.writeText "rootCA.pem" (maybeReadFile cfg.root-ca.cert);
preconfigured = pkgs.runCommand "rootCA" {} ''
mkdir $out
cp ${key} $out/${key.name}
cp ${cert} $out/${cert.name}
'';
in
if isPreconfigured then preconfigured else adHoc;
# Execute this script to install the project's development certificate authority
install-mkcert-ca = pkgs.writeShellScriptBin "install-mkcert-ca" ''
set -euo pipefail
shopt -s nullglob
log() {
IFS=$'\n' loglines=($*)
for line in ${"$"}{loglines[@]}; do echo -e "[mkcert] $line" >&2; done
}
# Set the CA root files directory for mkcert via env variable
export CAROOT=${rootCADir}
# Install local CA into system, java and nss (includes Firefox) trust stores
log "Install development CA into the system stores..."
log $(sudo -K; ${pkgs.mkcert}/bin/mkcert -install 2>&1)
log "root CA directory: $(${pkgs.mkcert}/bin/mkcert -CAROOT 2>&1)"
uninstall() {
log $(${pkgs.mkcert}/bin/mkcert -uninstall 2>&1)
}
# TODO: Uninstall when leaving the devshell
# trap uninstall EXIT
'';
in
{
options.mkcert = {
enable = mkEnableOption "provide a development CA within the shell";
root-ca = mkOption {
type = types.submodule { options = rootCAOption; };
default = {};
description = "preconfigure root CA files";
example = literalExample "
{
key = ''
-----BEGIN PRIVATE KEY-----
...
-----END PRIVATE KEY-----
'';
cert = ./path/tocert.pem;
}
";
};
};
config = mkIf cfg.enable {
env = [{
name = "CAROOT";
value = "${rootCADir}";
}];
commands = [ { package = pkgs.mkcert; category = "certs"; } ];
devshell = {
packages = [ install-mkcert-ca ];
startup.install-mkcert-ca.text = "
$DEVSHELL_DIR/bin/install-mkcert-ca
";
};
};
}