cdn/flake.nix

102 lines
2.8 KiB
Nix

{
description = "folliehiyuki's stupid CDN";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
gitignore = {
url = "github:hercules-ci/gitignore.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { nixpkgs, flake-utils, gitignore, ... }:
flake-utils.lib.eachSystem [
"x86_64-linux"
"x86_64-darwin"
"aarch64-linux"
"aarch64-darwin"
]
(system:
let
inherit (nixpkgs) lib;
pkgs = nixpkgs.legacyPackages."${system}";
tasks = with pkgs; {
gen = {
runtimeInputs = [ templ ];
script = "templ generate";
};
preview = {
runtimeInputs = [ go templ ];
script = ''
templ generate
go run . -preview result/
'';
};
# NOTE: need to login beforehand, or export CLOUDFLARE_API_TOKEN first
publish = {
runtimeInputs = [ nodePackages.wrangler git ];
script = ''
wrangler pages deploy \
--project-name=folliehiyuki-cdn \
--branch="$(git branch --show-current)" \
result/
'';
};
update-src = {
runtimeInputs = [ curl openssl unzip ];
script = builtins.readFile ./update-src.sh;
};
};
in
{
apps = (lib.attrsets.mapAttrs
(k: v: flake-utils.lib.mkApp {
drv = pkgs.writeShellApplication {
name = k;
runtimeInputs = v.runtimeInputs;
text = v.script;
};
})
tasks);
packages = rec {
default = bundle;
bundle = with pkgs; stdenv.mkDerivation {
buildInputs = [ templ go ];
name = "cdn.folliehiyuki.com";
src = lib.cleanSource (gitignore.lib.gitignoreSource ./.);
configurePhase = ''
export GOTOOLCHAIN=local
export HOME="$TMPDIR"
export GOCACHE="$PWD/go-cache"
export GOTMPDIR="$PWD"
export GOMODCACHE="$PWD/go"
templ generate
'';
buildPhase = ''
go run . -minify -gen src
'';
installPhase = ''
mkdir -p "$out"
cp -r ./src/* "$out"/
'';
};
};
devShells.default = with pkgs; mkShell {
packages = [ nodePackages.wrangler templ go ];
shellHook = ''
export GOTOOLCHAIN=local
'';
};
}
);
}