nidus-sync/default.nix

84 lines
2.3 KiB
Nix

{ pkgs ? import <nixpkgs> { }, proj ? pkgs.proj }:
let
# Fetch pnpm dependencies
pnpmDeps = pkgs.pnpm.fetchDeps {
pname = "nidus-sync-frontend";
version = "0.0.11";
src ./.;
hash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="; # nix will tell you the correct hash
};
in
pkgs.buildGoModule rec {
meta = {
description = "Nidus Sync";
homepage = "https://github.com/Gleipnir-Technology/nidus-sync";
};
pname = "nidus-sync";
src = ./.;
subPackages = [];
version = "0.0.11";
# Needs to be updated after every modification of go.mod/go.sum
vendorHash = "sha256-zXjryPAJYpc80cqYtrcp//i6OQi5V5QwhaKQYYfrlL8=";
buildInputs = [ pkgs.proj ];
nativeBuildInputs = [
pkgs.pkg-config
pkgs.dart-sass
pkgs.esbuild
pkgs.pnpm.configHook
pkgs.nodejs
];
pnpmDeps = pnpmDeps;
preBuild = ''
# Setup node_modules from pnpm dependencies
export HOME=$(mktemp -d)
pnpm config set store-dir $HOME/.pnpm-store
# Compile SCSS
SASS_SRC_DIR="./scss"
GEN_OUTPUT_DIR="./static/gen"
mkdir -p "$GEN_OUTPUT_DIR"
echo "Compiling $SASS_SRC_DIR/style.scss to $GEN_OUTPUT_DIR/main.css..."
sass --style=compressed --trace "$SASS_SRC_DIR/style.scss":"$GEN_OUTPUT_DIR/main.css"
# Generate hash and rename style
STYLE_HASH=$(sha256sum "$GEN_OUTPUT_DIR/main.css" | cut -c1-12)
mv "$GEN_OUTPUT_DIR/main.css" "$GEN_OUTPUT_DIR/style.$STYLE_HASH.css"
echo "Generated CSS style with hash: $STYLE_HASH"
# Bundle TypeScript
GEN_OUTPUT_DIR="./static/gen"
mkdir -p "$GEN_OUTPUT_DIR"
echo "Bundling TypeScript with Vue..."
esbuild ts/main.ts \
--bundle \
--minify \
--format=esm \
--define:__VUE_OPTIONS_API__=true \
--define:__VUE_PROD_DEVTOOLS__=false \
--define:__VUE_PROD_HYDRATION_MISMATCH_DETAILS__=false \
--alias:vue=vue/dist/vue.esm-bundler.js \
--outfile="$GEN_OUTPUT_DIR/main.js"
# Generate hash and rename bundle
BUNDLE_HASH=$(sha256sum "$GEN_OUTPUT_DIR/main.js" | cut -c1-12)
mv "$GEN_OUTPUT_DIR/main.js" "$GEN_OUTPUT_DIR/bundle.$BUNDLE_HASH.js"
echo "Generated JS bundle with hash: $BUNDLE_HASH"
# Generate gen.go with bundle path
cat > static/gen.go <<EOF
package static
// Generated by Nix build - do not edit manually
const BundlePathCSS = "/static/gen/css/style.$STYLE_HASH.css"
const BundlePathJS = "/static/gen/js/bundle.$BUNDLE_HASH.js"
EOF
echo "Generated static/gen.go"
'';
}