# Template file for 'nix'
pkgname=nix
version=2.11.0
revision=10
build_style=gnu-configure
build_helper=qemu
# Use /nix/var as suggested by the official Manual.
configure_args="--localstatedir=/nix/var
 --with-sandbox-shell=/usr/bin/busybox.static"
make_build_args="V=1 SET_RPATH_TO_LIBS=0"
make_install_args="V=1 SET_RPATH_TO_LIBS=0"
hostmakedepends="curl pkg-config flex tar xz automake autoconf-archive mdBook jq
 busybox-static lowdown"
makedepends="boost-devel brotli-devel bzip2-devel gc-devel libcurl-devel
 liblzma-devel openssl-devel libsodium-devel sqlite-devel
 libseccomp-devel editline-devel jq-devel libarchive-devel
 gtest-devel lowdown-devel json-c++"
depends="curl tar xz busybox-static"
short_desc="Purely functional package manager"
maintainer="Orphaned <orphan@voidlinux.org>"
license="LGPL-2.1-or-later"
homepage="https://nixos.org/nix/"
changelog="https://nixos.org/releases/${pkgname}/${pkgname}-${version}/manual/#sec-relnotes"
distfiles="https://github.com/NixOS/nix/archive/refs/tags/${version}.tar.gz"
checksum=6a90e144352eaccfab7ef7ed470bad382c4f92d62336b327df99627adba67a7c
# disable_parallel_build="build is fine, only linking test"

if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
	makedepends+=" musl-legacy-compat"
fi

case "$XBPS_TARGET_MACHINE" in
	x86_64*|i686*) makedepends+=" libcpuid-devel";;
esac

CXXFLAGS="-Wno-deprecated-declarations"

if [ "$CROSS_BUILD" ]; then
	make_build_args+=" CROSS_EMULATOR=/usr/bin/qemu-${XBPS_TARGET_QEMU_MACHINE}-static"
	make_install_args+=" CROSS_EMULATOR=/usr/bin/qemu-${XBPS_TARGET_QEMU_MACHINE}-static"
	# XXX: documentation generation hangs in cross on arm32 musl
	# see https://gitlab.com/qemu-project/qemu/-/issues/1729
	case "$XBPS_TARGET_MACHINE" in
		arm*musl) configure_args+=" --disable-doc-gen" ;;
	esac
fi

# Default configuration file.
conf_files="/etc/nix/nix.conf"
# Create required build users/groups.
system_groups="nixbld"
for f in {1..9}; do
	system_accounts+=" nixbld${f}"
	eval export nixbld\${f}_pgroup="nixbld"
	eval export nixbld\${f}_groups="nixbld"
done
# Create required run-time directories.
make_dirs="
	/nix/var/log/nix/drvs 0755 root root
	/nix/var/nix/profiles 0755 root root
	/nix/var/nix/profiles/per-user 1777 root root
	/nix/var/nix/gcroots/per-user 1777 root root
	/nix/var/nix/temproots 0755 root root
	/nix/var/nix/gcroots 0755 root root
	/nix/var/nix/manifests 0755 root root
	/nix/var/nix/userpool 0755 root root
	/nix/var/nix/db 0755 root root
	/nix/store 1775 root nixbld"

pre_configure() {
	./bootstrap.sh
}

post_install() {
	vmkdir etc/nix
	# Remove unused stuff.
	rm -rf ${DESTDIR}/usr/include ${DESTDIR}/nix ${DESTDIR}/etc/init
	rm -rf ${DESTDIR}/usr/lib/systemd ${DESTDIR}/usr/lib/pkgconfig
	# Let users interact with the nix-daemon by default.
	vmkdir etc/profile.d
	echo 'export NIX_REMOTE=daemon' > ${DESTDIR}/etc/profile.d/nix-daemon.sh
	# Setup build users.
	echo 'build-users-group = nixbld' > ${DESTDIR}/etc/nix/nix.conf
	echo 'build-use-sandbox = false' >> ${DESTDIR}/etc/nix/nix.conf
	# Install our profile.d/nix for multi-user by default.
	vinstall ${FILESDIR}/nix.sh 644 etc/profile.d
	# runit service
	vsv nix-daemon
}
