2022-12-04 13:45:43 +00:00
|
|
|
{
|
2023-09-18 03:56:58 +01:00
|
|
|
self,
|
2022-12-04 13:45:43 +00:00
|
|
|
config,
|
|
|
|
pkgs,
|
2023-09-18 03:56:58 +01:00
|
|
|
lib,
|
2022-12-04 13:45:43 +00:00
|
|
|
...
|
|
|
|
}: let
|
2023-09-18 03:56:58 +01:00
|
|
|
inherit (lib.modules) mkBefore;
|
|
|
|
|
2023-09-20 18:17:50 +01:00
|
|
|
encryptedUSB = import "${self}/data/drives/encryptedUSB.nix";
|
|
|
|
driveData = import "${self}/data/drives/encryptedDrive.nix";
|
2022-11-10 14:57:07 +00:00
|
|
|
in {
|
2023-09-18 03:56:58 +01:00
|
|
|
boot = {
|
2022-11-10 14:57:07 +00:00
|
|
|
initrd.availableKernelModules = [
|
|
|
|
# For USB w/ Encryption Key
|
|
|
|
"usb_storage"
|
|
|
|
"usbcore"
|
|
|
|
"uas"
|
|
|
|
"sd_mod"
|
|
|
|
# For USB Keyboards
|
|
|
|
"usbhid"
|
|
|
|
# For Cryptography
|
|
|
|
"aesni_intel"
|
|
|
|
"cryptd"
|
|
|
|
];
|
2023-09-18 03:56:58 +01:00
|
|
|
initrd.postDeviceCommands = mkBefore ''
|
2022-12-14 10:08:14 +00:00
|
|
|
mkdir -m 0755 -p /keys
|
2023-09-18 03:56:58 +01:00
|
|
|
mkdir -m 0755 -p ${encryptedUSB.mountpoint}
|
2022-11-10 14:57:07 +00:00
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
while !(test -b ${encryptedUSB.encryptedPath})
|
2022-11-10 14:57:07 +00:00
|
|
|
do
|
2023-09-01 01:46:14 +01:00
|
|
|
${
|
|
|
|
if config.boot.plymouth.enable
|
|
|
|
then ''
|
|
|
|
${pkgs.plymouth}/bin/plymouth display-message --text="Please Plug In USB"
|
|
|
|
''
|
|
|
|
else ''
|
|
|
|
echo "Please Plug In USB"
|
|
|
|
''
|
|
|
|
}
|
2022-11-10 14:57:07 +00:00
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
2023-09-01 01:46:14 +01:00
|
|
|
${
|
|
|
|
if config.boot.plymouth.enable
|
|
|
|
then ''
|
|
|
|
${pkgs.plymouth}/bin/plymouth hide-message --text="Please Plug In USB"
|
2022-11-10 14:57:07 +00:00
|
|
|
|
2023-09-01 01:46:14 +01:00
|
|
|
${pkgs.plymouth}/bin/plymouth ask-for-password \
|
|
|
|
--prompt="Please Decrypt USB" \
|
2023-09-18 14:55:36 +01:00
|
|
|
--command="cryptsetup -T1 open ${encryptedUSB.encryptedPath} ${encryptedUSB.preBootMapperName}" \
|
2023-09-01 01:46:14 +01:00
|
|
|
--number-of-tries=3
|
|
|
|
''
|
|
|
|
else ''
|
|
|
|
echo "Please Decrypt USB"
|
2023-09-18 03:56:58 +01:00
|
|
|
cryptsetup open ${encryptedUSB.encryptedPath} ${encryptedUSB.preBootMapperName}
|
2023-09-01 01:46:14 +01:00
|
|
|
''
|
|
|
|
}
|
2022-11-10 14:57:07 +00:00
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
mount -n -t ${encryptedUSB.unencryptedFSType} -o ro ${encryptedUSB.preBootMapperPath} ${encryptedUSB.mountpoint}
|
2022-12-14 10:08:14 +00:00
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
cp ${encryptedUSB.encryptionKeysPath}/${config.networking.hostName}.key /keys
|
2022-12-14 10:08:14 +00:00
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
umount -f ${encryptedUSB.mountpoint}
|
2022-12-14 10:08:14 +00:00
|
|
|
|
2023-09-18 03:56:58 +01:00
|
|
|
cryptsetup close ${encryptedUSB.preBootMapperName}
|
2022-11-10 14:57:07 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
initrd.luks.devices = {
|
2023-09-18 03:56:58 +01:00
|
|
|
"${driveData.mapperName}" = {
|
|
|
|
device = "${driveData.encryptedPath}";
|
2022-12-14 10:08:14 +00:00
|
|
|
keyFile = "/keys/${config.networking.hostName}.key";
|
2022-11-10 14:57:07 +00:00
|
|
|
preLVM = false;
|
|
|
|
allowDiscards = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
2023-09-18 03:56:58 +01:00
|
|
|
|
|
|
|
fileSystems = {
|
2022-11-10 14:57:07 +00:00
|
|
|
"/" = {
|
2023-09-18 03:56:58 +01:00
|
|
|
device = "${driveData.decryptedPath}";
|
|
|
|
fsType = "${driveData.unencryptedFSType}";
|
2022-11-10 14:57:07 +00:00
|
|
|
};
|
|
|
|
"/boot" = {
|
2023-09-18 03:56:58 +01:00
|
|
|
device = "${driveData.bootPath}";
|
|
|
|
fsType = "${driveData.bootFSType}";
|
2022-11-10 14:57:07 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|