Skip to content

Commit

Permalink
initial commit: squash history
Browse files Browse the repository at this point in the history
  • Loading branch information
denbeigh2000 committed Apr 3, 2023
0 parents commit 658d745
Show file tree
Hide file tree
Showing 31 changed files with 4,432 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
/wrangler*.toml
*.env
/dist/
/result*
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# denbeigh-bot

## What

This is the bot I use to manage my personal Discord server.

It provides:
- a tiered user-privilege system
- user-configurable groups (roless)
- user approval with a waiting room
- guest inviting

I run it on CloudFlare Workers free tier, and build/deploy it using Nix and Wrangler.

The bundle is stored in the Nix store to take advantage of reusability, remote
caching, etc.

## Why

I hadn't seen any good examples of using Wrangler via Nix, and I thought this
may be useful to others.

## How

Build: `nix build .#workerBundle`
Deploy (requires credentials): `nix run .#releaseTool [staging|production]`

## Note

In case you are setting this up for your own server:

I've made several configurations on my own server that this assumes (e.g.,
permissions, role names, etc.). As this is a practical tool for managing my
own server, I haven't documented these and don't really plan to.
47 changes: 47 additions & 0 deletions build.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{ stdenvNoCC
, writeShellScript
, age
, git
, sentry-cli
, nodeModules
}:

let
stubWranglerToml = ''
compatibility_date = \"2023-03-02\"
'';

workerBundle = stdenvNoCC.mkDerivation {
pname = "denbeigh-bot-cfworker-bundle";
version = "0.0.0";

src = ./.;

# IDEA: In this step, we use wrangler's bundling process via publish --dry-run
# to run the provided minification + bundling, then write that to the Nix
# store. We can then publish in the next step by running
# publish --no-bundle. This may also require a custom build step(?)
buildPhase = ''
WRANGLER_BIN="${nodeModules}/node_modules/.bin/wrangler2"
STUB_WRANGLER_TOML="${stubWranglerToml}"
NODE_MODULES_PATH="${nodeModules}"
${builtins.readFile ./build.sh}
'';
};
in

{
inherit workerBundle;
releaseTool = writeShellScript "release.sh" ''
set -euo pipefail
WRANGLER_BIN="${nodeModules}/node_modules/.bin/wrangler2"
AGE_BIN="${age}/bin/age"
GIT_BIN="${git}/bin/git"
SENTRY_BIN="${sentry-cli}/bin/sentry-cli"
BUNDLED_WORKER_PATH="${workerBundle}"
${builtins.readFile ./release.sh}
'';
}
17 changes: 17 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"$WRANGLER_BIN" --version 2>&1 >/dev/null

WRANGLER_TOML="$(mktemp)"
echo "$STUB_WRANGLER_TOML" > "$WRANGLER_TOML"

ln -s $NODE_MODULES_PATH/node_modules node_modules

"$WRANGLER_BIN" publish \
--name local-build \
--config "$WRANGLER_TOML" \
--minify \
--dry-run \
--outdir dist \
src/index.ts

mkdir -p $out
mv dist/index.js dist/index.js.map $out/
37 changes: 37 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ callPackage
, mkShell
, devPackages
, neovim
, yarn
, sentry-cli
, pre-commit
, age
, yarn2nix-moretea
}:

let
# nodeModules = js2nix.makeNodeModules ./package.json {
# tree = js2nix.load ./yarn.lock {};
# };
nodeModules = yarn2nix-moretea.mkYarnModules {
pname = "cf-worker-deps";
version = "0.0.0";
packageJSON = ./package.json;
yarnLock = ./yarn.lock;
};

worker = callPackage ./build.nix { inherit nodeModules; };
in
{
shell = mkShell {
packages = devPackages.node.allNode18 ++ [
age
neovim
yarn
sentry-cli
pre-commit
];
};

inherit (worker) workerBundle releaseTool;
}
193 changes: 193 additions & 0 deletions flake.lock

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

50 changes: 50 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
description = "Denbeigh's discord bot on CF workers";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";

flake-utils.url = "github:numtide/flake-utils";

# NOTE: We can't use this due to esbuild needing to chmod in post-install
# TODO: Write an isolated-ish bug report
# js2nix = {
# url = "github:canva-public/js2nix";
# flake = false;
# };

denbeigh = {
url = "github:denbeigh2000/nix-dev";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs = { self, nixpkgs, flake-utils, denbeigh }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ denbeigh.overlays.default ];
};

pkg = pkgs.callPackage ./default.nix { };

inherit (pkg) shell releaseTool;
in
{
apps.releaseTool = {
type = "app";
program = "${releaseTool}";
};

devShells = {
default = shell;
dev = shell;
};

packages = {
inherit (pkg) workerBundle releaseTool;
};
}
);
}
Loading

0 comments on commit 658d745

Please sign in to comment.