diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 2b4422e7..4322d55a 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -1,31 +1,31 @@ -name: Check Types + name: Check Types -on: - push: - branches: ["*"] - pull_request: - # The branches below must be a subset of the branches above - branches: ["main"] + on: + push: + branches: ["*"] + pull_request: + # The branches below must be a subset of the branches above + branches: ["main"] -jobs: - tests: - runs-on: ubuntu-latest - permissions: - contents: read + jobs: + tests: + runs-on: ubuntu-latest + permissions: + contents: read - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - submodules: recursive + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + submodules: recursive - - name: Setup Bun - uses: oven-sh/setup-bun@v2 + - name: Setup Bun + uses: oven-sh/setup-bun@v2 - - name: Install NPM packages - run: | - bun install + - name: Install NPM packages + run: | + bun install - - name: Run typechecks - run: | - bun run check + - name: Run typechecks + run: | + bun run check diff --git a/.github/workflows/staging.yml b/.github/workflows/staging.yml new file mode 100644 index 00000000..22a9c928 --- /dev/null +++ b/.github/workflows/staging.yml @@ -0,0 +1,50 @@ +name: Staging build bundle + +on: + push: + branches: ["staging"] + +jobs: + tests: + runs-on: ubuntu-latest + permissions: + contents: read + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Setup Bun + uses: oven-sh/setup-bun@v2 + + - name: Install NPM packages + run: | + bun install + + - name: Build dist + run: | + bun run build + + - name: Bundle + run: | + mkdir bundle + cp -r dist bundle/ + cp -r config bundle/ + cp -r docs bundle/ + cp -r CODE_OF_CONDUCT.md bundle/ + cp -r CONTRIBUTING.md bundle/ + cp -r README.md bundle/ + cp -r flake.nix bundle/ + cp -r shell.nix bundle/ + cp -r flake.lock bundle/ + cp -r LICENSE bundle/ + cp -r SECURITY.md bundle/ + tar cfJ archive.tar.xz bundle/ + + - name: Upload + uses: actions/upload-artifact@v4 + with: + name: staging-dist + path: archive.tar.xz diff --git a/.gitignore b/.gitignore index b378a2e4..79f66ab2 100644 --- a/.gitignore +++ b/.gitignore @@ -117,6 +117,10 @@ out .nuxt dist +# Nix build output + +result + # Gatsby files .cache/ @@ -182,4 +186,4 @@ glitch-dev *.pem oclif.manifest.json .direnv/ -tsconfig.tsbuildinfo \ No newline at end of file +tsconfig.tsbuildinfo diff --git a/flake.lock b/flake.lock index fb00c420..655e86c9 100644 --- a/flake.lock +++ b/flake.lock @@ -1,26 +1,30 @@ { "nodes": { - "flake-compat": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, "locked": { - "lastModified": 1696426674, - "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", - "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", - "revCount": 57, - "type": "tarball", - "url": "https://api.flakehub.com/f/pinned/edolstra/flake-compat/1.0.1/018afb31-abd1-7bff-a5e4-cff7e18efb7a/source.tar.gz" + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" }, "original": { - "type": "tarball", - "url": "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz" + "owner": "numtide", + "repo": "flake-utils", + "type": "github" } }, "nixpkgs": { "locked": { - "lastModified": 1722957468, - "narHash": "sha256-SQ0TCC4aklOhN/OzcztrKqDLY8SjpIZcyvTulzhDXs0=", + "lastModified": 1723703277, + "narHash": "sha256-nk0RaUB5f68BwtXAYy3WAjqFhVKqIl9Z89RGycTa2vk=", "owner": "nixos", "repo": "nixpkgs", - "rev": "2a13929e1f191b3690dd2f2db13098b04adb9043", + "rev": "8b908192e64224420e2d59dfd9b2e4309e154c5d", "type": "github" }, "original": { @@ -32,9 +36,24 @@ }, "root": { "inputs": { - "flake-compat": "flake-compat", + "flake-utils": "flake-utils", "nixpkgs": "nixpkgs" } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index fd5ebd8d..fadc8cba 100644 --- a/flake.nix +++ b/flake.nix @@ -1,97 +1,36 @@ { - description = "JavaScript example flake for Zero to Nix"; + description = "Lysand Server"; inputs = { nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; - flake-compat.url = "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz"; + flake-utils.url = "github:numtide/flake-utils"; }; - outputs = { self, nixpkgs, flake-compat }: - let - # Systems supported - allSystems = [ - "x86_64-linux" # 64-bit Intel/AMD Linux - "aarch64-linux" # 64-bit ARM Linux - "x86_64-darwin" # 64-bit Intel macOS - "aarch64-darwin" # 64-bit ARM macOS - ]; - - # Helper to provide system-specific attributes - forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f { - pkgs = import nixpkgs { inherit system; }; - }); - in - { - packages = forAllSystems ({ pkgs }: { - default = let - pin = pkgs.lib.importJSON ./pin.json; - src = self; - node_modules = pkgs.stdenv.mkDerivation { - pname = "versiajs-node_modules"; - inherit src; - version = pin.version; - impureEnvVars = pkgs.lib.fetchers.proxyImpureEnvVars - ++ [ "GIT_PROXY_COMMAND" "SOCKS_SERVER" ]; - nativeBuildInputs = with pkgs; [ bun nodejs nodePackages.typescript nodePackages.typescript-language-server ]; - buildInputs = with pkgs; [ libstdcxx5 ]; - dontConfigure = true; - buildPhase = '' - bun install --production --frozen-lockfile --ignore-scripts - ''; - installPhase = '' - mkdir -p $out/node_modules - cp -R ./node_modules $out - ''; - outputHash = pin."${pkgs.stdenv.system}"; - outputHashAlgo = "sha256"; - outputHashMode = "recursive"; - dontFixup = true; - }; - in pkgs.stdenv.mkDerivation { - name = "versiajs"; - version = pin.version; - - buildPhase = '' - runHook preBuild - - ln -s ${node_modules}/node_modules . - bun run build - - runHook postBuild - ''; - - installPhase = '' - runHook preInstall - mkdir -p $out/bin - cp -R ./* $out - makeBinaryWrapper ${pkgs.bun}/bin/bun $out/bin/versiajs \ - --prefix PATH : ${pkgs.lib.makeBinPath [ pkgs.bun ]} \ - --add-flags "run --prefer-offline --no-install --cwd $out ./cli/index.ts start" - - runHook postInstall - ''; - - buildInputs = with pkgs; [ - nodejs - ]; - - nativeBuildInputs = with pkgs; [ makeBinaryWrapper bun ]; - - inherit src; - - dontConfigure = true; - }; - }); - devShells = forAllSystems ({ pkgs }: { - default = pkgs.mkShell { - buildInputs = with pkgs; [ - bun - nodejs - nodePackages.typescript - nodePackages.typescript-language-server - nix-ld - ]; - }; - }); + outputs = { self, nixpkgs, flake-utils }: { + hydraJobs = { + inherit (self) packages; }; + } // flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + }; + in { + packages = { + versiajs = pkgs.callPackage ./nix/package.nix {}; + default = self.packages.${system}.versiajs; + }; + + devShells = { + default = pkgs.mkShell { + buildInputs = with pkgs; [ + bun + nodejs + nodePackages.typescript + nodePackages.typescript-language-server + nix-ld + ]; + }; + }; + }); } diff --git a/nix/fix-build-spinner.patch b/nix/fix-build-spinner.patch new file mode 100644 index 00000000..7445c3d1 --- /dev/null +++ b/nix/fix-build-spinner.patch @@ -0,0 +1,33 @@ +diff --git a/build.ts b/build.ts +index 3b3ebe9..9531121 100644 +--- a/build.ts ++++ b/build.ts +@@ -1,10 +1,10 @@ + import { $ } from "bun"; +-import ora from "ora"; ++// import ora from "ora"; + import { routes } from "~/routes"; + +-const buildSpinner = ora("Building").start(); ++// const buildSpinner = ora("Building").start(); + +-await $`rm -rf dist && mkdir dist`; ++// await $`rm -rf dist && mkdir dist`; + + await Bun.build({ + entrypoints: [ +@@ -25,7 +25,7 @@ await Bun.build({ + } + }); + +-buildSpinner.text = "Transforming"; ++// buildSpinner.text = "Transforming"; + + // Copy Drizzle migrations to dist + await $`cp -r drizzle dist/drizzle`; +@@ -49,4 +49,4 @@ await $`cp package.json dist/package.json`; + // Copy cli/theme.json + await $`cp cli/theme.json dist/cli/theme.json`; + +-buildSpinner.stop(); ++// buildSpinner.stop(); diff --git a/nix/nodeHashes.nix b/nix/nodeHashes.nix new file mode 100644 index 00000000..df2b68d4 --- /dev/null +++ b/nix/nodeHashes.nix @@ -0,0 +1,6 @@ +{ lib }: { + x86_64-linux = "sha256-T/U9altP5HFzmULtsuvHQIXQXDCmQEAau8KFN8J5i/8="; + x86_64-darwin = lib.fakeHash; + aarch64-linux = "sha256-6ZzrYI2G+7q9Efgu5iKhZB3bT2C7T5fk4I/t5glpQYA="; + aarch64-darwin = lib.fakeHash; +} diff --git a/nix/package.nix b/nix/package.nix new file mode 100644 index 00000000..947f9732 --- /dev/null +++ b/nix/package.nix @@ -0,0 +1,90 @@ +{ + lib, + stdenv, + fetchFromGitHub, + bun, + callPackage, + nodeHashes ? callPackage ./nodeHashes.nix { inherit lib; }, + nodePackages_latest, + makeBinaryWrapper, +}: + +stdenv.mkDerivation (finalAttrs: { + pname = "versiajs"; + version = "0.7.0"; + + src = ../.; + + node_modules = stdenv.mkDerivation { + pname = "${finalAttrs.pname}-node_modules"; + + inherit (finalAttrs) version src; + + nativeBuildInputs = with nodePackages_latest; [ bun nodejs typescript ]; + + dontConfigure = true; + + buildPhase = '' + bun install --production --no-progress --ignore-scripts --frozen-lockfile + ''; + + installPhase = '' + mkdir -p $out/node_modules + cp -r node_modules $out + ''; + + dontFixup = true; + + outputHash = nodeHashes.${stdenv.system}; + outputHashMode = "recursive"; + }; + + nativeBuildInputs = [ bun ]; + + buildInputs = [ bun nodePackages_latest.nodejs makeBinaryWrapper ]; + + patches = [ ./fix-build-spinner.patch ]; + + configurePhase = '' + runHook preConfigure + + cp -r ${finalAttrs.node_modules}/node_modules . + + runHook postConfigure + ''; + + buildPhase = '' + runHook preBuild + + bun run build + + runHook postBuild + ''; + + installPhase = '' + runHook preInstall + + cp -r dist/ $out + + mkdir -p $out/bin + + makeBinaryWrapper ${bun}/bin/bun $out/bin/versiajs \ + --prefix PATH : ${lib.makeBinPath [ bun ]} \ + --set NODE_ENV "production" \ + --add-flags "run --prefer-offline --no-install --cwd $out $out/index.js" + + makeBinaryWrapper ${bun}/bin/bun $out/bin/versiajs-cli \ + --prefix PATH : ${lib.makeBinPath [ bun ]} \ + --add-flags "run --prefer-offline --no-install --cwd $out $out/cli/index.js" + + runHook postInstall + ''; + + meta = { + description = "A new federated server written with TypeScript and Bun "; + homepage = "https://lysand.org"; + license = with lib.licenses; [ agpl3Plus ]; + maintainers = with lib.maintainers; [ snaki ]; + platforms = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ]; + }; +})