nixfiles/extras/mk-enc-usb.nix

52 lines
1.2 KiB
Nix
Raw Normal View History

{
parted,
cryptsetup,
e2fsprogs,
2023-09-18 03:56:58 +01:00
writeShellApplication,
}: let
2023-09-20 15:46:20 +01:00
encryptedUSBData = import ../data/drives/encryptedUSB.nix;
2024-03-10 17:26:18 +00:00
in writeShellApplication {
2023-09-18 03:56:58 +01:00
name = "mk-enc-usb";
runtimeInputs = [
parted
cryptsetup
e2fsprogs
];
text = ''
if [ -z "''${1-}" ]; then
echo "Please specify a path to device as first argument"
exit 1
fi
# e.g /dev/sdb
USB_DEVICE=$1
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
echo "Creating Encrypted USB."
echo "Creating Partitions..."
parted "$USB_DEVICE" -- mklabel gpt
parted "$USB_DEVICE" -- mkpart primary 0% 100%
echo "Creating Encrypted Partition"
cryptsetup luksFormat "''${USB_DEVICE}1"
2023-09-18 03:56:58 +01:00
echo "Opening Encrypted Partition"
cryptsetup open "''${USB_DEVICE}1" "mk_enc_usb"
2023-09-18 03:56:58 +01:00
echo "Making Encrypted Filesystem"
mkfs.ext4 -L "${encryptedUSBData.unencryptedLabel}" /dev/mapper/mk_enc_usb
echo "Closing Encrypted Partition"
cryptsetup close "mk_enc_usb"
# Do this now so that i can run the damn script with usb-automount and stop it trying to mount
echo "Naming Partitions"
parted "$USB_DEVICE" -- name 1 ${encryptedUSBData.encryptedPartLabel}
'';
2024-03-10 17:26:18 +00:00
}