vegan-barcode/flake.nix
2025-03-29 19:06:07 -05:00

77 lines
2.4 KiB
Nix

{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
};
outputs = {
self,
nixpkgs,
...
}: let
# System types to support.
supportedSystems = ["x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin"];
# Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'.
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
# Nixpkgs instantiated for supported system types.
nixpkgsFor = forAllSystems (system: import nixpkgs {inherit system;});
in {
devShells = forAllSystems (system: let
pkgs = nixpkgsFor.${system};
startPostgres = pkgs.writeScriptBin "postgres_start" ''
background() {
exec 0>&-
exec 1>&-
exec 2>&-
exec 3>&-
"$@" &
disown $!
}
echo "Starting postgres server..."
# start postgres server
background ${pkgs.postgresql}/bin/pg_ctl -l $DB_LOG -o "--unix_socket_directories='$PWD'" start
echo "Postgres server started."
'';
in {
default = pkgs.mkShell {
buildInputs = with pkgs; [go gopls gotools go-tools postgresql];
PGDATA = "postgres.db";
DB_LOG = "postgres.log";
DB_NAME = "vegan-barcode";
shellHook = ''
echo "Using ${pkgs.postgresql.name}."
create_db=0
# initialize postgres if we have not done so already
if [ ! -d $PGDATA ]; then
echo "Postgres not found. Initializing postgres at $PGDATA"
${pkgs.postgresql}/bin/initdb > /dev/null
echo "Setting postgres unix socket directories to $PWD/$SOCKET_DIRECTORIES..."
echo "unix_socket_directories = '$PWD'" >> $PGDATA/postgresql.conf
echo "unix socket directories set."
echo "Creating database $DB_NAME..."
echo "CREATE DATABASE \"$DB_NAME\";" | postgres --single -E postgres
echo "Database created."
fi
export PGHOST="$PWD"
if [ ! -f "$PWD/.s.PGSQL.5432.lock" ]; then
${startPostgres}/bin/postgres_start
fi
'';
};
});
# The default package for 'nix build'. This makes sense if the
# flake provides only one package or there is a clear "main"
# package.
defaultPackage = forAllSystems (system: self.packages.${system}.go-hello);
};
}