feat(systemModules:host): add initial host configuration module

used to define general host configuration, including:

- hardware
- name
- admin
- type
- role
This commit is contained in:
ooks-io 2024-04-29 18:48:18 +12:00
parent 6db577fbf5
commit 7a74befde3
19 changed files with 538 additions and 0 deletions

View file

@ -0,0 +1,49 @@
{ lib, config, pkgs, ... }:
let
cfg = config.systemModules.host.admin;
ifTheyExist = groups: builtins.filter (group: builtins.hasAttr group config.users.groups) groups;
inherit (lib) types mkOption;
in
{
options.systemModules.host.admin = {
name = mkOption {
type = types.str;
default = "ooks";
description = "Name of the primary user";
};
shell = mkOption {
type = types.enum ["fish" "bash" "zsh"];
default = "zsh";
description = "The login shell of the primary user";
};
sshKey = mkOption {
type = types.str;
default = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBn3ff3HaZHIyH4K13k8Mwqu/o7jIABJ8rANK+r2PfJk";
description = "The ssh key for the admin user";
};
};
config = {
users.users.${cfg.name} = {
isNormalUser = true;
shell = pkgs.${cfg.shell};
initialPassword = "password";
openssh.authorizedKeys = "${cfg.sshKey}";
extraGroups = [
"wheel"
"video"
"audio"
] ++ ifTheyExist [
"git"
"media"
"network"
"libvirtd"
"deluge"
"streamer"
"torrenter"
];
};
};
}

View file

@ -0,0 +1,8 @@
{
imports = [
./admin
./name
./type
./function
];
}

View file

@ -0,0 +1,19 @@
{ lib, ... }:
let
inherit (lib) types mkOption;
in
{
imports = [
./gaming
./workstation
./media-server
];
options.systemModules.host.function = mkOption {
type = with types; listOf (enum ["gaming" "workstation" "media-server"]);
default = [];
description = "Host's primary function/s";
};
}

View file

@ -0,0 +1,29 @@
{ lib, config, ... }:
let
inherit (lib) mkIf;
inherit (builtins) elem;
function = config.systemModules.host.function;
in
{
config = mkIf (elem "workstation" function) {
systemModules = {
audio.enable = true;
video.enable = true;
programs = {
dconf.enable = true;
wireshark.enable = true;
bandwhich.enable = true;
kdeconnect.enable = true;
};
services = {
}
}
}
}

View file

@ -0,0 +1,43 @@
{ lib, config, pkgs, ... }:
let
inherit (lib) mkMerge mkEnableOption mkIf versionAtLeast versionOlder;
hardware = config.systemModules.host.hardware.cpu;
cfg = hardware.amd;
kernelVersion = config.kernelPackages.kernel.version;
kernelVersionAtLeast = versionAtLeast kernelVersion;
kernelVersionOlder= versionOlder kernelVersion;
in
{
options.systemModules.host.hardware.cpu.amd = {
pstate.enable = mkEnableOption "Enable pstate amd module";
};
config = mkIf (builtins.elem hardware.type ["amd"]) {
environment.systemPackages = [pkgs.amdctl];
hardware.cpu.amd.updateMicrocode = true;
boot = mkMerge [
{
kernelModules = [
"amd-pstate"
"amd-kvm" # virtulization
"msr" # required for amdctl
];
}
(mkIf (cfg.pstate.enable && (kernelVersionAtLeast "5.27") && (kernelVersionOlder "6.1")) {
kernelParams = ["initcall_blacklist-acpi_cpufreq_init"];
kernelModules = ["amd-pstate"];
})
(mkIf (cfg.pstate.enable && (kernelVersionAtLeast "6.1") && (kernelVersionOlder "6.3")) {
kernelParams = ["amd_pstate=passive"];
})
(mkIf (cfg.pstate.enable && (kernelVersionAtLeast "6.3")) {
kernelParams = ["amd_pstate=active"];
})
];
};
}

View file

@ -0,0 +1,18 @@
{ lib, ... }:
let
inherit (lib) types mkOption;
in
{
imports = [
./amd
./intel
];
options.systemModules.host.hardware.cpu.type = mkOption {
type = with types; nullOr (enum ["intel" "amd"]);
default = null;
description = "Type of cpu system module to use";
};
}

View file

@ -0,0 +1,20 @@
{ config, lib, pkgs, ... }:
let
inherit (lib) mkIf;
inherit (builtins) elem;
hardware = config.systemModules.host.hardware.cpu;
in
{
# TODO: put kvm/gvt behind virtualization module flag
config = mkIf (elem hardware.type ["intel"]) {
boot = {
kernelModules = ["kvm-intel"];
kernelParams = ["i915.fastboot=1" "enable_gvt=1"];
};
hardware.cpu.intel.updateMicrocode = true;
environment.systemPackages = [pkgs.intel-gpu-tools];
};
}

View file

@ -0,0 +1,8 @@
{
imports = [
./cpu
./gpu
./features
./ssd
];
}

View file

@ -0,0 +1,13 @@
{ lib, config, ... }:
let
features = config.systemModules.host.hardware.features;
inherit (lib) mkIf;
inherit (builtins) elem;
in
{
config = mkIf (elem "backlight" features) {
hardware.brillo.enable = true;
};
}

View file

@ -0,0 +1,94 @@
{ lib, config, pkgs, ... }:
let
features = config.systemModules.host.hardware.features;
cfg = config.systemModules.host.hardware.battery;
inherit (lib) mkIf mkDefault mkOption types;
inherit (builtins) elem;
MHz = x: x * 1000;
in
{
options.systemModules.host.hardware.battery = {
powersave = {
minFreq = mkOption {
type = types.int;
default = 800;
description = "Minimum frequency for powersave mode in MHz";
};
maxFreq = mkOption {
type = types.int;
default = 1100;
description = "Maximum frequency for powersave mode in MHz";
};
};
performance = {
minFreq = mkOption {
type = types.int;
default = 1500;
description = "Minimum frequency for performance mode in MHz";
};
maxFreq = mkOption {
type = types.int;
default = 2600;
description = "Maximum frequency for performance mode in MHz";
};
};
};
config = mkIf (elem "battery" features) {
boot = {
kernelModules = ["acpi_call"];
extraModulePackages = with config.boot.kernelPackages; [
acpi_call
cpupower
];
};
services = {
auto-cpufreq = {
enable = true;
settings = {
battery = {
governor = "powersave";
scaling_min_freq = mkDefault (MHz cfg.powersave.minFreq);
scaling_max_freq = mkDefault (MHz cfg.powersave.maxFreq);
turbo = "never";
};
charger = {
governor = "performance";
scaling_min_freq = mkDefault (MHz cfg.performance.minFreq);
scaling_max_freq = mkDefault (MHz cfg.performance.maxFreq);
turbo = "auto";
};
};
};
upower = {
enable = true;
percentageLow = 25;
percentageCritical = 5;
percentageAction = 3;
criticalPowerAction = "Hibernate";
};
undervolt = {
enable = true;
tempBat = 65;
};
thermald.enable = true;
power-profiles-daemon.enable = true;
logind = {
lidSwitch = "suspend";
};
};
environment.systemPackages = with pkgs; [
acpi
powertop
];
};
}

View file

@ -0,0 +1,24 @@
{ config, lib, pkgs, ... }:
let
features = config.systemModules.host.hardware.features;
inherit (lib) mkIf;
inherit (builtins) elem;
in
{
config = mkIf (elem "bluetooth" features) {
hardware.bluetooth = {
enable = true;
package = pkgs.bluez5-experimental;
};
environment.systemPackages = with pkgs; [
live-buds-cli
bluetuith
];
# https://github.com/NixOS/nixpkgs/issues/114222
systemd.user.services.telephony_client.enable = false;
};
}

View file

@ -0,0 +1,20 @@
{ lib, config, ... }:
let
inherit (lib) types mkOption;
in
{
imports = [
./bluetooth
./backlight
./battery
./ssd
];
options.systemModules.host.hardware.features = mkOption {
type = with types; listOf (enum ["bluetooth" "backlight" "battery" "ssd"]);
default = [];
description = "What extra hardware feature system modules to use";
};
}

View file

@ -0,0 +1,23 @@
{ lib, config, ... }:
let
features = config.systemModules.host.hardware.ssd;
inherit (lib) mkIf;
inherit (builtins) elem;
in
{
config = mkIf (elem "ssd" features) {
services.fstrim = {
enable = true;
};
# only run fstrim while connected on AC
systemd.services.fstrim = {
unitConfig.ConditionACPower = true;
serviceConfig = {
Nice = 19;
IOSchedulingClass = "idle";
};
};
};
}

View file

@ -0,0 +1,29 @@
{ config, lib, pkgs, ... }:
let
gpu = config.systemModules.host.hardware.gpu;
inherit (lib) mkIf mkDefault;
inherit (builtins) elem;
in
{
config = mkIf (elem gpu.type ["amd"]) {
hardware.opengl = {
extraPackages = with pkgs; [
vulkan-tools
vulkan-loader
vulkan-extension-layer
vulkan-validation-layers
# amdvlk
mesa
];
extraPackages32 = [ pkgs.driversi686Linux.amdvlk ];
};
boot = {
initrd.kernelModules = ["amdgpu"];
kernelModules = ["amdgpu"];
};
environment.systemPackages = [ pkgs.nvtopPackages.amd ];
services.xserver.videoDrivers = mkDefault ["modesetting" "amdgpu"];
};
}

View file

@ -0,0 +1,19 @@
{ lib, ... }:
let
inherit (lib) types mkOption;
in
{
imports = [
./amd
./intel
./nvidia
];
options.systemModules.host.hardware.gpu.type = mkOption {
type = with types; nullOr (enum ["intel" "amd" "nvidia"]);
default = null;
description = "Type of gpu system module to use";
};
}

View file

@ -0,0 +1,37 @@
{ config, lib, pkgs, ... }:
let
gpu = config.systemModules.host.hardware.gpu;
inherit (lib) mkIf;
inherit (builtins) elem;
# vaapiIntel = pkgs.vaapiIntel.override {enableHybridCodec = true;};
in
{
config = mkIf (elem gpu.type ["intel"]) {
services.xserver.videoDrivers = ["modesetting"];
hardware.opengl = {
extraPackages = with pkgs; [
vaapiIntel
vaapiVdpau
libvdpau-va-gl
intel-compute-runtime
intel-media-driver
];
extraPackages32 = with pkgs.pkgsi686Linux; [
vaapiIntel
vaapiVdpau
libvdpau-va-gl
intel-media-driver
];
};
boot.initrd.kernelModules = ["i915"];
environment.variables = mkIf config.hardware.opengl.enable {
VDPAU_DRIVER = "va_gl";
};
};
}

View file

@ -0,0 +1,51 @@
{ config, lib, pkgs, ... }:
let
gpu = config.systemModules.host.hardware.gpu;
inherit (lib) mkIf mkDefault;
inherit (builtins) elem;
production = config.boot.kernelPackages.nvidiaPackages.production;
# beta = config.boot.kernelPackages.nvidiaPackages.beta;
in
{
# TODO: make option to choose nvidia package
config = mkIf (elem gpu.type ["nvidia"]) {
hardware = {
nvidia = {
open = mkDefault true;
package = production;
forceFullCompositionPipeline = true;
nvidiaSettings = false;
nvidiaPersistenced = true;
modesetting.enable = true;
powerManagement = {
enable = mkDefault true;
finegrained = mkDefault false;
};
};
opengl = {
extraPackages = with pkgs; [ nvidia-vaapi-driver ];
extraPackages32 = with pkgs.pkgsi686Linux; [ nvidia-vaapi-driver ];
};
};
environment.systemPackages = with pkgs; [
libva
libva-utils
vulkan-loader
vulkan-validation-layers
vulkan-tools
vulkan-extension-layer
mesa
nvtopPackages.nvidia
];
environment.sessionVariables = {
LIBVA_DRIVER_NAME = "nvidia";
NVD_BACKEND = "direct";
};
};
}

View file

@ -0,0 +1,21 @@
{ lib, config, ... }:
let
inherit (lib) types mkOption;
cfg = config.systemModules.host;
in
{
options.systemModules.host = {
name = mkOption {
type = types.str;
default = "ooksgeneric";
description = "Name of host machine";
};
};
config = {
networking.hostname = cfg.name;
environment.sessionVariables.HN = cfg.name;
};
}

View file

@ -0,0 +1,13 @@
{ lib, ... }:
let
inherit (lib) mkOption types;
in
{
options.systemModules.host.type = mkOption {
type = types.enum ["desktop" "laptop" "mixed" "server" "phone" "laptop" "micro" "vm"];
default = "";
description = "Declare what type of device the host is";
};
}