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

# nixos-rebuild also requires a "system" profile
/nix/store/qj39l35igj1mp503wzb7yl4lx919474w-nix-2.31.2/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/6gfsc1r9l64g90gg3hz573a76gm445in-configuration.nix /etc/nixos/configuration.nix
  chmod +w /etc/nixos/configuration.nix
fi

if ! [ -e /etc/nixos/vpsadminos.nix ]; then
  cp /nix/store/jvy7gl31rn95s3aq04lvm77brw80shz9-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/qj39l35igj1mp503wzb7yl4lx919474w-nix-2.31.2/bin/nix-env -p /nix/var/nix/profiles/per-user/root/channels \
    -i /nix/store/cili23fj2zxdvclirkvdmfph1qkh3mzz-nixos-25.11pre882227.01f116e4df6a --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


