85 lines
2.6 KiB
Nix
85 lines
2.6 KiB
Nix
{
|
|
description = "Go backend template — Go HTTP server, PostgreSQL, sessions, HTMX/Alpine.js frontend";
|
|
|
|
inputs = {
|
|
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
|
flake-utils.url = "github:numtide/flake-utils";
|
|
};
|
|
|
|
outputs =
|
|
{ self, nixpkgs, flake-utils }:
|
|
flake-utils.lib.eachDefaultSystem (
|
|
system:
|
|
let
|
|
pkgs = import nixpkgs { inherit system; };
|
|
go = pkgs.go_1_27;
|
|
in
|
|
{
|
|
packages.default = pkgs.buildGoModule {
|
|
pname = "go-backend";
|
|
version = "0.1.0";
|
|
src = ./.;
|
|
|
|
subPackages = [ "cmd" ];
|
|
|
|
# `go.sum` only pins 3 modules today, but this hash still has to
|
|
# match nix's vendor fetch. `nix build` will fail on a mismatch and
|
|
# print the correct value to paste in here.
|
|
vendorHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
|
|
|
|
nativeBuildInputs = [ pkgs.makeWrapper ];
|
|
|
|
# The server reads templates/static assets from a relative "./ui"
|
|
# path (see internal/handlers/templates.go, cmd/main.go), so ship
|
|
# that directory next to the binary and cd there before exec,
|
|
# mirroring what the Dockerfile does.
|
|
postInstall = ''
|
|
mkdir -p $out/share/go-backend
|
|
cp -r ${./ui} $out/share/go-backend/ui
|
|
mv $out/bin/cmd $out/bin/go-backend
|
|
wrapProgram $out/bin/go-backend --run "cd $out/share/go-backend"
|
|
'';
|
|
|
|
meta.mainProgram = "go-backend";
|
|
};
|
|
|
|
apps.default = flake-utils.lib.mkApp { drv = self.packages.${system}.default; };
|
|
|
|
devShells.default = pkgs.mkShell {
|
|
packages = [
|
|
go
|
|
pkgs.gopls
|
|
pkgs.gotools
|
|
pkgs.delve
|
|
pkgs.goose
|
|
pkgs.sqlc
|
|
pkgs.air
|
|
pkgs.postgresql_16
|
|
pkgs.gnumake
|
|
];
|
|
|
|
shellHook = ''
|
|
echo "go-backend dev shell — $(go version)"
|
|
|
|
export PGDATA="$PWD/.devdata/pgdata"
|
|
export PGHOST="$PWD/.devdata/pgsocket"
|
|
|
|
mkdir -p "$PGHOST"
|
|
|
|
if [ ! -d "$PGDATA" ]; then
|
|
echo "Initializing Postgres data dir..."
|
|
initdb --auth=trust --no-locale --encoding=UTF8 -U postgres >/dev/null
|
|
fi
|
|
|
|
pg_ctl status >/dev/null 2>&1 || pg_ctl start -l "$PWD/.devdata/pg.log" -o "-k $PGHOST -h 127.0.0.1 -p 5432"
|
|
|
|
createdb -U postgres -h 127.0.0.1 backend_template 2>/dev/null || true
|
|
|
|
alias pg_stop="pg_ctl stop"
|
|
'';
|
|
};
|
|
|
|
formatter = pkgs.nixpkgs-fmt;
|
|
}
|
|
);
|
|
}
|