nixfiles/presets/nixos/encryptedDrive.nix

110 lines
3.1 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;
2023-09-20 18:17:50 +01:00
encryptedUSB = import "${self}/data/drives/encryptedUSB.nix";
driveData = import "${self}/data/drives/encryptedDrive.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 ''
2023-12-20 17:38:38 +00:00
mkdir -m 0755 -p /keys
mkdir -m 0755 -p ${encryptedUSB.mountpoint}
2023-12-20 17:38:38 +00:00
if grep --quiet "cryptsetup_password" /proc/cmdline; then
USE_PASSWORD=true
else
USE_PASSWORD=false
fi
2023-12-20 17:38:38 +00:00
while !(test -b ${encryptedUSB.encryptedPath}) && [ "$USE_PASSWORD" == "false" ]
do
${
if config.boot.plymouth.enable
then ''
${pkgs.plymouth}/bin/plymouth display-message --text="Please Plug In USB"
''
else ''
echo "Please Plug In USB"
''
}
2023-12-20 17:38:38 +00:00
sleep 1
done
2023-12-20 17:38:38 +00:00
${
if config.boot.plymouth.enable
then ''
${pkgs.plymouth}/bin/plymouth hide-message --text="Please Plug In USB"
2023-12-20 17:38:38 +00:00
if [ "$USE_PASSWORD" == "true" ]; then
${pkgs.plymouth}/bin/plymouth ask-for-password \
--prompt="Please Enter Password" \
--command="cryptsetup -T1 open ${driveData.encryptedPath} ${driveData.mapperName}" \
--number-of-tries=3
else
${pkgs.plymouth}/bin/plymouth ask-for-password \
--prompt="Please Decrypt USB" \
--command="cryptsetup -T1 open ${encryptedUSB.encryptedPath} ${encryptedUSB.preBootMapperName}" \
--number-of-tries=3
fi
''
else ''
2023-12-20 17:38:38 +00:00
if [ "$USE_PASSWORD" == "true" ]; then
echo "Please Decrypt Drive"
cryptsetup open ${driveData.encryptedPath} ${driveData.mapperName}
2023-12-20 17:38:38 +00:00
else
echo "Please Decrypt USB"
cryptsetup open ${encryptedUSB.encryptedPath} ${encryptedUSB.preBootMapperName}
fi
''
}
2023-12-20 17:38:38 +00:00
if [ "$USE_PASSWORD" == "false" ]; then
mount -n -t ${encryptedUSB.unencryptedFSType} -o ro ${encryptedUSB.preBootMapperPath} ${encryptedUSB.mountpoint}
2022-12-14 10:08:14 +00:00
2023-12-20 17:38:38 +00:00
cp ${encryptedUSB.encryptionKeysPath}/${config.networking.hostName}.key /keys
chmod 0755 /keys/${config.networking.hostName}.key
umount -f ${encryptedUSB.mountpoint}
2022-12-14 10:08:14 +00:00
2023-12-20 17:38:38 +00:00
cryptsetup close ${encryptedUSB.preBootMapperName}
fi
'';
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;
fallbackToPassword = 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}";
};
};
}