From 7a74befde3945585ce3c27e0c5966dc2df28d83c Mon Sep 17 00:00:00 2001 From: ooks-io Date: Mon, 29 Apr 2024 18:48:18 +1200 Subject: [PATCH] feat(systemModules:host): add initial host configuration module used to define general host configuration, including: - hardware - name - admin - type - role --- system/modules/host/admin/default.nix | 49 ++++++++++ system/modules/host/default.nix | 8 ++ system/modules/host/function/default.nix | 19 ++++ .../host/function/workstation/default.nix | 29 ++++++ .../modules/host/hardware/cpu/amd/default.nix | 43 +++++++++ system/modules/host/hardware/cpu/default.nix | 18 ++++ .../host/hardware/cpu/intel/default.nix | 20 ++++ system/modules/host/hardware/default.nix | 8 ++ .../hardware/features/backlight/default.nix | 13 +++ .../hardware/features/battery/default.nix | 94 +++++++++++++++++++ .../hardware/features/bluetooth/default.nix | 24 +++++ .../host/hardware/features/default.nix | 20 ++++ .../host/hardware/features/ssd/default.nix | 23 +++++ .../modules/host/hardware/gpu/amd/default.nix | 29 ++++++ system/modules/host/hardware/gpu/default.nix | 19 ++++ .../host/hardware/gpu/intel/default.nix | 37 ++++++++ .../host/hardware/gpu/nvidia/default.nix | 51 ++++++++++ system/modules/host/name/default.nix | 21 +++++ system/modules/host/type/default.nix | 13 +++ 19 files changed, 538 insertions(+) create mode 100644 system/modules/host/admin/default.nix create mode 100644 system/modules/host/default.nix create mode 100644 system/modules/host/function/default.nix create mode 100644 system/modules/host/function/workstation/default.nix create mode 100644 system/modules/host/hardware/cpu/amd/default.nix create mode 100644 system/modules/host/hardware/cpu/default.nix create mode 100644 system/modules/host/hardware/cpu/intel/default.nix create mode 100644 system/modules/host/hardware/default.nix create mode 100644 system/modules/host/hardware/features/backlight/default.nix create mode 100644 system/modules/host/hardware/features/battery/default.nix create mode 100644 system/modules/host/hardware/features/bluetooth/default.nix create mode 100644 system/modules/host/hardware/features/default.nix create mode 100644 system/modules/host/hardware/features/ssd/default.nix create mode 100644 system/modules/host/hardware/gpu/amd/default.nix create mode 100644 system/modules/host/hardware/gpu/default.nix create mode 100644 system/modules/host/hardware/gpu/intel/default.nix create mode 100644 system/modules/host/hardware/gpu/nvidia/default.nix create mode 100644 system/modules/host/name/default.nix create mode 100644 system/modules/host/type/default.nix diff --git a/system/modules/host/admin/default.nix b/system/modules/host/admin/default.nix new file mode 100644 index 0000000..1d3a8a7 --- /dev/null +++ b/system/modules/host/admin/default.nix @@ -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" + ]; + }; + }; +} diff --git a/system/modules/host/default.nix b/system/modules/host/default.nix new file mode 100644 index 0000000..ed7c8c1 --- /dev/null +++ b/system/modules/host/default.nix @@ -0,0 +1,8 @@ +{ + imports = [ + ./admin + ./name + ./type + ./function + ]; +} diff --git a/system/modules/host/function/default.nix b/system/modules/host/function/default.nix new file mode 100644 index 0000000..8a06929 --- /dev/null +++ b/system/modules/host/function/default.nix @@ -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"; + }; +} diff --git a/system/modules/host/function/workstation/default.nix b/system/modules/host/function/workstation/default.nix new file mode 100644 index 0000000..cd4687b --- /dev/null +++ b/system/modules/host/function/workstation/default.nix @@ -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 = { + + } + } + } + +} diff --git a/system/modules/host/hardware/cpu/amd/default.nix b/system/modules/host/hardware/cpu/amd/default.nix new file mode 100644 index 0000000..2f860d1 --- /dev/null +++ b/system/modules/host/hardware/cpu/amd/default.nix @@ -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"]; + }) + ]; + }; +} diff --git a/system/modules/host/hardware/cpu/default.nix b/system/modules/host/hardware/cpu/default.nix new file mode 100644 index 0000000..58b75a3 --- /dev/null +++ b/system/modules/host/hardware/cpu/default.nix @@ -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"; + }; +} diff --git a/system/modules/host/hardware/cpu/intel/default.nix b/system/modules/host/hardware/cpu/intel/default.nix new file mode 100644 index 0000000..e6749f2 --- /dev/null +++ b/system/modules/host/hardware/cpu/intel/default.nix @@ -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]; + }; +} diff --git a/system/modules/host/hardware/default.nix b/system/modules/host/hardware/default.nix new file mode 100644 index 0000000..7b40e27 --- /dev/null +++ b/system/modules/host/hardware/default.nix @@ -0,0 +1,8 @@ +{ + imports = [ + ./cpu + ./gpu + ./features + ./ssd + ]; +} diff --git a/system/modules/host/hardware/features/backlight/default.nix b/system/modules/host/hardware/features/backlight/default.nix new file mode 100644 index 0000000..0a60789 --- /dev/null +++ b/system/modules/host/hardware/features/backlight/default.nix @@ -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; + }; +} diff --git a/system/modules/host/hardware/features/battery/default.nix b/system/modules/host/hardware/features/battery/default.nix new file mode 100644 index 0000000..f933c0a --- /dev/null +++ b/system/modules/host/hardware/features/battery/default.nix @@ -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 + ]; + }; +} diff --git a/system/modules/host/hardware/features/bluetooth/default.nix b/system/modules/host/hardware/features/bluetooth/default.nix new file mode 100644 index 0000000..c002138 --- /dev/null +++ b/system/modules/host/hardware/features/bluetooth/default.nix @@ -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; + }; +} diff --git a/system/modules/host/hardware/features/default.nix b/system/modules/host/hardware/features/default.nix new file mode 100644 index 0000000..1513b5b --- /dev/null +++ b/system/modules/host/hardware/features/default.nix @@ -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"; + }; +} diff --git a/system/modules/host/hardware/features/ssd/default.nix b/system/modules/host/hardware/features/ssd/default.nix new file mode 100644 index 0000000..f9cdaed --- /dev/null +++ b/system/modules/host/hardware/features/ssd/default.nix @@ -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"; + }; + }; + }; +} diff --git a/system/modules/host/hardware/gpu/amd/default.nix b/system/modules/host/hardware/gpu/amd/default.nix new file mode 100644 index 0000000..c1a4a35 --- /dev/null +++ b/system/modules/host/hardware/gpu/amd/default.nix @@ -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"]; + }; +} diff --git a/system/modules/host/hardware/gpu/default.nix b/system/modules/host/hardware/gpu/default.nix new file mode 100644 index 0000000..6417200 --- /dev/null +++ b/system/modules/host/hardware/gpu/default.nix @@ -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"; + }; +} diff --git a/system/modules/host/hardware/gpu/intel/default.nix b/system/modules/host/hardware/gpu/intel/default.nix new file mode 100644 index 0000000..6974a71 --- /dev/null +++ b/system/modules/host/hardware/gpu/intel/default.nix @@ -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"; + }; + }; +} diff --git a/system/modules/host/hardware/gpu/nvidia/default.nix b/system/modules/host/hardware/gpu/nvidia/default.nix new file mode 100644 index 0000000..c0ddec9 --- /dev/null +++ b/system/modules/host/hardware/gpu/nvidia/default.nix @@ -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"; + }; + }; +} diff --git a/system/modules/host/name/default.nix b/system/modules/host/name/default.nix new file mode 100644 index 0000000..a497a98 --- /dev/null +++ b/system/modules/host/name/default.nix @@ -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; + }; +} diff --git a/system/modules/host/type/default.nix b/system/modules/host/type/default.nix new file mode 100644 index 0000000..5226056 --- /dev/null +++ b/system/modules/host/type/default.nix @@ -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"; + }; +}