2023-10-09 19:29:22 +00:00
|
|
|
{ config, options, lib, ... }:
|
2022-08-12 12:06:08 +00:00
|
|
|
let
|
2023-10-09 19:29:22 +00:00
|
|
|
cfgSev = config.hardware.cpu.amd.sev;
|
|
|
|
cfgSevGuest = config.hardware.cpu.amd.sevGuest;
|
|
|
|
|
|
|
|
optionsFor = device: group: {
|
2024-09-19 14:19:46 +00:00
|
|
|
enable = lib.mkEnableOption "access to the AMD ${device} device";
|
|
|
|
user = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Owner to assign to the ${device} device.";
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.str;
|
2023-10-09 19:29:22 +00:00
|
|
|
default = "root";
|
|
|
|
};
|
2024-09-19 14:19:46 +00:00
|
|
|
group = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Group to assign to the ${device} device.";
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.str;
|
2023-10-09 19:29:22 +00:00
|
|
|
default = group;
|
2022-08-12 12:06:08 +00:00
|
|
|
};
|
2024-09-19 14:19:46 +00:00
|
|
|
mode = lib.mkOption {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = "Mode to set for the ${device} device.";
|
2024-09-19 14:19:46 +00:00
|
|
|
type = lib.types.str;
|
2023-10-09 19:29:22 +00:00
|
|
|
default = "0660";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in
|
|
|
|
with lib; {
|
|
|
|
options.hardware.cpu.amd.sev = optionsFor "SEV" "sev";
|
|
|
|
|
|
|
|
options.hardware.cpu.amd.sevGuest = optionsFor "SEV guest" "sev-guest";
|
2022-08-12 12:06:08 +00:00
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
config = lib.mkMerge [
|
2023-10-09 19:29:22 +00:00
|
|
|
# /dev/sev
|
2024-09-19 14:19:46 +00:00
|
|
|
(lib.mkIf cfgSev.enable {
|
2022-08-12 12:06:08 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
2024-09-19 14:19:46 +00:00
|
|
|
assertion = lib.hasAttr cfgSev.user config.users.users;
|
2022-08-12 12:06:08 +00:00
|
|
|
message = "Given user does not exist";
|
|
|
|
}
|
|
|
|
{
|
2024-09-19 14:19:46 +00:00
|
|
|
assertion = (cfgSev.group == options.hardware.cpu.amd.sev.group.default) || (lib.hasAttr cfgSev.group config.users.groups);
|
2022-08-12 12:06:08 +00:00
|
|
|
message = "Given group does not exist";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
boot.extraModprobeConfig = ''
|
|
|
|
options kvm_amd sev=1
|
|
|
|
'';
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
users.groups = lib.optionalAttrs (cfgSev.group == options.hardware.cpu.amd.sev.group.default) {
|
2023-10-09 19:29:22 +00:00
|
|
|
"${cfgSev.group}" = { };
|
2022-08-12 12:06:08 +00:00
|
|
|
};
|
|
|
|
|
2023-10-09 19:29:22 +00:00
|
|
|
services.udev.extraRules = with cfgSev; ''
|
2022-08-12 12:06:08 +00:00
|
|
|
KERNEL=="sev", OWNER="${user}", GROUP="${group}", MODE="${mode}"
|
|
|
|
'';
|
2023-10-09 19:29:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
# /dev/sev-guest
|
2024-09-19 14:19:46 +00:00
|
|
|
(lib.mkIf cfgSevGuest.enable {
|
2023-10-09 19:29:22 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
2024-09-19 14:19:46 +00:00
|
|
|
assertion = lib.hasAttr cfgSevGuest.user config.users.users;
|
2023-10-09 19:29:22 +00:00
|
|
|
message = "Given user does not exist";
|
|
|
|
}
|
|
|
|
{
|
2024-09-19 14:19:46 +00:00
|
|
|
assertion = (cfgSevGuest.group == options.hardware.cpu.amd.sevGuest.group.default) || (lib.hasAttr cfgSevGuest.group config.users.groups);
|
2023-10-09 19:29:22 +00:00
|
|
|
message = "Given group does not exist";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2024-09-19 14:19:46 +00:00
|
|
|
users.groups = lib.optionalAttrs (cfgSevGuest.group == options.hardware.cpu.amd.sevGuest.group.default) {
|
2023-10-09 19:29:22 +00:00
|
|
|
"${cfgSevGuest.group}" = { };
|
|
|
|
};
|
|
|
|
|
|
|
|
services.udev.extraRules = with cfgSevGuest; ''
|
|
|
|
KERNEL=="sev-guest", OWNER="${user}", GROUP="${group}", MODE="${mode}"
|
|
|
|
'';
|
|
|
|
})
|
|
|
|
];
|
|
|
|
}
|