-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
196 lines (179 loc) · 6.65 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
{
description = "A rust implementation of PRISM node";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
flake-utils.url = "github:numtide/flake-utils";
};
outputs =
{
nixpkgs,
rust-overlay,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
config.unfree = true;
overlays = [ (import rust-overlay) ];
};
rust = pkgs.rust-bin.nightly."2024-10-30".default.override {
extensions = [
"rust-src"
"rust-analyzer"
];
targets = [ ];
};
rustPlatform = pkgs.makeRustPlatform {
cargo = rust;
rustc = rust;
};
in
{
packages = rec {
default = rustPlatform.buildRustPackage {
name = "neoprism";
cargoLock.lockFile = ./Cargo.lock;
src = pkgs.lib.cleanSource ./.;
buildInputs = [ pkgs.protobuf ];
PROTOC = "${pkgs.protobuf}/bin/protoc";
};
assets = pkgs.stdenv.mkDerivation {
name = "neoprism-assets";
src = pkgs.lib.cleanSource ./.;
installPhase = ''
mkdir -p $out/assets
cp prism-node/assets/tailwind.css $out/assets/tailwind.css
'';
};
dockerImage = pkgs.dockerTools.buildImage {
name = "neoprism";
tag = "0.1.0-SNAPSHOT";
created = "now";
copyToRoot = pkgs.buildEnv {
name = "image-root";
paths = [
pkgs.coreutils
assets
];
};
config = {
Env = [ "RUST_LOG=info,oura=warn,tracing::span=warn" ];
Entrypoint = [ "${default}/bin/prism-node" ];
Cmd = [
"--assets"
"/assets"
];
WorkingDir = "";
};
};
};
devShells.default =
let
rootDir = "$ROOT_DIR";
localDb = {
port = 5432;
username = "postgres";
password = "postgres";
dbName = "postgres";
};
scripts = rec {
format = pkgs.writeShellScriptBin "format" ''
cd ${rootDir}
find ${rootDir} | grep '\.nix$' | xargs -I _ bash -c "echo running nixfmt on _ && ${pkgs.nixfmt-rfc-style}/bin/nixfmt _"
find ${rootDir} | grep '\.toml$' | xargs -I _ bash -c "echo running taplo on _ && ${pkgs.taplo}/bin/taplo format _"
${pkgs.dioxus-cli}/bin/dx fmt
${rust}/bin/cargo fmt
'';
buildAssets = pkgs.writeShellScriptBin "buildAssets" ''
cd ${rootDir}/prism-node
${pkgs.nodePackages.tailwindcss}/bin/tailwindcss -i tailwind.css -o ./assets/tailwind.css
'';
build = pkgs.writeShellScriptBin "build" ''
cd ${rootDir}
${buildAssets}/bin/buildAssets
${rust}/bin/cargo build --all-features
'';
clean = pkgs.writeShellScriptBin "clean" ''
${rust}/bin/cargo clean
'';
dbUp = pkgs.writeShellScriptBin "dbUp" ''
${pkgs.docker}/bin/docker run \
-d --rm \
--name prism-db \
-e POSTGRES_DB=${localDb.dbName} \
-e POSTGRES_USER=${localDb.username} \
-e POSTGRES_PASSWORD=${localDb.password} \
-p ${toString localDb.port}:5432 postgres:16
'';
dbDown = pkgs.writeShellScriptBin "dbDown" ''
${pkgs.docker}/bin/docker stop prism-db
'';
pgDump = pkgs.writeShellScriptBin "pgDump" ''
export PGPASSWORD=${localDb.password}
${pkgs.postgresql_16}/bin/pg_dump -h localhost -p ${toString localDb.port} -U ${localDb.username} -w -d ${localDb.dbName} -Fc > ${rootDir}/postgres.dump
'';
pgRestore = pkgs.writeShellScriptBin "pgRestore" ''
export PGPASSWORD=${localDb.password}
${pkgs.postgresql_16}/bin/pg_restore -h localhost -p ${toString localDb.port} -U ${localDb.username} -w -d ${localDb.dbName} ${rootDir}/postgres.dump
'';
migrate = pkgs.writeShellScriptBin "migrate" ''
${pkgs.sea-orm-cli}/bin/sea-orm-cli migrate up -d prism-migration --database-url postgres://${localDb.username}:${localDb.password}@localhost:${toString localDb.port}/${localDb.dbName}
'';
generateEntity = pkgs.writeShellScriptBin "generateEntity" ''
rm -rf prism-storage/src/entity
${pkgs.sea-orm-cli}/bin/sea-orm-cli generate entity \
--database-url postgres://${localDb.username}:${localDb.password}@localhost:${toString localDb.port}/${localDb.dbName} \
-o prism-storage/src/entity \
--date-time-crate time
'';
runNode = pkgs.writeShellScriptBin "runNode" ''
cd ${rootDir}
${buildAssets}/bin/buildAssets
${rust}/bin/cargo run --bin prism-node -- --db postgres://${localDb.username}:${localDb.password}@localhost:${toString localDb.port}/${localDb.dbName} "$@"
'';
};
in
pkgs.mkShell {
packages =
with pkgs;
[
# base
docker
git
less
ncurses
protobuf
which
# lsp
nil
taplo
# rust
cargo-edit
cargo-license
cargo-udeps
cargo-watch
dioxus-cli
protobuf
rust
sea-orm-cli
# tailwind & html
nodejs_20
nodePackages."@tailwindcss/language-server"
nodePackages.vscode-langservers-extracted
]
++ (builtins.attrValues scripts);
shellHook = ''
export ROOT_DIR=$(${pkgs.git}/bin/git rev-parse --show-toplevel)
${pkgs.cowsay}/bin/cowsay "Working on project root directory: ${rootDir}"
cd ${rootDir}
'';
# envs
RUST_LOG = "info,oura=warn,tracing::span=warn";
};
}
);
}