#!/bin/sh
set -eu

if type podman >/dev/null 2>&1; then
    RUNC=podman
else
    RUNC="sudo docker"
fi

# sandboxing requires privileged container; https://github.com/NixOS/docker#limitations
$RUNC run --interactive ${DEBUG:+--tty} --privileged --volume `pwd`:/source:ro ${1:-docker.io/nixos/nix} /bin/sh <<EOF
sed -i '/sandbox/ s/false/true/' /etc/nix/nix.conf

cat <<EOG > /tmp/default.nix
let pkgs = (import (builtins.fetchTarball { url = "https://github.com/NixOS/nixpkgs/archive/master.tar.gz"; }) {});

in pkgs.umockdev.overrideAttrs (attrs: {
  src = /source;
  patches = [];
  preCheck = "";
  doCheck = true;
  nativeBuildInputs = attrs.nativeBuildInputs ++ [ ${DEBUG:+pkgs.breakpointHook} ];
})
EOG

[ -z "${DEBUG:-}" ] || nix-env -i cntr

nix-build --keep-failed /tmp/default.nix
EOF
