-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
236 lines (214 loc) · 7.04 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
235
236
{
inputs = {
nixpkgs.url = "github:cachix/devenv-nixpkgs/rolling";
systems.url = "github:nix-systems/default";
devenv = {
url = "github:cachix/devenv";
inputs.nixpkgs.follows = "nixpkgs";
# inputs.cachix.inputs.devenv.follows = "devenv";
};
fenix = {
url = "github:nix-community/fenix/monthly";
inputs.nixpkgs.follows = "nixpkgs";
};
};
nixConfig = {
extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
extra-substituters = "https://devenv.cachix.org";
};
outputs =
{ self
, nixpkgs
, devenv
, systems
, fenix
, ...
}@inputs:
let
pkg_name = "moonlit_binge";
forEachSystem = nixpkgs.lib.genAttrs (import systems);
in
{
packages = forEachSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ fenix.overlays.default ];
};
toolchain = pkgs.fenix.complete;
rustPlatform = pkgs.makeRustPlatform { inherit (toolchain) cargo rustc; };
cargoToml =
with builtins;
let
toml = readFile ./Cargo.toml;
in
fromTOML toml;
buildMetadata =
with pkgs.lib.strings;
let
lastModifiedDate = self.lastModifiedDate or self.lastModified or "";
date = builtins.substring 0 8 lastModifiedDate;
shortRev = self.shortRev or "dirty";
hasDateRev = lastModifiedDate != "" && shortRev != "";
dot = optionalString hasDateRev ".";
in
"${date}${dot}${shortRev}";
version =
with pkgs.lib.strings;
let
hasBuildMetadata = buildMetadata != "";
plus = optionalString hasBuildMetadata "+";
in
"${cargoToml.package.version}${plus}${buildMetadata}";
bin = rustPlatform.buildRustPackage {
inherit version;
pname = pkg_name;
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
nativeBuildInputs = with pkgs; [
pkg-config
];
buildInputs = with pkgs; [
openssl
];
doCheck = false; # Tests can't run inside sandbox as they need postgres and redis services...
};
# Converts self.lastModifiedDate to ISO-8601 for the layered image, it's cursed...
# but at least it's pure...
created = "${builtins.substring 0 4 self.lastModifiedDate}-${builtins.substring 4 2 self.lastModifiedDate}-${builtins.substring 6 2 self.lastModifiedDate}T${builtins.substring 8 2 self.lastModifiedDate}:${builtins.substring 10 2 self.lastModifiedDate}:${builtins.substring 12 2 self.lastModifiedDate}Z";
dockerImage = pkgs.dockerTools.buildLayeredImage {
inherit created;
name = "${pkg_name}";
tag = "v${builtins.replaceStrings [ "+" ] [ "-" ] version}";
contents = [
pkgs.bashInteractive
pkgs.busybox
pkgs.dockerTools.caCertificates
bin
];
extraCommands = ''
set -e
mkdir tmp
mkdir data
mkdir -p app/assets
cd app
cp -r "${./.}"/assets .
'';
fakeRootCommands = ''
set -e
${pkgs.dockerTools.shadowSetup}
chmod 0777 tmp
groupadd --system -g 999 docker
useradd --system --no-create-home -u 999 -g 999 docker
chown -R docker:docker app data
'';
enableFakechroot = true;
config = {
Entrypoint = [ "${bin}/bin/${pkg_name}-cli" "start" ];
Env = [ "LOCO_ENV=production" ];
WorkingDir = "/app";
Volumes = {
"/app/config" = { };
"/data" = { };
};
ExposedPorts = {
"5150/tcp" = { };
"3000/tcp" = { };
};
User = "docker:docker";
};
};
in
{
inherit bin dockerImage;
devenv-up = self.devShells.${system}.default.config.procfileScript;
default = bin;
}
);
apps = forEachSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
watch =
let
script = pkgs.writeShellScriptBin "watch" ''
${pkgs.systemfd}/bin/systemfd --no-pid -s http::0.0.0.0:3001 -- ${pkgs.cargo-watch}/bin/cargo-watch -w assets -w config -w migration -w src -w players -x "run -- start"
'';
in
{
type = "app";
program = "${script}/bin/watch";
};
}
);
devShells = forEachSystem (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
in
{
default = devenv.lib.mkShell {
inherit inputs pkgs;
modules = [
{
# https://devenv.sh/reference/options/
languages.rust.enable = true;
packages = with pkgs; [
cargo-watch
cargo-insta
cargo-deny
systemfd
jq
dive
nil
sea-orm-cli
unixtools.xxd
openssl
];
pre-commit.hooks = {
statix.enable = true;
nixpkgs-fmt.enable = true;
clippy.enable = true;
rustfmt.enable = true;
};
services = {
postgres = {
enable = true;
listen_addresses = "127.0.0.1";
port = 5433;
initialDatabases = [{ name = "${pkg_name}"; }];
initialScript = ''
CREATE USER ${pkg_name} SUPERUSER PASSWORD '${pkg_name}';
'';
};
mailpit.enable = true;
adminer.enable = true;
nginx = {
enable = true;
httpConfig = ''
server {
listen 3002;
location /stream {
rewrite ^/stream/(.*) /$1 break;
proxy_pass http://localhost:3000/$uri$is_args$args;
}
location / {
proxy_pass http://localhost:3001;
}
}
'';
};
};
env = {
DATABASE_URL = "postgres://${pkg_name}:${pkg_name}@127.0.0.1:5433/${pkg_name}";
};
}
];
};
}
);
};
}