Skip to content

Commit

Permalink
nix: update flake
Browse files Browse the repository at this point in the history
  • Loading branch information
SpiralP committed Feb 29, 2024
1 parent 57defb4 commit bea603d
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 275 deletions.
388 changes: 200 additions & 188 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ serde-wasm-bindgen = "0.6"
# nix's wasm-pack requires wasm-bindgen's version to match this one;
# the docker version will download the correct version on demand.
# nix run github:NixOS/nixpkgs/nixos-23.05#wasm-bindgen-cli -- --version
wasm-bindgen = "=0.2.84"
wasm-bindgen = "=0.2.89"
wasm-bindgen-futures = "0.4"
web-sys = { version = "0.3", features = ["console"] }

Expand Down
36 changes: 8 additions & 28 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 52 additions & 49 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,65 +1,45 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";
nixpkgs-mozilla.url = "github:mozilla/nixpkgs-mozilla/master";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
chatsounds-cli-repo = {
url = "github:SpiralP/chatsounds-cli/master";
inputs.nixpkgs.follows = "nixpkgs";
inputs.nixpkgs-mozilla.follows = "nixpkgs-mozilla";
};
};

outputs = { nixpkgs, nixpkgs-mozilla, chatsounds-cli-repo, ... }:
outputs = { nixpkgs, chatsounds-cli-repo, ... }:
let
inherit (nixpkgs) lib;

makePackage = (system: dev:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ nixpkgs-mozilla.overlays.rust ];
};
chatsounds-cli = chatsounds-cli-repo.outputs.packages.${system}.default;

rust = (pkgs.rustChannelOf {
channel = "1.74.0";
sha256 = "sha256-U2yfueFohJHjif7anmJB5vZbpP7G6bICH4ZsjtufRoU=";
}).rust.override {
extensions = if dev then [ "rust-src" ] else [ ];
targets = [ "wasm32-unknown-unknown" ];
};
rustPlatform = pkgs.makeRustPlatform {
cargo = rust;
rustc = rust;
};

src = lib.cleanSourceWith rec {
src = ./.;
filter = path: type:
lib.cleanSourceFilter path type
&& (
let
baseName = builtins.baseNameOf (builtins.toString path);
relPath = lib.removePrefix (builtins.toString ./.) (builtins.toString path);
in
lib.any (re: builtins.match re relPath != null) [
"/Cargo.lock"
"/Cargo.toml"
"/package-lock.json"
"/package.json"
"/src"
"/src/.*"
"/web"
"/web/.*"
]
);
};
in
rec {
wasm = pkgs.stdenv.mkDerivation {
pname = "chatsounds-web-wasm";
version = "0.0.1";
inherit src;

src = lib.cleanSourceWith rec {
src = ./.;
filter = path: type:
lib.cleanSourceFilter path type
&& (
let
baseName = builtins.baseNameOf (builtins.toString path);
relPath = lib.removePrefix (builtins.toString ./.) (builtins.toString path);
in
lib.any (re: builtins.match re relPath != null) [
"/Cargo.lock"
"/Cargo.toml"
"/src"
"/src/.*"
]
);
};

buildPhase = ''
wasm-bindgen --version
Expand All @@ -71,7 +51,7 @@
cp -av pkg $out/
'';

cargoDeps = rustPlatform.importCargoLock {
cargoDeps = pkgs.rustPlatform.importCargoLock {
lockFile = ./Cargo.lock;
outputHashes = {
"chatsounds-0.2.0" = "sha256-HJq5MXkXnEKGOHX+DRzVhQjLTPmar0MWW7aItqrlpys=";
Expand All @@ -83,10 +63,14 @@
# wasm-pack requires wasm-bindgen-cli's version to match the one in your Cargo.lock
wasm-bindgen-cli
pkg-config
rust
rustc-wasm32
rustc-wasm32.llvmPackages.lld
cargo
rustPlatform.bindgenHook
rustPlatform.cargoSetupHook
];
# fix "linker `rust-lld` not found"
CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_LINKER = "lld";

buildInputs = with pkgs; [
openssl
Expand All @@ -96,29 +80,50 @@

default = pkgs.buildNpmPackage {
name = "chatsounds-web";
inherit src;

npmDepsHash = "sha256-ftvVNvMF/CIWC5Zv0nKhxlkrssxERh2eu7eUDlqcGZA=";
src = lib.cleanSourceWith rec {
src = ./.;
filter = path: type:
lib.cleanSourceFilter path type
&& (
let
baseName = builtins.baseNameOf (builtins.toString path);
relPath = lib.removePrefix (builtins.toString ./.) (builtins.toString path);
in
lib.any (re: builtins.match re relPath != null) [
"/package-lock.json"
"/package.json"
"/web"
"/web/.*"
]
);
};

npmDepsHash = "sha256-SdwNPEpN+Vjwc1mX7szWlGFlgYMUkZBSPy8ib6lGNZg=";

preBuild = ''
ln -vsf ${wasm}/pkg ./node_modules/chatsounds-web
'';

postInstall = with pkgs; ''
wrapProgram $out/bin/discord-embed-proxy \
wrapProgram $out/bin/chatsounds-web \
--prefix PATH : ${lib.makeBinPath [ chatsounds-cli ffmpeg ]}
'';

nativeBuildInputs = with pkgs; if dev then
(wasm.nativeBuildInputs ++ [
chatsounds-cli
ffmpeg
clippy
rustfmt
rust-analyzer
]) else [ ];

buildInputs = with pkgs; if dev then
wasm.buildInputs else [ ];
};

meta.mainProgram = "chatsounds-web";
};

docker = pkgs.dockerTools.streamLayeredImage {
name = "chatsounds-web";
Expand Down Expand Up @@ -146,9 +151,7 @@
"${pkgs.tini}/bin/tini"
"--"
];
Cmd = [
"discord-embed-proxy"
];
Cmd = [ (lib.getExe default) ];
Env = [
"NODE_ENV=production"
];
Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"private": true,
"type": "module",
"bin": {
"discord-embed-proxy": "./dist-node/discord-embed-proxy.js"
"chatsounds-web": "./dist-node/discord-embed-proxy.js"
},
"scripts": {
"start": "cd web && ts-node-esm discord-embed-proxy.ts",
"start": "cd web && node --loader ts-node/esm discord-embed-proxy.ts",
"dev": "npm run build:js dev",
"build": "cd web && concurrently --raw \"cross-env NODE_ENV=production npm run build:js\" \"cross-env NODE_ENV=production npm run build:node\"",
"build:js": "rimraf dist && cd web && ts-node-esm build.ts",
"build:node": "rimraf dist-node && cd web && ts-node-esm build-node.ts",
"build:js": "rimraf dist && cd web && node --loader ts-node/esm build.ts",
"build:node": "rimraf dist-node && cd web && node --loader ts-node/esm build-node.ts",
"build:wasm": "cross-env RUST_LOG=info wasm-pack -vvvv build --target web",
"typecheck": "cd web && tsc --noEmit",
"format": "cd web && prettier --write --ignore-path ../.gitignore .",
Expand Down
11 changes: 9 additions & 2 deletions web/discord-embed-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ const DISCORD_USER_AGENT =
const VIDEO_USER_AGENT =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0";

// ./dist-node/(discord-embed-proxy.ts)
const DIR_NAME = path.dirname(fileURLToPath(import.meta.url));
// ./
const PROJECT_DIR = path.resolve(DIR_NAME, "..");
// ./dist
const DIST_DIR = path.join(PROJECT_DIR, "dist");
if (!fs.existsSync(DIST_DIR)) {
throw new Error("!dist");
}

const CHATSOUNDS_CLI =
process.platform === "win32" ? "chatsounds-cli.exe" : "chatsounds-cli";
Expand Down Expand Up @@ -105,7 +112,7 @@ const getChatsoundBuffer = memoizee(
// cache for 1 minute
maxAge: 1000 * 60,
promise: true,
},
}
);

async function respondMedia(sentence: string, ext: Extension, res: Response) {
Expand Down Expand Up @@ -169,7 +176,7 @@ app.get("/media.mp4", async (req, res) => {
await respondMedia(input, "mp4", res);
});

app.use(express.static(path.join(PROJECT_DIR, "dist")));
app.use(express.static(DIST_DIR));

app.listen(PORT, () => {
console.log(`app listening on port ${PORT}`);
Expand Down
3 changes: 1 addition & 2 deletions web/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@
"src/**/*",
"discord-embed-proxy.ts",
"build.ts",
"build-node.ts",
"index.ts"
"build-node.ts"
],
"exclude": [".eslintrc.cjs"]
}

0 comments on commit bea603d

Please sign in to comment.