#!/usr/bin/env bash

# /**
#  * Builds a Nix file using `callPackage` semantics.
#  *
#  * Unlike standard `nix-build` which imports the file directly, this script
#  * wraps the import in `pkgs.callPackage`. This allows the Nix file to declare
#  * dependencies as arguments, which are then automatically injected from `nixpkgs`.
#  *
#  * @param filename The Nix file to build (defaults to `default.nix`).
#  * @param args Additional arguments passed to `nix-build`.
#  */

FILENAME="$1"
if [ -z "$FILENAME" ]; then
	FILENAME=default.nix
fi
FILENAME="$(realpath "$FILENAME")"

tempfile="$(mktemp).nix"

{
	echo '{ pkgs ? import <nixpkgs> {} }:'
	echo "pkgs.callPackage "$FILENAME" {}"
} >$tempfile

nix-build $tempfile "$@"

rm $tempfile
