-
Notifications
You must be signed in to change notification settings - Fork 78
/
flake.nix
150 lines (127 loc) · 5.55 KB
/
flake.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
{
description = "a postgres driver for crystal";
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nix-filter.url = "github:numtide/nix-filter";
};
outputs = { nixpkgs, flake-utils, nix-filter, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
crystal = pkgs.crystal;
pg_versions = builtins.map builtins.toString [ 17 16 15 14 13 ];
default_pg = pkgs."postgresql_${builtins.head pg_versions}";
certs = pkgs.stdenvNoCC.mkDerivation {
name = "crystal-pg-test-certs";
nativeBuildInputs = [ pkgs.openssl ];
installPhase = ''
mkdir $out
openssl req -new -nodes -text -out ca.csr -keyout ca-key.pem -subj "/CN=certificate-authority"
openssl x509 -req -in ca.csr -text -signkey ca-key.pem -out ca-cert.pem
openssl req -new -nodes -text -out server.csr -keyout server-key.pem -subj "/CN=pg-server"
openssl x509 -req -in server.csr -text -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem
openssl req -new -nodes -text -out client.csr -keyout client-key.pem -subj "/CN=crystal_ssl"
openssl x509 -req -in client.csr -text -CA ca-cert.pem -CAkey ca-key.pem -CAcreateserial -out client-cert.pem
# NOTE(2024-11-05): something broke with the newer openssl and client certs and the CA but I can't figure it out
# openssl verify -CAfile ca-cert.pem client-cert.pem
mv *.pem $out
'';
dontUnpack = true; # allows not giving a src
dontPatch = true;
dontConfigure = true;
dontBuild = true;
dontFixup = true;
};
tempdbSrc = pg: ''
export PATH=${pg}/bin:"$PATH"
tmpdir="$(mktemp -d)"
export PGDATA="$tmpdir"
export PGHOST="$tmpdir"
export PGUSER=postgres
export PGDATABASE=postgres
while
port=$(shuf -n 1 -i 49152-65535)
${pkgs.unixtools.netstat}/bin/netstat -atun | grep -q "$port"
do
continue
done
export PGPORT=$port
export DATABASE_URL=postgres://127.0.0.1:$port/
trap 'pg_ctl stop -m i; rm -rf "$tmpdir"' sigint sigterm exit
PGTZ=UTC initdb --no-locale --encoding=UTF8 --no-sync -U "$PGUSER" --auth=trust > /dev/null
#options="-F -c listen_addresses=\"\" -k $PGDATA"
options="-F -c port=$port -k $PGDATA"
cp ${certs}/*.pem "$tmpdir"
chmod 600 "$tmpdir"/*.pem
cert_opts="-c ssl=on -c ssl_cert_file='server-cert.pem' -c ssl_key_file='server-key.pem' -c ssl_ca_file='ca-cert.pem' "
echo "
local all postgres trust
host all postgres 127.0.0.1/32 trust
host all crystal_md5 127.0.0.1/32 md5
hostssl all crystal_ssl 127.0.0.1/32 cert
host all crystal_clear 127.0.0.1/32 password
host all crystal_scram 127.0.0.1/32 scram-sha-256
" > $tmpdir/pg_hba.conf
pg_ctl start -o "$options" -o "$cert_opts" #--log $tmpdir/pglogs.log
export CRYSTAL_PG_CERT_DIR=${certs}
"$@"
'';
specs = crystal.buildCrystalPackage {
name = "specs";
src = specSrc;
buildPhase = ''
echo 'require "./spec/**"' > specs.cr
'';
installPhase = "mkdir -p $out/bin && crystal build --error-on-warnings specs.cr -o $out/bin/specs";
shardsFile = specSrc + "/shards.nix";
preConfigure = "touch shard.lock";
lockfile = null;
doCheck = false;
dontPatch = true;
dontFixup = true;
doInstallCheck = false;
buildInputs = [ pkgs.gmp ];
};
filterSrc = files: (nix-filter.lib { root = ./.; include = [ "src" "spec" ] ++ files; });
specSrc = filterSrc [ "shard.lock" "shards.nix" "shard.yml" ];
check = pkgs.writeScriptBin "check" "nix build .#check --keep-going --print-build-logs";
tempdb = pkgs.writeScriptBin "tempdb" (tempdbSrc default_pg);
in
rec {
devShells.default = pkgs.mkShell {
buildInputs = [ crystal pkgs.crystal2nix pkgs.shards pkgs.gmp check tempdb ];
};
packages = {
check = pkgs.linkFarmFromDrvs "crystal-pg-all-checks" (builtins.attrValues checks);
inherit certs specs;
};
checks = {
format = pkgs.stdenvNoCC.mkDerivation {
name = "format";
src = (filterSrc [ ]);
installPhase = "mkdir $out && crystal tool format --check";
nativeBuildInputs = [ crystal ];
dontPatch = true;
dontConfigure = true;
dontBuild = true;
dontFixup = true;
};
} // pkgs.lib.genAttrs pg_versions (ver:
pkgs.stdenvNoCC.mkDerivation {
name = "specs-${ver}";
nativeBuildInputs = [ specs (pkgs.writeScriptBin "tempdb" (tempdbSrc pkgs."postgresql_${ver}")) ];
installPhase = "mkdir $out && tempdb specs";
dontUnpack = true; # allows not giving a src
doCheck = false;
dontPatch = true;
dontBuild = true;
dontFixup = true;
}
);
}
);
nixConfig = {
extra-substituters = "https://crunchy-public.cachix.org";
extra-trusted-public-keys = "crunchy-public.cachix.org-1:bsv90PlrrUAFcIA7NoajCWDpddTY2GGXX7XG+C1BMzQ=";
};
}