towerops/nix/build.nix
Graham McInitre 33cb8ca769 fix: auto-reinit PG cluster when extensions or version change
- Write pg derivation path to .nix-postgres-version after init
- On start, compare current pg path with stored version
- If changed (extensions added/removed/version bumped), destroy and
  reinitialize the cluster automatically
- Also reinit legacy clusters that have no version file
- Exclude .nix-postgres-version from release builds
2026-07-16 09:02:42 -05:00

115 lines
2.9 KiB
Nix

{
lib,
beamPackages,
towerops-nif,
net-snmp,
writeShellScriptBin,
git,
cacert,
}:
beamPackages.mixRelease {
pname = "towerops";
version = "0.1.0";
# Source files (filtered to exclude build artifacts)
src = lib.cleanSourceWith {
src = ../.;
filter =
path: type:
let
baseName = baseNameOf path;
in
# Exclude build artifacts and temp directories
baseName != "_build"
&& baseName != "deps"
&& baseName != ".elixir_ls"
&& baseName != ".elixir-tools"
&& baseName != "node_modules"
&& baseName != "cover"
&& baseName != ".git"
&& baseName != ".nix-postgres"
&& baseName != ".nix-redis"
&& baseName != ".nix-mix"
&& baseName != ".nix-hex"
&& baseName != ".worktrees"
&& baseName != ".nix-services-started"
&& baseName != ".nix-postgres-version";
};
# Mix environment
mixEnv = "prod";
# Environment variables for build
MIX_OS_DEPS_COMPILE_PARTITION_COUNT = "6";
# Need git and SSL certificates for fetching git dependencies
nativeBuildInputs = [
git
cacert
];
# Override Mix phases to handle vendored deps properly
configurePhase = ''
runHook preConfigure
# Set SSL certificate path for git operations
export SSL_CERT_FILE="${cacert}/etc/ssl/certs/ca-bundle.crt"
export GIT_SSL_CAINFO="${cacert}/etc/ssl/certs/ca-bundle.crt"
# Disable Hex offline mode to allow fetching dependencies
export HEX_OFFLINE=false
# Get all deps (including hex deps that vendored packages need)
mix deps.get --only prod
runHook postConfigure
'';
# Pre-build: Copy pre-built NIF and stub the Makefile to skip rebuild
preBuild = ''
# Copy pre-built NIF shared library
mkdir -p priv
cp ${towerops-nif}/lib/towerops_nif.so priv/
# Stub the c_src/Makefile to prevent rebuild
cat > c_src/Makefile << 'EOF'
# Stubbed Makefile - NIF pre-built by Nix
all:
@echo "Using pre-built NIF from ${towerops-nif}"
clean:
@echo "NIF managed by Nix, nothing to clean"
.PHONY: all clean
EOF
'';
# Custom build phase to compile deps first, then app
buildPhase = ''
runHook preBuild
# Compile dependencies (including custom compilers like phoenix_live_view)
mix deps.compile
# Compile application
mix compile
runHook postBuild
'';
# Build assets after Mix compilation
postBuild = ''
# Build production assets (Tailwind + esbuild)
mix assets.deploy
'';
# MIB files and NIF are automatically included by Mix release
# Mix includes everything in priv/ directory when building the release
# No postInstall needed - NIF was copied to priv/ in preBuild
# and MIB files are already in priv/mibs/ from source
meta = {
description = "Towerops - Phoenix LiveView SNMP monitoring platform";
license = lib.licenses.unfree;
platforms = lib.platforms.unix;
};
}