# github.com/brianmcgee has lots of great ideas for going above and beyond for this
{
	description = "Greg's homepage";
	inputs = {
		nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
		devshell.url = "github:numtide/devshell";
		flake-parts.url = "github:hercules-ci/flake-parts";
		flake-root.url = "github:srid/flake-root";
		process-compose.url = "github:Platonic-Systems/process-compose-flake";
		treefmt-nix.url = "github:numtide/treefmt-nix";
	};

	outputs = { devshell, flake-parts, flake-root, process-compose, treefmt-nix, ... }@inputs:
	flake-parts.lib.mkFlake { inherit inputs; } {
		imports = [
			inputs.devshell.flakeModule
			inputs.flake-root.flakeModule
			inputs.process-compose.flakeModule
			inputs.treefmt-nix.flakeModule
		];
		flake = {
			# Universal things
		};
		systems = [
			"x86_64-linux"
			"aarch64-linux"
		];
		perSystem = { config, pkgs, self', ... }: let
			lib = pkgs.lib;
		in {
			devshells.default = {
				commands = [
					{
						package = pkgs.hugo;
						category = "server";
					}
					{
						package = self'.packages.dev-server;
						category = "server";
						help = "Start the development server";
					}
					{
						package = self'.packages.prod-server;
						category = "server";
						help = "Start the prod server";
					}
				];
			};

			packages = {
				default = pkgs.dockerTools.buildLayeredImage {
					name = "gregs-homepage";
					tag = "latest";
					contents = [
						self'.packages.site
						(pkgs.buildEnv {
							name = "image";
							paths = with pkgs; [
								dockerTools.fakeNss
								nginx
							];
							pathsToLink = [ "/bin" "/etc" "/var" ];
						})
					];
					fakeRootCommands = ''
						mkdir tmp
						chmod 0777 tmp
					'';
					config = {
						Cmd = [ (lib.getExe pkgs.nginx) "-c" self'.packages.nginx-config "-e" "stderr" ];
						Port = [ 80 ];
					};
				};
				site = pkgs.stdenv.mkDerivation {
					name = "Greg's homepage";
					nativeBuildInputs = [ pkgs.hugo ];
					buildPhase = ''
						hugo -v
					'';
					installPhase = ''
						mkdir -p $out
						cp -R public/* public/.* $out
					'';
					src = ./.;
				};
				nginx-config = pkgs.writeText "nginx.conf" ''
					user root root;
					worker_processes 10;
					pid /tmp/nginx.pid;
					daemon off;

					error_log stderr;

					events {
					}
					
					http {
						access_log stdout;
						include ${pkgs.nginx}/conf/mime.types;
						server {
							listen 80;
							root ${self'.packages.site};
							autoindex off;
						}
					}
				'';
			};

			process-compose = {
				dev-server.settings.processes = {
					hugo.command = "${lib.getExe pkgs.hugo} serve";
				};
				prod-server.settings.processes = {
					import.command = "${lib.getExe pkgs.podman} load -i ${self'.packages.default}";
					run = {
						command = "${lib.getExe pkgs.podman} run --rm -p 8000:80 gregs-homepage:latest";
						depends_on.import.condition = "process_completed_successfully";
						readiness_probe.http_get = {
							host = "localhost";
							port = 8000;
						};
					};
				};
			};

			treefmt.config = {
				inherit (config.flake-root) projectRootFile;
				package = pkgs.treefmt;

				programs = {
					deadnix.enable = true;
					#mdformat.enable = false;
				};

				settings.formatter = {
					#mdformat = {
					#	includes = pkgs.lib.mkForce [
					#		"archtypes/*.md"
					#		"content/*.md"
					#		"public/*.md"
					#	];
					#};
				};
			};
		};
	};
}
