Add flake.nix

This commit is contained in:
Leo 2026-08-21 22:13:08 +03:00
parent ff2d6f9c9c
commit fe0363746b
2 changed files with 70 additions and 0 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@
main
tmp/
build-errors.log
result

69
flake.nix Normal file
View file

@ -0,0 +1,69 @@
{
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_24;
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)"
'';
};
formatter = pkgs.nixpkgs-fmt;
}
);
}