forked from denbeigh2000/denbeigh-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 658d745
Showing
31 changed files
with
4,432 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules/ | ||
/wrangler*.toml | ||
*.env | ||
/dist/ | ||
/result* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
} | ||
); | ||
} |
Oops, something went wrong.