let pkg = import <nixpkgs> {};
in {stdenv          ? pkg.stdenv,
	fetchFromGitHub ? pkg.fetchFromGitHub,
	terra           ? pkg.terra,
	pkgconfig       ? pkg.pkgconfig,
	json_c          ? pkg.json_c,
	postgresql      ? pkg.postgresql.lib,
	mbedtls         ? pkg.mbedtls,
	glibc           ? pkg.glibc,
	cmark           ? pkg.cmark,
	inkscape        ? pkg.inkscape,
	mongoose        ? null,
	lib             ? pkg.lib
}:
let mgsrc = if mongoose != null then mongoose
			else fetchFromGitHub {
				owner = "cesanta";
				repo = "mongoose";
				rev = "369f44adfa38e0d8fa9667e9d6bafd7e0e3c6231";
				sha256 = "17rkd7ydic39cw9sfh11mcil02vmi6jjyj2ncbxan6psak09xbrc";
			};
	pkv = val: p: "parsav_pkg_${p}_${val}";
	pkp = pkv "prefix";
in stdenv.mkDerivation {
	name = "parsav";
	version = "dev";
	nativeBuildInputs = [terra json_c pkgconfig mbedtls];
	buildInputs = [cmark inkscape postgresql postgresql.lib];
	src = ./.;

	${pkp "json-c"} = "${json_c}";
	${pkp "mbedtls"} = "${mbedtls}";
	${pkp "libc"} = "${glibc.out}";
	${pkp "cmark"} = "${cmark}";
	${pkp "inkscape"} = "${inkscape}";
	${pkv "incdir" "libpq"} = "${postgresql.out}/include";
	${pkv "libdir" "libpq"} = "${postgresql.lib}/lib";

	parsav_build_branch = "trunk";
	parsav_build_checkout = "<latest>";
	parsav_builder = "nixpkgs";

	parsav_install_prefix_cfg="/etc/parsav";
	preBuild = ''
		export parsav_install_prefix="$out"
	'';

	parsav_target_os = "linux";
	parsav_dist = "nixos";
	configurePhase = ''
		cp -r ${mgsrc} lib/mongoose
	'';
	# postInstall = ''
	# 	make prefix=$out install
	# '';
	
	meta = {
		description = "lightweight fediverse server";
		homepage = https://c.hale.su/parsav;
		license = lib.licenses.agpl3;
		platforms = lib.platforms.unix;
	};
} 
