-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
134 lines (132 loc) · 4.77 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
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};
crane = {
url = "github:ipetkov/crane";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
};
outputs = { self, nixpkgs, flake-utils, rust-overlay, crane }:
let
system = "x86_64-linux";
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit system overlays;
};
inherit (pkgs) lib;
rustToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
src = lib.cleanSourceWith {
src = ./.; # The original, unfiltered source
filter = path: type:
# Allow sql files for migrations
(lib.hasSuffix "\.sql" path) ||
# Default filter from crane (allow .rs files)
(craneLib.filterCargoSources path type)
;
};
nativeBuildInputs = with pkgs; [ rustToolchain pkg-config ];
buildInputs = with pkgs; [ rustToolchain openssl postgresql_15.lib ];
developmentTools = with pkgs; [ (diesel-cli.override { sqliteSupport = false; mysqlSupport = false; }) postgresql cargo ];
commonArgs = {
inherit src buildInputs nativeBuildInputs;
installCargoArtifactsMode = "use-zstd";
strictDeps = true;
};
integrationTestsArtifacts = craneLib.buildDepsOnly (commonArgs // {
cargoBuildCommand = "cargo build --profile dev";
cargoExtraArgs = "--bin integration-tests";
doCheck = false;
pname = "integration-tests";
});
integrationTestsBinary = craneLib.buildPackage (commonArgs // {
cargoArtifacts = integrationTestsArtifacts;
cargoBuildCommand = "cargo build --profile dev";
cargoExtraArgs = "--bin integration-tests";
doCheck = false;
pname = "integration-tests";
});
cargoDebugArtifacts = craneLib.buildDepsOnly (commonArgs // {
cargoBuildCommand = "cargo build --profile dev";
cargoExtraArgs = "--bin rvoc-backend";
doCheck = false;
pname = "rvoc-backend";
});
debugBinary = craneLib.buildPackage (commonArgs // {
cargoArtifacts = cargoDebugArtifacts;
cargoBuildCommand = "cargo build --profile dev";
cargoExtraArgs = "--bin rvoc-backend";
doCheck = false;
pname = "rvoc-backend";
});
cargoArtifacts = craneLib.buildDepsOnly (commonArgs // {
cargoBuildCommand = "cargo build --profile release";
cargoExtraArgs = "--bin rvoc-backend";
doCheck = false;
pname = "rvoc-backend";
});
binary = craneLib.buildPackage (commonArgs // {
cargoArtifacts = cargoArtifacts;
cargoBuildCommand = "cargo build --profile release";
cargoExtraArgs = "--bin rvoc-backend";
pname = "rvoc-backend";
});
dockerImage = pkgs.dockerTools.streamLayeredImage {
name = "rvoc-backend";
tag = "latest";
contents = [ binary pkgs.cacert ];
config = {
Cmd = [ "${binary}/bin/rvoc-backend" ];
};
};
debugDockerImage = pkgs.dockerTools.streamLayeredImage {
name = "rvoc-backend";
tag = "latest";
contents = [ debugBinary pkgs.cacert ];
config = {
Cmd = [ "${debugBinary}/bin/rvoc-backend" ];
};
};
in
with pkgs;
{
packages.${system} = {
inherit binary debugBinary integrationTestsBinary dockerImage debugDockerImage;
default = binary;
};
formatter.${system} = pkgs.nixpkgs-fmt;
devShells.${system}.default = mkShell {
inputsFrom = [ binary ];
buildInputs = with pkgs; [ dive wget ];
packages = developmentTools;
shellHook = ''
export PGDATA=$PWD/backend/data/postgres_dev_data
export PGHOST=$PWD/backend/data/postgres_dev
export LOG_PATH=$PWD/backend/data/postgres_dev/LOG
export PGDATABASE=rvoc_dev
export POSTGRES_RVOC_URL="postgresql://''${USER}@/''${PGDATABASE}?host=$PGHOST"
export DATABASE_URL=$POSTGRES_RVOC_URL
if [ ! -d $PGHOST ]; then
mkdir -p $PGHOST
fi
if [ ! -d $PGDATA ]; then
echo 'Initializing postgresql database...'
initdb $PGDATA --no-locale --encoding=UTF8 --auth=trust >/dev/null
fi
echo "Starting postgres"
pg_ctl start -l $LOG_PATH -o "-c listen_addresses= -c unix_socket_directories=$PGHOST"
echo "Shell hook finished"
'';
};
};
}