nixfiles/presets/nixos/encryptedDrive.nix

90 lines
2.2 KiB
Nix
Raw Normal View History

{
2023-09-18 03:56:58 +01:00
self,
config,
pkgs,
2023-09-18 03:56:58 +01:00
lib,
...
}: let
2023-09-18 03:56:58 +01:00
inherit (lib.modules) mkBefore;
encryptedUSB = import "${self}/data/encryptedUSB.nix";
2023-09-20 15:46:20 +01:00
driveData = import "${self}/data/drives/normalEncryptedDrive.nix";
in {
2023-09-18 03:56:58 +01:00
boot = {
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}
2023-09-18 03:56:58 +01:00
while !(test -b ${encryptedUSB.encryptedPath})
do
${
if config.boot.plymouth.enable
then ''
${pkgs.plymouth}/bin/plymouth display-message --text="Please Plug In USB"
''
else ''
echo "Please Plug In USB"
''
}
sleep 1
done
${
if config.boot.plymouth.enable
then ''
${pkgs.plymouth}/bin/plymouth hide-message --text="Please Plug In USB"
${pkgs.plymouth}/bin/plymouth ask-for-password \
--prompt="Please Decrypt USB" \
--command="cryptsetup -T1 open ${encryptedUSB.encryptedPath} ${encryptedUSB.preBootMapperName}" \
--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-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}
'';
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";
preLVM = false;
allowDiscards = true;
};
};
};
2023-09-18 03:56:58 +01:00
fileSystems = {
"/" = {
2023-09-18 03:56:58 +01:00
device = "${driveData.decryptedPath}";
fsType = "${driveData.unencryptedFSType}";
};
"/boot" = {
2023-09-18 03:56:58 +01:00
device = "${driveData.bootPath}";
fsType = "${driveData.bootFSType}";
};
};
}