Skip to content

Commit df4f172

Browse files
committed
replace clover with opencore
1 parent e5f9f47 commit df4f172

1 file changed

Lines changed: 139 additions & 138 deletions

File tree

nix/os/boot.nix

Lines changed: 139 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -47,145 +47,146 @@ in
4747
pkgs.systemdUkify
4848
pkgs.binutils
4949
]
50-
++ lib.optionals (boot == "BIOS") [ pkgs.gptfdisk ];
51-
text = lib.concatStrings [
52-
# Set environment
53-
''
54-
boot_json=/nix/var/nix/profiles/system/boot.json
55-
kernel=$(jq -r '."org.nixos.bootspec.v1".kernel' "$boot_json")
56-
initrd=$(jq -r '."org.nixos.bootspec.v1".initrd' "$boot_json")
57-
init=$(jq -r '."org.nixos.bootspec.v1".init' "$boot_json")
58-
kernelParams=$(jq -r '."org.nixos.bootspec.v1".kernelParams | join(" ")' "$boot_json")
59-
60-
esp=/boot
61-
tmp=$(mktemp -d)
62-
''
63-
64-
# Build UKI
65-
''
66-
ukify build \
67-
--linux="$kernel" \
68-
--initrd="$initrd" \
69-
--cmdline="init=$init $kernelParams" \
70-
--uname "${config.boot.kernelPackages.kernel.modDirVersion}" \
71-
--os-release "@${config.system.build.etc}/etc/os-release" \
72-
--output="$tmp/uki.efi"
73-
''
74-
75-
# Sign UKI
76-
''
77-
sbctl sign "$tmp/uki.efi"
78-
''
79-
80-
# Clean up ESP
81-
''
82-
rm -rf "''${esp:?}/*"
83-
''
84-
85-
# Move UKI to ESP
86-
''
87-
mkdir -p "$esp/EFI/BOOT"
88-
mv "$tmp/uki.efi" "$esp/EFI/BOOT/BOOT${lib.toUpper config.nixpkgs.hostPlatform.efiArch}.EFI"
89-
''
90-
91-
# Emulate UEFI on BIOS to allow UKI booting
92-
# https://github.com/NixOS/nixpkgs/issues/124132
93-
# https://wiki.archlinux.org/title/Clover#chainload_systemd-boot
94-
(lib.optionalString (boot == "BIOS") ''
95-
clover=${
96-
let
97-
version = "5165";
98-
in
99-
pkgs.fetchzip {
100-
name = "clover-${version}";
101-
url = "https://github.com/CloverHackyColor/CloverBootloader/releases/download/${version}/CloverV2-${version}.zip";
102-
sha256 = "sha256-KbaSQMJWNkBwdFKbYALCTfw0XcL5Cnfb2uIDzLdiLI0=";
103-
}
104-
}
105-
boot0=$clover/BootSectors/boot0af
106-
boot1=$clover/BootSectors/boot1f32
107-
boot2=$clover/Bootloaders/x64/boot7
108-
disks=(${
109-
lib.concatStringsSep " " (map (disk: disk.device) (lib.attrValues config.disko.devices.disk))
110-
})
111-
112-
for disk in "''${disks[@]}"; do
113-
partition="1"
114-
case "$disk" in
115-
*nvme*|*mmcblk*)
116-
part="''${disk}p''${partition}"
117-
;;
118-
*)
119-
part="''${disk}''${partition}"
120-
;;
121-
esac
122-
123-
sgdisk --attributes="''${partition}:set:2" "$disk"
124-
125-
dd if="$boot0" of="$disk" bs=1 count=440 conv=notrunc
126-
127-
cp "$boot1" "$tmp/new_PBR"
128-
dd if="$part" of="$tmp/original_PBR" bs=512 count=1 conv=notrunc
129-
dd if="$tmp/original_PBR" of="$tmp/new_PBR" skip=3 seek=3 bs=1 count=87 conv=notrunc
130-
dd if="$tmp/new_PBR" of="$part" bs=512 count=1 conv=notrunc
131-
rm "$tmp/new_PBR" "$tmp/original_PBR"
132-
done
133-
134-
cp $boot2 "$esp/boot"
135-
mkdir -p "$esp/EFI"
136-
cp -a "$clover/EFI/CLOVER" "$esp/EFI/CLOVER"
137-
cat << EOF > "$esp/EFI/CLOVER/config.plist"
138-
<?xml version="1.0" encoding="UTF-8"?>
139-
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "https://www.apple.com/DTDs/PropertyList-1.0.dtd">
140-
<plist version="1.0">
141-
<dict>
142-
<key>Boot</key>
143-
<dict>
144-
<key>DefaultVolume</key>
145-
<string>EFI</string>
146-
<key>DefaultLoader</key>
147-
<string>\EFI\BOOT\BOOT${lib.toUpper config.nixpkgs.hostPlatform.efiArch}.EFI</string>
148-
<key>Fast</key>
149-
<true/>
150-
</dict>
151-
<key>GUI</key>
152-
<dict>
153-
<key>Custom</key>
154-
<dict>
155-
<key>Entries</key>
156-
<array>
157-
<dict>
158-
<key>Hidden</key>
159-
<false/>
160-
<key>Disabled</key>
161-
<false/>
162-
<key>Volume</key>
163-
<string>EFI</string>
164-
<key>Path</key>
165-
<string>\EFI\BOOT\BOOT${lib.toUpper config.nixpkgs.hostPlatform.efiArch}.EFI</string>
166-
<key>Title</key>
167-
<string>XnodeOS</string>
168-
<key>Type</key>
169-
<string>Linux</string>
170-
</dict>
171-
</array>
172-
</dict>
173-
</dict>
174-
</dict>
175-
</plist>
176-
EOF
177-
'')
178-
179-
# Update unattended disk decryption lock
180-
(lib.optionalString (tpm == "2") ''
181-
SYSTEMD_ESP_PATH="$esp" ${config.systemd.package}/lib/systemd/systemd-pcrlock make-policy --pcr=7
182-
'')
183-
184-
# Remove temporary files
185-
''
186-
rm -rf "$tmp"
187-
''
50+
++ lib.optionals (boot == "BIOS") [
51+
pkgs.gptfdisk
52+
pkgs.gnused
18853
];
54+
text =
55+
let
56+
arch = lib.toUpper config.nixpkgs.hostPlatform.efiArch;
57+
in
58+
lib.concatStrings [
59+
# Set environment
60+
''
61+
boot_json=/nix/var/nix/profiles/system/boot.json
62+
kernel=$(jq -r '."org.nixos.bootspec.v1".kernel' "$boot_json")
63+
initrd=$(jq -r '."org.nixos.bootspec.v1".initrd' "$boot_json")
64+
init=$(jq -r '."org.nixos.bootspec.v1".init' "$boot_json")
65+
kernelParams=$(jq -r '."org.nixos.bootspec.v1".kernelParams | join(" ")' "$boot_json")
66+
67+
esp=/boot
68+
tmp=$(mktemp -d)
69+
''
70+
71+
# Build UKI
72+
''
73+
ukify build \
74+
--linux="$kernel" \
75+
--initrd="$initrd" \
76+
--cmdline="init=$init $kernelParams" \
77+
--uname "${config.boot.kernelPackages.kernel.modDirVersion}" \
78+
--os-release "@${config.system.build.etc}/etc/os-release" \
79+
--output="$tmp/uki.efi"
80+
''
81+
82+
# Sign UKI
83+
''
84+
sbctl sign "$tmp/uki.efi"
85+
''
86+
87+
# Clean up ESP
88+
''
89+
rm -rf "''${esp:?}/*"
90+
''
91+
92+
# Move UKI to ESP
93+
(lib.optionalString (boot == "UEFI") ''
94+
mkdir -p "$esp/EFI/BOOT"
95+
mv "$tmp/uki.efi" "$esp/EFI/BOOT/BOOT${arch}.EFI"
96+
'')
97+
98+
# Emulate UEFI on BIOS to allow UKI booting
99+
# https://github.com/NixOS/nixpkgs/issues/124132
100+
# https://wiki.archlinux.org/title/Clover#chainload_systemd-boot
101+
(lib.optionalString (boot == "BIOS") ''
102+
oc=${
103+
let
104+
version = "1.0.6";
105+
in
106+
pkgs.fetchzip {
107+
name = "open-core-${version}";
108+
url = "https://github.com/acidanthera/OpenCorePkg/releases/download/${version}/OpenCore-${version}-RELEASE.zip";
109+
sha256 = "sha256-+YcwRZ4mbbyh4Ivbk1bzLPFLlYtKUON0n+Co0+cp8c8=";
110+
stripRoot = false;
111+
}
112+
}
113+
boot0=$oc/Utilities/LegacyBoot/boot0
114+
boot1=$oc/Utilities/LegacyBoot/boot1f32
115+
boot2=$oc/Utilities/LegacyBoot/boot${arch}-blockio
116+
disks=(${
117+
lib.concatStringsSep " " (map (disk: disk.device) (lib.attrValues config.disko.devices.disk))
118+
})
119+
120+
for disk in "''${disks[@]}"; do
121+
partition="1"
122+
case "$disk" in
123+
*nvme*|*mmcblk*)
124+
part="''${disk}p''${partition}"
125+
;;
126+
*)
127+
part="''${disk}''${partition}"
128+
;;
129+
esac
130+
131+
sgdisk --attributes="''${partition}:set:2" "$disk"
132+
133+
dd if="$boot0" of="$disk" bs=1 count=446 conv=notrunc
134+
135+
cp "$boot1" "$tmp/new_PBR"
136+
dd if="$part" of="$tmp/original_PBR" count=1
137+
dd if="$tmp/original_PBR" of="$tmp/new_PBR" skip=3 seek=3 bs=1 count=87 conv=notrunc
138+
dd if=/dev/random of="$tmp/new_PBR" skip=496 seek=496 bs=1 count=14 conv=notrunc
139+
dd if="$tmp/new_PBR" of="$part"
140+
rm "$tmp/new_PBR" "$tmp/original_PBR"
141+
done
142+
143+
cp $boot2 "$esp/boot"
144+
mkdir -p "$esp/EFI"
145+
cp -a "$oc/${arch}/EFI/OC" "$esp/EFI/OC"
146+
cp -a "$oc/${arch}/EFI/BOOT" "$esp/EFI/BOOT"
147+
mv "$tmp/uki.efi" "$esp/EFI/xnodeos.efi"
148+
cp "$oc/Docs/Sample.plist" "$esp/EFI/OC/config.plist"
149+
150+
# Disable Vault
151+
sed -i '/<key>Vault<\/key>/,/string>/ s/<string>Secure<\/string>/<string>Optional<\/string>/' "$esp/EFI/OC/config.plist"
152+
153+
# Disable Secure Boot
154+
sed -i '/<key>SecureBootModel<\/key>/,/string>/ s/<string>Default<\/string>/<string>Disabled<\/string>/' "$esp/EFI/OC/config.plist"
155+
156+
# Relax Scan Policy
157+
sed -i '/<key>ScanPolicy<\/key>/,/string>/ s/<integer>17760515<\/integer>/<integer>0<\/integer>/' "$esp/EFI/OC/config.plist"
158+
159+
# Instantly Boot Into OS
160+
sed -i '/<key>ShowPicker<\/key>/,/string>/ s/<true\/>/<false\/>/' "$esp/EFI/OC/config.plist"
161+
162+
# Remove Default Drivers
163+
sed -i '/<key>Drivers<\/key>/,/<\/array>/c\
164+
<key>Drivers</key>\
165+
<array/>' "$esp/EFI/OC/config.plist"
166+
167+
# Remove Default Entries
168+
sed -i '/<key>Entries<\/key>/,/<\/array>/c\
169+
<key>Entries</key>\
170+
<array/>' "$esp/EFI/OC/config.plist"
171+
172+
# Add XnodeOS Entry
173+
sed -i '/<key>BlessOverride<\/key>/,/<array\/>/c\
174+
<key>BlessOverride</key>\
175+
<array>\
176+
<string>\\EFI\\xnodeos.efi</string>\
177+
</array>' "$esp/EFI/OC/config.plist"
178+
'')
179+
180+
# Update unattended disk decryption lock
181+
(lib.optionalString (tpm == "2") ''
182+
SYSTEMD_ESP_PATH="$esp" ${config.systemd.package}/lib/systemd/systemd-pcrlock make-policy --pcr=7
183+
'')
184+
185+
# Remove temporary files
186+
''
187+
rm -rf "$tmp"
188+
''
189+
];
189190
}
190191
)}";
191192
};

0 commit comments

Comments
 (0)