# Maintainer: Rocket Aaron <rocka@archlinuxcn.org>
# Contributor: Joan Bruguera Micó <joanbrugueram@gmail.com>

pkgname=nix-user-chroot
pkgver=2.1.1
pkgrel=1
pkgdesc="Run and install nix as user without root permissions."
url="https://github.com/nix-community/nix-user-chroot"
arch=(x86_64)
license=(MIT)
makedepends=(cargo)
checkdepends=(busybox)
source=("${url}/archive/refs/tags/${pkgver}.tar.gz")
b2sums=('a350fad114a5587a158163c933b7132ff8d09a57ea9b7a43714bd13507e0eaefa1035a0b0f5df92d2362cf918132f69e8d6075996ef18c6f0e4d09a56207a496')

build() {
  cd "${pkgname}-${pkgver}"
  cargo build --release --locked
}

check() {
  cd "${pkgname}-${pkgver}"
  # See https://github.com/nix-community/nix-user-chroot/tree/1.2.2#check-if-your-kernel-supports-user-namespaces-for-unprivileged-users
  # Plus, ensure basic mount+chroot works inside the user namespace (this fails in some locked down
  # environments, such as e.g. when running a regular Podman container with AppArmor enabled)
  if ! unshare --user --mount --map-root-user sh -c 'mount -t tmpfs none /tmp && chroot / true'; then
    echo "WARNING: Skipping tests because user namespaces are not supported or restricted" >&2
    return
  fi

  # The tests require NIX_USER_CHROOT_TEST_BUSYBOX to be set
  NIX_USER_CHROOT_TEST_BUSYBOX=$(which busybox) cargo test --release --locked
}

package() {
  cd "${pkgname}-${pkgver}"
  install -Dm755 target/release/nix-user-chroot -t "${pkgdir}/usr/bin/"
  install -Dm644 LICENSE -t "${pkgdir}/usr/share/licenses/${pkgname}/"
}
