-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflake.nix
234 lines (221 loc) · 7.74 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
{
inputs = {
pkgs.url = "github:nixos/nixpkgs/nixos-23.11";
utils.url = "github:numtide/flake-utils";
# Provides cargo dependencies.
fenix = {
url = "github:nix-community/fenix/monthly";
inputs.nixpkgs.follows = "pkgs";
};
# Builds rust projects.
naersk = {
url = "github:nmattia/naersk";
inputs.nixpkgs.follows = "pkgs";
};
nix-filter.url = "github:numtide/nix-filter";
};
outputs = inputs:
inputs.utils.lib.eachDefaultSystem (system:
let
pkgs = import inputs.pkgs {
inherit system;
config.allowUnfree = true;
};
fenix = inputs.fenix.packages.${system};
toolchainFile = {
file = ./rust-toolchain.toml;
sha256 = "sha256-s1RPtyvDGJaX/BisLT+ifVfuhDT1nZkZ1NcK8sbwELM=";
};
rust-toolchain = fenix.fromToolchainFile toolchainFile;
naersk = inputs.naersk.lib.${system}.override {
cargo = rust-toolchain;
rustc = rust-toolchain;
};
filter = inputs.nix-filter.lib;
packageSrc = filter.filter {
root = ./.;
include = [
(filter.inDirectory ".cargo")
(filter.inDirectory "types")
(filter.inDirectory "graphql")
(filter.inDirectory "migration")
(filter.inDirectory "admin-event-handlers")
./Cargo.toml
./Cargo.lock
./rust-toolchain.toml
./sqlx-data.json
];
};
# The rust compiler is internally a cross compiler, so a single
# toolchain can be used to compile multiple targets. In a hermetic
# build system like nix flakes, there's effectively one package for
# every permutation of the supported hosts and targets.
targetPackage = let
target = "x86_64-unknown-linux-musl";
pkgsCross = import inputs.pkgs {
inherit system;
crossSystem.config = target;
};
cc = pkgsCross.pkgsStatic.stdenv.cc;
in naersk.buildPackage {
root = ./.;
src = packageSrc;
doCheck = true;
doTest = true;
nativeBuildInputs = [ cc ];
# Configures the target which will be built.
# ref: https://doc.rust-lang.org/cargo/reference/config.html#buildtarget
CARGO_BUILD_TARGET = target;
TARGET_CC = "${cc}/bin/${target}-gcc";
CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static";
};
hostPackage = naersk.buildPackage {
root = ./.;
src = packageSrc;
};
dailpFunctions = with pkgs;
stdenv.mkDerivation {
name = "dailp-functions";
buildInputs = [ zip ];
# Permits a derivation with no source files.
unpackPhase = "true";
installPhase = ''
mkdir -p $out
cp -f ${targetPackage}/bin/dailp-graphql $out/bootstrap
zip -j $out/dailp-graphql.zip $out/bootstrap
cp -f ${targetPackage}/bin/auth-post-confirmation $out/bootstrap
zip -j $out/auth-post-confirmation.zip $out/bootstrap
'';
};
terraformConfig = pkgs.writeTextFile {
name = "terraform-config";
text = let
tf = import "${pkgs.terranix}/core/default.nix" {
inherit pkgs;
terranix_config = {
imports = [ ./terraform/main.nix ];
functions.package_path = "${dailpFunctions}";
};
strip_nulls = true;
};
in builtins.toJSON (tf.config);
executable = false;
destination = "/config.tf.json";
};
mkBashApp = name: script:
inputs.utils.lib.mkApp {
drv = pkgs.writers.writeBashBin name script;
exePath = "/bin/${name}";
};
tf = "${pkgs.terraform}/bin/terraform";
inherit (builtins) getEnv;
tfInit = ''
cp -f ${terraformConfig}/config.tf.json ./
export AWS_ACCESS_KEY_ID=${getEnv "AWS_ACCESS_KEY_ID"}
export AWS_SECRET_ACCESS_KEY=${getEnv "AWS_SECRET_ACCESS_KEY"}
export TF_DATA_DIR=$(pwd)/.terraform
${tf} init -upgrade
'';
in rec {
# Add extra binary caches for quicker builds of the rust toolchain
nixConfig = {
binaryCaches =
[ "https://nix-community.cachix.org" "https://dailp.cachix.org" ];
binaryCachePublicKeys = [
"nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs="
"dailp.cachix.org-1:QKIYFfTB/jrD6J8wZoBEpML64ONrIxs3X5ifSKoJ3kA="
];
};
packages.default = terraformConfig;
apps.migrate-data = inputs.utils.lib.mkApp {
drv = hostPackage;
exePath = "/bin/dailp-migration";
};
apps.validate-data = inputs.utils.lib.mkApp {
drv = hostPackage;
exePath = "/bin/dailp-validate";
};
apps.migrate-schema = mkBashApp "migrate-schema" ''
cd types
${pkgs.sqlx-cli}/bin/sqlx database create
${pkgs.sqlx-cli}/bin/sqlx migrate run
'';
apps.tf-init = mkBashApp "tf-init" tfInit;
apps.tf-plan = mkBashApp "plan" ''
${tfInit}
${tf} plan
'';
apps.tf-apply = mkBashApp "apply" ''
${tfInit}
${tf} apply
'';
apps.tf-apply-now = mkBashApp "apply-now" ''
${tfInit}
${tf} apply -auto-approve
'';
apps.tf-output = mkBashApp "tf-output" ''
${tf} output $1 | xargs
'';
devShells.default = with pkgs;
mkShell rec {
name = "dailp-dev";
unpackPhase = "true";
RUST_LOG = "info";
LD_LIBRARY_PATH = "${lib.makeLibraryPath buildInputs}";
shellHook = ''
export PROJECT_ROOT=$PWD
export PGDATA=$PROJECT_ROOT/.postgres
git config --local core.hooksPath $PROJECT_ROOT/.git-hooks
eval $(${direnv}/bin/direnv dotenv)
'';
buildInputs = [
autoconf
automake
libtool
pkg-config
file
nasm
terraform
rust-toolchain
nodejs-18_x
yarn
act
postgresql_14
sqlx-cli
sqlfluff
(writers.writeBashBin "dev-check" ./check.sh)
(writers.writeBashBin "dev-database" ''
[ ! -d "$PGDATA" ] && initdb
postgres -D $PGDATA -c unix_socket_directories=/tmp
'')
(writers.writeBashBin "dev-graphql" ''
cd $PROJECT_ROOT
cargo run --bin dailp-graphql-schema
cargo run --bin dailp-graphql-local
'')
(writers.writeBashBin "dev-website" ''
cd $PROJECT_ROOT/website
yarn install
yarn dev
'')
(writers.writeBashBin "dev-migrate-schema" ''
cd $PROJECT_ROOT/types
sqlx database create
sqlx migrate run
'')
(writers.writeBashBin "dev-migrate-data" ''
cd $PROJECT_ROOT
cargo run --bin dailp-migration
'')
(writers.writeBashBin "dev-generate-types" ''
cd $PROJECT_ROOT/types
cargo sqlx prepare -- -p dailp
'')
] ++ lib.optionals stdenv.isDarwin [
darwin.apple_sdk.frameworks.Security
darwin.apple_sdk.frameworks.SystemConfiguration
libiconv
];
};
});
}