# After booting, register the contents of the Nix store in the Nix database.
if [ -f /nix/nix-path-registration ]; then
  /nix/store/hcr0d9l6wwm8imljsxfr612zvyisjjag-nix-2.31.3/bin/nix-store --load-db < /nix/nix-path-registration &&
  rm /nix/nix-path-registration
fi

# nixos-rebuild also requires a "system" profile
/nix/store/hcr0d9l6wwm8imljsxfr612zvyisjjag-nix-2.31.3/bin/nix-env -p /nix/var/nix/profiles/system --set /run/current-system

# Add profiles to gcroots
ln -sf /nix/var/nix/profiles /nix/var/nix/gcroots/profiles

# Copy configuration required to reproduce this build
if ! [ -e /etc/nixos/configuration.nix ]; then
  cp /nix/store/a3r9zqg7yx8dyycd3vgiq90y5dk1h4yc-configuration.nix /etc/nixos/configuration.nix
  chmod +w /etc/nixos/configuration.nix
fi

if ! [ -e /etc/nixos/vpsadminos.nix ]; then
  cp /nix/store/1rn6wnjqy6dagpvhbimpr438sz83mff5-vpsadminos.nix /etc/nixos/vpsadminos.nix
  chmod +w /etc/nixos/vpsadminos.nix
fi

if ! [ -e /var/lib/nixos/did-channel-init ]; then
  echo "unpacking the NixOS/Nixpkgs sources..."
  mkdir -p /nix/var/nix/profiles/per-user/root
  /nix/store/hcr0d9l6wwm8imljsxfr612zvyisjjag-nix-2.31.3/bin/nix-env -p /nix/var/nix/profiles/per-user/root/channels \
    -i /nix/store/c1cchjvzj446kc6spcgphifhpa093dvq-nixos-26.05pre931542.88d3861acdd3 --quiet --option build-use-substitutes false \
     # There's an issue with pivot_root
  mkdir -m 0700 -p /root/.nix-defexpr
  ln -s /nix/var/nix/profiles/per-user/root/channels /root/.nix-defexpr/channels
  mkdir -m 0755 -p /var/lib/nixos
  touch /var/lib/nixos/did-channel-init
fi


