-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
231 lines (203 loc) · 7.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
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
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
chatsounds-cli-repo = {
url = "github:SpiralP/chatsounds-cli";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, chatsounds-cli-repo }:
let
inherit (nixpkgs) lib;
nodeManifest = lib.importJSON ./package.json;
rustManifest = lib.importTOML ./Cargo.toml;
revSuffix = lib.optionalString (self ? shortRev || self ? dirtyShortRev)
"-${self.shortRev or self.dirtyShortRev}";
makePackage = (system: dev:
let
pkgs = import nixpkgs {
inherit system;
};
chatsounds-cli = chatsounds-cli-repo.outputs.packages.${system}.default;
hl1-msgpack = pkgs.fetchurl {
url = "https://raw.githubusercontent.com/PAC3-Server/chatsounds-valve-games/HEAD/hl1/list.msgpack";
hash = "sha256-ArdqCFv0wjiElqp6cwRZA/iFuaALr2silJh3STBgCl8=";
};
hl1-test-sound = pkgs.fetchurl {
url = "https://raw.githubusercontent.com/PAC3-Server/chatsounds-valve-games/HEAD/hl1/scientist/ah%20hello%20gordon%20freeman%20its%20good%20to%20see%20you.ogg";
hash = "sha256-QHHEXW18p2dtH/4ph0XGLZ5uvVokBQf+Njce37QWXnc=";
};
# use FC_DEBUG=1024 when running to debug what config files are loaded
fontconfig = pkgs.runCommand "fontconfig"
{
nativeBuildInputs = with pkgs; [ sd ];
}
''
mkdir -vp "$out/conf.d"
ln -vs ${pkgs.fontconfig.out}/etc/fonts/conf.d/* "$out/conf.d/"
cat '${pkgs.fontconfig.out}/etc/fonts/fonts.conf' > "$out/fonts.conf"
sd --fixed-strings '${pkgs.dejavu_fonts.minimal}' '${pkgs.noto-fonts}' "$out/fonts.conf"
sd --fixed-strings '/etc/fonts/conf.d' "$out/conf.d" "$out/fonts.conf"
sd --fixed-strings '/var/cache/fontconfig' "/tmp/fontconfig" "$out/fonts.conf"
'';
in
rec {
default = pkgs.buildNpmPackage rec {
pname = nodeManifest.name;
version = nodeManifest.version + revSuffix;
src = lib.sourceByRegex ./. [
"^\.gitignore$"
# need npmignore or else dist isn't copied
"^\.npmignore$"
"^package-lock\.json$"
"^package\.json$"
"^web(/.*)?$"
];
npmConfigHook = pkgs.importNpmLock.npmConfigHook;
npmDeps = pkgs.importNpmLock {
npmRoot = src;
packageSourceOverrides = {
"node_modules/chatsounds-web" = "${wasm}/pkg";
};
};
nativeBuildInputs = (with pkgs; [
# for tests
chatsounds-cli
ffmpeg
procps
])
++ (if dev then
(wasm.nativeBuildInputs ++ (with pkgs; [
clippy
rust-analyzer
(rustfmt.override { asNightly = true; })
])) else [ ]);
buildInputs =
if dev
then wasm.buildInputs
else [ ];
PUPPETEER_SKIP_DOWNLOAD = true;
PUPPETEER_EXECUTABLE_PATH = "${lib.getExe pkgs.chromium}";
postFixup = with pkgs; ''
wrapProgram $out/bin/chatsounds-web \
--prefix PATH : ${lib.makeBinPath [ chatsounds-cli ffmpeg ]}
'';
FONTCONFIG_FILE = "${fontconfig.out}/fonts.conf";
FONTCONFIG_PATH = "${fontconfig.out}/";
doCheck = true;
preCheck =
let
cache-path = (fetched:
builtins.concatStringsSep "/" (
[ "chatsounds" ] ++
builtins.match "(..)(.+)" (
builtins.hashString "sha256" (
builtins.replaceStrings [ "%20" ] [ " " ] fetched.url
)
)
)
);
in
''
mkdir -vp \
"$(dirname ${cache-path hl1-msgpack})" \
"$(dirname ${cache-path hl1-test-sound})"
cp -v ${hl1-msgpack} "${cache-path hl1-msgpack}"
cp -v ${hl1-test-sound} "${cache-path hl1-test-sound}"
stat ${cache-path hl1-msgpack}
stat ${cache-path hl1-test-sound}
chatsounds-cli 'search ah hello gordon freeman its good to see you' | grep 'ah hello gordon freeman its good to see you'
chatsounds-cli 'ah hello gordon freeman its good to see you'
ln -vs ../chatsounds web/chatsounds
'';
checkPhase = ''
runHook preCheck
npm run test
runHook postCheck
'';
meta.mainProgram = "chatsounds-web";
};
wasm = pkgs.rustPlatform.buildRustPackage {
pname = "${rustManifest.package.name}-wasm";
version = rustManifest.package.version + revSuffix;
src = lib.sourceByRegex ./. [
"^\.cargo(/.*)?$"
"^build\.rs$"
"^Cargo\.(lock|toml)$"
"^src(/.*)?$"
];
buildInputs = with pkgs; [
openssl
alsa-lib
];
nativeBuildInputs = with pkgs; [
wasm-pack
(
# wasm-pack requires wasm-bindgen-cli's version to match the one in your Cargo.lock
lib.trivial.throwIf (wasm-bindgen-cli.version != "0.2.95")
"wasm-bindgen-cli updated, bump version here and in Cargo.toml to ${wasm-bindgen-cli.version}"
wasm-bindgen-cli
)
pkg-config
rustc-wasm32
rustc-wasm32.llvmPackages.lld
cargo
rustPlatform.bindgenHook
];
# fix "linker `rust-lld` not found"
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_LINKER = "lld";
buildPhase = ''
wasm-bindgen --version
HOME=$TMPDIR RUST_LOG=info wasm-pack -vvvv build --target web --mode no-install
'';
installPhase = ''
mkdir -p $out
cp -av pkg $out/
'';
cargoLock = {
lockFile = ./Cargo.lock;
allowBuiltinFetchGit = true;
};
};
docker = pkgs.dockerTools.streamLayeredImage {
name = "chatsounds-web";
tag = "latest";
fakeRootCommands = ''
mkdir tmp \
&& chmod -v 1777 tmp
'';
contents = with pkgs; with pkgs.dockerTools; [
default
binSh
caCertificates
coreutils
usrBinEnv
(fakeNss.override {
extraPasswdLines = [ "user:x:1000:1000:user:/tmp:/bin/sh" ];
extraGroupLines = [ "user:x:1000:1000" ];
})
];
config = {
Entrypoint = [
"${pkgs.tini}/bin/tini"
"--"
];
Cmd = [ (lib.getExe default) ];
Env = [
"NODE_ENV=production"
];
ExposedPorts = { "8080/tcp" = { }; };
User = "1000:1000";
WorkingDir = "/tmp";
};
};
}
);
in
builtins.foldl' lib.recursiveUpdate { } (builtins.map
(system: {
devShells.${system} = makePackage system true;
packages.${system} = makePackage system false;
})
lib.systems.flakeExposed);
}