matabas/flake.nix

66 lines
2.1 KiB
Nix
Raw Normal View History

{
description = "Matabas databas för mat";
inputs = {
nixpkgs.url = "github:NixOs/nixpkgs/nixos-23.05";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, flake-utils, nixpkgs }:
flake-utils.lib.eachDefaultSystem (system:
let pkgs = nixpkgs.legacyPackages.${system};
in rec {
packages = rec {
2023-08-08 17:24:46 +02:00
postgres = pkgs.postgresql_15_jit;
default = pkgs.hello;
};
devShells.default = pkgs.mkShell {
2023-08-08 17:24:46 +02:00
packages = [ packages.default packages.postgres ];
shellHook = ''
set -e
2023-08-08 17:42:57 +02:00
log() {
printf '\e[1m\e[38;5;87m==>\e[0m\e[1m %s.\e[0m\n' "$*"
}
log "Checking for existing postgres directory"
[ -d ./matabas-db ] || {
log "Non-existent, creating postgres directory (./matabas-db)"
pg_ctl initdb --pgdata=./matabas-db
}
[ -f matabas-db/matabas.log ] && {
log "Moving logs from last session into log archive (./matabas-db/matabas.log.old)"
cat matabas-db/matabas.log >> matabas-db/matabas.log.old
rm -f matabas-db/matabas.log
}
log "Starting postgres"
pg_ctl start --pgdata=./matabas-db --log=./matabas-db/matabas.log --options="-p 2137 -k /tmp"
2023-08-08 17:42:57 +02:00
trap 'log "Stopping postgres" && pg_ctl stop --pgdata=./matabas-db' EXIT
log "Creating database"
createdb -p 2137 matabas 2>>./matabas-db/matabas.log || log "Database already exists, not overwriting"
2023-08-08 17:24:46 +02:00
2023-08-08 17:42:57 +02:00
log "Environment ready!"
2023-08-08 17:24:46 +02:00
set +e
'';
};
}
) // {
nixosConfigurations.matabas-container =
let pkgs = nixpkgs.legacyPackages.x86_64-linux;
in nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [({ pkgs, ... }: {
boot.isContainer = true;
system.configurationRevision = nixpkgs.lib.mkIf (self ? rev) self.rev;
system.stateVersion = "23.05";
environment.systemPackages = [ self.packages.x86_64-linux.default ];
})];
};
};
}