From 95f337e5c5ca602a71931f78ceb540a6a3cc4345 Mon Sep 17 00:00:00 2001 From: blahai Date: Mon, 17 Feb 2025 20:46:20 +0200 Subject: [PATCH] boot config --- modules/nixos/boot/default.nix | 2 + modules/nixos/boot/generic.nix | 160 ++++++++++++++++++++++++++++++++ modules/nixos/boot/plymouth.nix | 31 +++++++ 3 files changed, 193 insertions(+) create mode 100644 modules/nixos/boot/generic.nix create mode 100644 modules/nixos/boot/plymouth.nix diff --git a/modules/nixos/boot/default.nix b/modules/nixos/boot/default.nix index 11bcd26..1294c50 100644 --- a/modules/nixos/boot/default.nix +++ b/modules/nixos/boot/default.nix @@ -1,5 +1,7 @@ { imports = [ ./loader + ./generic.nix + ./plymouth.nix ]; } diff --git a/modules/nixos/boot/generic.nix b/modules/nixos/boot/generic.nix new file mode 100644 index 0000000..f60445b --- /dev/null +++ b/modules/nixos/boot/generic.nix @@ -0,0 +1,160 @@ +{ + pkgs, + config, + lib, + ... +}: let + inherit (lib.modules) mkIf mkForce mkMerge mkDefault; + inherit (lib.lists) optionals; + inherit (lib.options) mkOption mkEnableOption; + inherit (lib.types) raw; + + cfg = config.olympus.system.boot; +in { + options.olympus.system.boot = { + enableKernelTweaks = mkEnableOption "security and performance related kernel parameters"; + recommendedLoaderConfig = mkEnableOption "tweaks for common bootloader configs per my liking"; + loadRecommendedModules = mkEnableOption "kernel modules that accommodate for most use cases"; + + kernel = mkOption { + type = raw; + default = pkgs.linuxPackages_cachyos; + description = "The kernel to use for the system"; + }; + + initrd = { + enableTweaks = mkEnableOption "quality of life tweaks for the initrd stage"; + optimizeCompressor = mkEnableOption '' + initrd compression algorithm optimizations for size. + Enabling this option will force initrd to use zstd (default) with + level 19 and -T0 (STDIN). This will reduce the initrd size greatly + at the cost of compression speed. + Not recommended for low-end hardware. + ''; + }; + + silentBoot = + mkEnableOption '' + almost entirely silent boot process through `quiet` kernel parameter + '' + // { + default = cfg.plymouth.enable; + }; + }; + + config = { + boot = { + consoleLogLevel = 3; + + kernelPackages = mkDefault cfg.kernel; + + loader = { + # if set to 0, space needs to be held to get the boot menu to appear + timeout = mkForce 2; + + # copy boot files to /boot so that /nix/store is not required to boot + # it takes up more space but it makes my messups a bit safer + generationsDir.copyKernels = true; + + # we need to allow installation to modify EFI variables + efi.canTouchEfiVariables = true; + }; + + # increase the map count, this is important for applications that require a lot of memory mappings + # such as games and emulators + kernel.sysctl."vm.max_map_count" = 2147483642; + + initrd = mkMerge [ + (mkIf cfg.initrd.enableTweaks { + # Verbosity of the initrd + # disabling verbosity removes only the mandatory messages generated by the NixOS + verbose = false; + + systemd = { + # enable systemd in initrd (experimental) + enable = true; + + # strip copied binaries and libraries from inframs + # saves some nice space + strip = true; + }; + + kernelModules = [ + "nvme" + "xhci_pci" + "ahci" + "btrfs" + "sd_mod" + "dm_mod" + ]; + + availableKernelModules = [ + "vmd" + "usbhid" + "sd_mod" + "sr_mod" + "dm_mod" + "uas" + "usb_storage" + "ata_piix" + "virtio_pci" + "virtio_scsi" + "ehci_pci" + ]; + }) + + (mkIf cfg.initrd.optimizeCompressor { + compressor = "zstd"; + compressorArgs = [ + "-19" + "-T0" + ]; + }) + ]; + + # https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html + kernelParams = + optionals cfg.enableKernelTweaks [ + # https://en.wikipedia.org/wiki/Kernel_page-table_isolation + # auto means kernel will automatically decide the pti state + "pti=auto" # on || off + + # enable IOMMU for devices used in passthrough and provide better host performance + "iommu=pt" + + # disable usb autosuspend + "usbcore.autosuspend=-1" + + # allow systemd to set and save the backlight state + "acpi_backlight=native" + + # prevent the kernel from blanking plymouth out of the fb + "fbcon=nodefer" + + # disable boot logo + "logo.nologo" + + # disable the cursor in vt to get a black screen during intermissions + "vt.global_cursor_default=0" + ] + ++ optionals cfg.silentBoot [ + # tell the kernel to not be verbose, the voices are too loud + "quiet" + + # kernel log message level + "loglevel=3" # 1: system is unusable | 3: error condition | 7: very verbose + + # udev log message level + "udev.log_level=3" + + # lower the udev log level to show only errors or worse + "rd.udev.log_level=3" + + # disable systemd status messages + # rd prefix means systemd-udev will be used instead of initrd + "systemd.show_status=auto" + "rd.systemd.show_status=auto" + ]; + }; + }; +} diff --git a/modules/nixos/boot/plymouth.nix b/modules/nixos/boot/plymouth.nix new file mode 100644 index 0000000..9b88ac4 --- /dev/null +++ b/modules/nixos/boot/plymouth.nix @@ -0,0 +1,31 @@ +{ + lib, + pkgs, + inputs', + config, + ... +}: let + inherit (lib.meta) getExe'; + inherit (lib.modules) mkIf; + inherit (lib.options) mkEnableOption; + + cfg = config.olympus.system.boot.plymouth; +in { + options.olympus.system.boot.plymouth.enable = mkEnableOption "plymouth boot splash"; + + config = mkIf cfg.enable { + boot.plymouth = { + enable = true; + theme = "mikuboot"; + themePackages = [ + inputs'.haipkgs.packages.mikuboot + ]; + }; + + # make plymouth work with sleep + powerManagement = { + powerDownCommands = "${getExe' pkgs.plymouth "plymouth"} --show-splash"; + resumeCommands = "${getExe' pkgs.plymouth "plymouth"} --quit"; + }; + }; +}