let pkg = import <nixpkgs> {};
in {stdenv          ? pkg.stdenv,
	fetchFromGitHub ? pkg.fetchFromGitHub,
	terra           ? pkg.terra-modern,
	pkgconfig       ? pkg.pkgconfig,
	json_c          ? pkg.json_c,
	postgresql      ? pkg.postgresql.lib,
	mbedtls         ? pkg.mbedtls,
	mongoose        ? null
}:
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];
	src = ./.;

	${pkp "json-c"} = "${json_c}";
	${pkp "libpq"} = "${postgresql}";
	${pkp "mbedtls"} = "${mbedtls}";

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