47 lines
1.3 KiB
PowerShell

# Requires running PowerShell as Administrator
# Define the GUID for the EFI global variable
$EFI_GLOBAL_GUID = "{8BE4DF61-93CA-11D2-AA0D-00E098032B8C}"
# NixOS boot entry ID
$NixOSBootEntryID = "0002"
# Convert the Boot Entry ID to a byte array
$BootNextValue = [UInt16]::Parse($NixOSBootEntryID, "AllowHexSpecifier")
$BootNextBytes = [BitConverter]::GetBytes($BootNextValue)
# Define the SetFirmwareEnvironmentVariable function
$SetFirmwareEnvironmentVariable = @"
using System;
using System.Runtime.InteropServices;
public class UEFI
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool SetFirmwareEnvironmentVariable(
string lpName,
string lpGuid,
byte[] pValue,
int nSize
);
}
"@
Add-Type -TypeDefinition $SetFirmwareEnvironmentVariable
# Set the BootNext variable
$success = [UEFI]::SetFirmwareEnvironmentVariable(
"BootNext",
$EFI_GLOBAL_GUID,
$BootNextBytes,
$BootNextBytes.Length
)
if ($success) {
Write-Host "BootNext variable set successfully to Boot$NixOSBootEntryID."
# reboot to nixos
Write-Host "Rebooting to NixOS..."
Restart-Computer -Force
} else {
$errorCode = [Runtime.InteropServices.Marshal]::GetLastWin32Error()
Write-Host "Failed to set BootNext variable. Error code: $errorCode"
}