40 lines
1.3 KiB
Nix
40 lines
1.3 KiB
Nix
{pkgs, ...}: let
|
|
usb_data = import ../data/usb_data.nix {};
|
|
enc_usb_mount = pkgs.writeShellScriptBin "enc_usb_mount" ''
|
|
set -x
|
|
${enc_usb_unmount}/bin/enc_usb_unmount
|
|
cat /secrets/usb_encryption_passphrase | cryptsetup luksOpen ${usb_data.encrypted_path} ${usb_data.mapper_name}_afterboot -
|
|
mount ${usb_data.mapper_path}_afterboot -o rw ${usb_data.mountpoint}
|
|
'';
|
|
|
|
enc_usb_unmount = pkgs.writeShellScriptBin "enc_usb_unmount" ''
|
|
set -x
|
|
umount -flR ${usb_data.mountpoint} || true
|
|
cryptsetup close ${usb_data.mapper_name}_afterboot || true
|
|
'';
|
|
in {
|
|
environment.systemPackages = [enc_usb_mount enc_usb_unmount];
|
|
|
|
systemd.tmpfiles.rules = ["d ${usb_data.mountpoint} - chaos root"];
|
|
|
|
systemd.services.enc-usb-mount = {
|
|
path = [pkgs.util-linux pkgs.cryptsetup];
|
|
wantedBy = ["multi-user.target"];
|
|
script = ''
|
|
${enc_usb_mount}/bin/enc_usb_mount
|
|
'';
|
|
};
|
|
|
|
systemd.services.enc-usb-unmount = {
|
|
path = [pkgs.util-linux pkgs.cryptsetup];
|
|
script = ''
|
|
${enc_usb_unmount}/bin/enc_usb_unmount
|
|
'';
|
|
};
|
|
|
|
services.udev.extraRules = ''
|
|
ACTION=="add", ENV{PARTNAME}=="${usb_data.encrypted_partlabel}", ENV{SYSTEMD_WANTS}="enc-usb-mount.service", ENV{UDISKS_PRESENTATION_HIDE}="1"
|
|
ACTION=="remove", ENV{PARTNAME}=="${usb_data.encrypted_partlabel}", ENV{SYSTEMD_WANTS}="enc-usb-unmount.service"
|
|
'';
|
|
}
|