#! /bin/sh
#
# build : hostname -> system-path
#
set -euf

host=$1

#target=root@$host

pubkeys=$config_root/pubkeys
nixpkgs=$nixpkgs_root/$host
nixos_config=$config_root/modules/$host
secrets_nix=$secrets_root/$host/nix
secrets_rsync=$secrets_root/$host/rsync

(
  # Notice NIX_PATH used from host
  # Notice secrets required to evaluate configuration
  NIX_PATH=$NIX_PATH:nixos-config=$nixos_config
  NIX_PATH=$NIX_PATH:secrets=$secrets_nix
  export NIX_PATH

  rev=$(nixos-query nixpkgs.rev)
  url=$(nixos-query nixpkgs.url)

  fetchgit "$rev" "$url" tmp/nixpkgs/$host
)

nix-build \
    -I "$nixpkgs" \
    -I pubkeys="$pubkeys" \
    -I nixos-config="$nixos_config" \
    -I retiolum-hosts="$retiolum_hosts" \
    -I secrets="$secrets_nix" \
    -A system \
    --no-out-link \
    '<nixos>'