2024-11-05 05:49:14 -05:00
|
|
|
{ config, pkgs, ... }:
|
2024-11-02 17:19:40 -04:00
|
|
|
let
|
|
|
|
commonKernelParams = [
|
|
|
|
# VM/GPU passthrough
|
|
|
|
"amd_iommu=on"
|
|
|
|
"iommu=pt"
|
|
|
|
"nested=1"
|
|
|
|
|
|
|
|
# Virtualization nonsense
|
|
|
|
"transparent_hugepage=never"
|
|
|
|
|
|
|
|
# Isolate devices into IOMMU groups
|
|
|
|
"pcie_acs_override=downstream,multifunction"
|
|
|
|
"pci=routeirq"
|
|
|
|
];
|
|
|
|
in {
|
|
|
|
boot = {
|
2024-11-19 13:34:29 -05:00
|
|
|
# Must be Zen for IOMMU isolation
|
2024-11-21 20:00:29 -05:00
|
|
|
kernelPackages = pkgs.unstable.linuxPackages_zen;
|
2024-11-02 17:19:40 -04:00
|
|
|
kernel.sysctl."vm.max_map_count" = 2147483642;
|
|
|
|
kernelParams = commonKernelParams ++ [ "vfio-pci.ids=10de:1f82,10de:10fa" ];
|
2024-11-06 11:49:40 -05:00
|
|
|
blacklistedKernelModules = [ "pcspkr" ];
|
2024-11-02 17:19:40 -04:00
|
|
|
|
2024-11-24 09:55:02 -05:00
|
|
|
# Needed for GPU passthrough
|
|
|
|
initrd.kernelModules = [
|
|
|
|
"vfio"
|
|
|
|
"vfio_pci"
|
|
|
|
"vfio_iommu_type1"
|
|
|
|
];
|
|
|
|
|
2024-11-06 11:49:40 -05:00
|
|
|
initrd.systemd = {
|
|
|
|
enable = true;
|
|
|
|
services.root-reset = {
|
|
|
|
description = "Reset root and snapshot last boot";
|
|
|
|
wantedBy = [ "initrd.target" ];
|
|
|
|
before = [ "sysroot.mount" ];
|
2024-11-12 04:25:51 -05:00
|
|
|
after = [ "dev-${config.networking.hostName}-root.device" ];
|
2024-11-06 11:49:40 -05:00
|
|
|
unitConfig.DefaultDependencies = "no";
|
|
|
|
serviceConfig.Type = "oneshot";
|
|
|
|
script = ''
|
|
|
|
mkdir -p /mnt
|
|
|
|
mount /dev/${config.networking.hostName}/root /mnt
|
2024-11-02 17:19:40 -04:00
|
|
|
|
2024-11-06 11:49:40 -05:00
|
|
|
if [[ -e /mnt/prev ]]; then
|
|
|
|
btrfs subvolume delete /mnt/prev
|
|
|
|
fi
|
2024-11-02 17:19:40 -04:00
|
|
|
|
2024-11-06 11:49:40 -05:00
|
|
|
btrfs subvolume snapshot /mnt/root /mnt/prev
|
2024-11-02 17:19:40 -04:00
|
|
|
|
2024-11-06 11:49:40 -05:00
|
|
|
btrfs subvolume list -o /mnt/root | cut -f9 -d' ' | while read subvolume; do
|
|
|
|
btrfs subvolume delete "/mnt/$subvolume"
|
|
|
|
done
|
2024-11-02 17:19:40 -04:00
|
|
|
|
2024-11-06 11:49:40 -05:00
|
|
|
btrfs subvolume delete /mnt/root
|
|
|
|
btrfs subvolume create /mnt/root
|
2024-11-02 17:19:40 -04:00
|
|
|
|
2024-11-06 11:49:40 -05:00
|
|
|
umount /mnt
|
|
|
|
'';
|
2024-11-02 17:19:40 -04:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-11-06 22:39:23 -05:00
|
|
|
# Use second GPU on boot
|
2024-11-06 11:49:40 -05:00
|
|
|
specialisation.gputwo.configuration = {
|
|
|
|
boot.kernelParams = commonKernelParams ++ [ "vfio-pci.ids=10de:2504,10de:228e" ];
|
2024-11-02 17:19:40 -04:00
|
|
|
};
|
|
|
|
}
|