Created
December 15, 2025 22:05
-
-
Save vai/90980f359a2e41d7795349aa55f7e791 to your computer and use it in GitHub Desktop.
a ziggy flake
This file contains hidden or 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
| # flake.nix | |
| { | |
| description = "Customizable Zig project with service isolation"; | |
| inputs = { | |
| nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; | |
| zig-overlay.url = "github:mitchellh/zig-overlay"; | |
| flake-utils.url = "github:numtide/flake-utils"; | |
| }; | |
| outputs = { self, nixpkgs, flake-utils, zig-overlay }: | |
| flake-utils.lib.eachDefaultSystem (system: | |
| let | |
| pkgs = nixpkgs.legacyPackages.${system}; | |
| appName = "hadeda"; # <-- Single variable for application name | |
| in | |
| { | |
| devShells.default = pkgs.mkShell { | |
| packages = with pkgs; [ | |
| zig-overlay.packages.x86_64-linux."0.15.2" | |
| zls | |
| postgresql | |
| valkey | |
| direnv | |
| gnused | |
| jujutsu | |
| ]; | |
| shellHook = '' | |
| export APP_NAME="${appName}" | |
| export PG_DATA="$PWD/.postgres_data" | |
| export PG_SOCKET_DIR="$PG_DATA/sockets" | |
| export PG_PORT=5434 | |
| export VALKEY_PORT=6383 | |
| export VALKEY_DATA_DIR="$PWD/.valkey_data" | |
| export ZIG_GLOBAL_CACHE_DIR="$PWD/.zig-cache" | |
| export APP_PORT=9009 | |
| export JETZIG_SECRET=EK6yAO... | |
| # Create required directories | |
| mkdir -p $PG_DATA $VALKEY_DATA_DIR $ZIG_GLOBAL_CACHE_DIR | |
| # Initialize Postgres with custom port | |
| if [ ! -d "$PG_DATA/base" ]; then | |
| initdb -D $PG_DATA | |
| mkdir -p $PG_DATA/sockets | |
| echo "unix_socket_directories = '$PG_SOCKET_DIR'" >> $PG_DATA/postgresql.conf | |
| echo "port = $PG_PORT" >> $PG_DATA/postgresql.conf | |
| echo "local all all trust" >> $PG_DATA/pg_hba.conf | |
| echo "host all all 127.0.0.1/32 trust" >> $PG_DATA/pg_hba.conf | |
| pg_ctl -D $PG_DATA -o "-k $PG_SOCKET_DIR" start | |
| createuser -h $PG_SOCKET_DIR -p $PG_PORT -s postgres # Create default superuser | |
| createdb -h $PG_SOCKET_DIR -p $PG_PORT -O postgres ${appName}_db | |
| echo "Creating Database" | |
| pg_ctl -D $PG_DATA stop | |
| fi | |
| # Start services with custom ports | |
| valkey-cli -p $VALKEY_PORT shutdown | |
| pg_ctl -D $PG_DATA -o "-k $PG_SOCKET_DIR -p $PG_PORT" -l $PG_DATA/postgres.log start | |
| valkey-server --dir $VALKEY_DATA_DIR --port $VALKEY_PORT --daemonize yes | |
| # Set environment variables | |
| export DATABASE_URL="postgresql://postgres@/${appName}_db?host=$PG_SOCKET_DIR&port=$PG_PORT" | |
| export REDIS_URL="redis://localhost:$VALKEY_PORT" | |
| export $(grep -v '^#' .env | xargs) | |
| # Wait for services to be ready | |
| until pg_isready -h $PG_SOCKET_DIR -p $PG_PORT; do sleep 1; done | |
| # Cleanup on exit | |
| trap " | |
| pg_ctl -D $PG_DATA -o '-k $PG_SOCKET_DIR -p $PG_PORT' stop | |
| valkey-cli -p $VALKEY_PORT shutdown | |
| " EXIT | |
| ''; | |
| }; | |
| packages.default = pkgs.stdenv.mkDerivation { | |
| name = "${appName}"; | |
| src = ./.; | |
| nativeBuildInputs = [ pkgs.zig ]; | |
| buildPhase = '' | |
| zig build -Doptimize=ReleaseSafe | |
| ''; | |
| installPhase = '' | |
| install -Dm755 zig-out/bin/${appName} -t $out/bin | |
| ''; | |
| }; | |
| }); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment