-
Notifications
You must be signed in to change notification settings - Fork 3
/
flake.nix
79 lines (69 loc) · 2.41 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
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, crane, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
};
craneLib = crane.lib.${system};
my-crate = craneLib.buildPackage {
src = craneLib.cleanCargoSource (craneLib.path ./.);
buildInputs = [
pkgs.just
pkgs.openssl
pkgs.zlib
pkgs.postgresql
pkgs.gcc
pkgs.gcc.cc.lib
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
pkgs.libiconv
];
};
setupPostgresScript = pkgs.writeShellScript "setup-postgres" ''
export PGDATA=$(mktemp -d)
export PGSOCKETS=$(mktemp -d)
${pkgs.postgresql}/bin/initdb -D $PGDATA
${pkgs.postgresql}/bin/pg_ctl start -D $PGDATA -o "-h localhost -p 5432 -k $PGSOCKETS"
until ${pkgs.postgresql}/bin/pg_isready -h localhost -p 5432; do sleep 1; done
${pkgs.postgresql}/bin/createuser -h localhost -p 5432 -s postgres
${pkgs.postgresql}/bin/psql -h localhost -p 5432 -c "CREATE USER \"vss_user\" WITH PASSWORD 'password';" -U postgres
${pkgs.postgresql}/bin/psql -h localhost -p 5432 -c "CREATE DATABASE \"vss\" OWNER \"vss_user\";" -U postgres
exit
'';
setupEnvScript = pkgs.writeShellScript "setup-env" ''
if [ ! -f .env ]; then
cp .env.sample .env
sed -i 's|DATABASE_URL=postgres://localhost/vss|DATABASE_URL=postgres://vss_user:password@localhost:5432/vss|g' .env
fi
'';
in
{
packages.default = my-crate;
devShells.default = craneLib.devShell {
inputsFrom = [ my-crate ];
packages = [
pkgs.just
pkgs.openssl
pkgs.zlib
pkgs.postgresql
pkgs.gcc
pkgs.rust-analyzer
pkgs.diesel-cli
pkgs.gcc.cc.lib
];
shellHook = ''
export LD_LIBRARY_PATH=${pkgs.openssl}/lib:${pkgs.gcc.cc.lib}/lib:$LD_LIBRARY_PATH
${setupPostgresScript}
${setupEnvScript}
'';
};
});
}