···44 ./generic.nix # generic boot configuration
55 ./loader.nix # which system loader are we using
66 ./secure-boot.nix # pretty much what it looks like
77+ ./tmpfs.nix # configs to allow you to run on tmpfs
78 # keep-sorted end
89 ];
910}
+73-180
modules/nixos/boot/generic.nix
···11-{
22- lib,
33- pkgs,
44- config,
55- ...
66-}:
11+{ lib, config, ... }:
72let
83 inherit (lib.modules)
94 mkIf
105 mkForce
116 mkMerge
127 mkDefault
1313- mkOverride
148 ;
1515- inherit (lib.lists) optionals;
1616- inherit (lib.options) mkOption mkEnableOption;
1717- inherit (lib.types)
1818- str
1919- raw
2020- ;
99+ inherit (lib.options) mkEnableOption;
21102211 cfg = config.garden.system.boot;
2312in
2413{
2514 options.garden.system.boot = {
2626- enableKernelTweaks = mkEnableOption "security and performance related kernel parameters";
2727- recommendedLoaderConfig = mkEnableOption "tweaks for common bootloader configs per my liking";
2828- loadRecommendedModules = mkEnableOption "kernel modules that accommodate for most use cases";
2929- tmpOnTmpfs =
3030- mkEnableOption "`/tmp` living on tmpfs. false means it will be cleared manually on each reboot"
3131- // {
1515+ initrd = {
1616+ enableTweaks = mkEnableOption "quality of life tweaks for the initrd stage" // {
3217 default = true;
3318 };
34193535- kernel = mkOption {
3636- type = raw;
3737- default = pkgs.linuxPackages_latest;
3838- defaultText = "pkgs.linuxPackages_latest";
3939- description = "The kernel to use for the system.";
4040- };
4141-4242- initrd = {
4343- enableTweaks = mkEnableOption "quality of life tweaks for the initrd stage";
4444- optimizeCompressor = mkEnableOption ''
4545- initrd compression algorithm optimizations for size.
4646- Enabling this option will force initrd to use zstd (default) with
4747- level 19 and -T0 (STDIN). This will reduce thee initrd size greatly
4848- at the cost of compression speed.
4949- Not recommended for low-end hardware.
5050- '';
5151- };
5252-5353- extraModprobeConfig = mkOption {
5454- type = str;
5555- default = "options hid_apple fnmode=1";
5656- description = "Extra modprobe config that will be passed to system modprobe config.";
2020+ optimizeCompressor =
2121+ mkEnableOption ''
2222+ initrd compression algorithm optimizations for size.
2323+ Enabling this option will force initrd to use zstd (default) with
2424+ level 19 and -T0 (STDIN). This will reduce thee initrd size greatly
2525+ at the cost of compression speed.
2626+ Not recommended for low-end hardware.
2727+ ''
2828+ // {
2929+ default = config.garden.profiles.workstation.enable;
3030+ defaultText = "config.garden.profiles.workstation.enable";
3131+ };
5732 };
5858-5959- silentBoot = mkEnableOption ''
6060- almost entirely silent boot process through `quiet` kernel parameter
6161- '';
6233 };
63346464- config = {
6565- boot = {
6666- consoleLogLevel = 3;
3535+ config.boot = {
3636+ consoleLogLevel = 3;
67376868- # we set the kernel to be defaulted to the one set by our settings
6969- # we happen to default this to the latest kernel sooo:
7070- # always use the latest kernel, love the unstablity
7171- kernelPackages = mkOverride 500 cfg.kernel;
3838+ extraModprobeConfig = mkDefault "options hid_apple fnmode=1";
72397373- extraModprobeConfig = mkDefault cfg.extraModprobeConfig;
4040+ # whether to enable support for Linux MD RAID arrays
4141+ # as of 23.11>, this throws a warning if neither MAILADDR nor PROGRAM are set
4242+ swraid.enable = mkDefault false;
74437575- # whether to enable support for Linux MD RAID arrays
7676- # as of 23.11>, this throws a warning if neither MAILADDR nor PROGRAM are set
7777- swraid.enable = mkDefault false;
4444+ # shared config between bootloaders
4545+ # they are set unless system.boot.loader != none
4646+ loader = {
4747+ # if set to 0, space needs to be held to get the boot menu to appear
4848+ timeout = mkForce 2;
78497979- # shared config between bootloaders
8080- # they are set unless system.boot.loader != none
8181- loader = {
8282- # if set to 0, space needs to be held to get the boot menu to appear
8383- timeout = mkForce 2;
5050+ # copy boot files to /boot so that /nix/store is not required to boot
5151+ # it takes up more space but it makes my messups a bit safer
5252+ generationsDir.copyKernels = true;
84538585- # copy boot files to /boot so that /nix/store is not required to boot
8686- # it takes up more space but it makes my messups a bit safer
8787- generationsDir.copyKernels = true;
5454+ # we need to allow installation to modify EFI variables
5555+ efi.canTouchEfiVariables = true;
5656+ };
88578989- # we need to allow installation to modify EFI variables
9090- efi.canTouchEfiVariables = true;
9191- };
5858+ # initrd and kernel tweaks
5959+ # read what each parameter or module does before doing so, it will defo break something otherwise
6060+ initrd = mkMerge [
6161+ (mkIf cfg.initrd.enableTweaks {
6262+ # Verbosity of the initrd
6363+ # disabling verbosity removes only the mandatory messages generated by the NixOS
6464+ verbose = false;
92659393- # increase the map count, this is important for applications that require a lot of memory mappings
9494- # such as games and emulators
9595- kernel.sysctl."vm.max_map_count" = 2147483642;
6666+ # enable systemd in initrd (experimental)
6767+ systemd.enable = true;
96689797- # if you have a lack of ram, you should avoid tmpfs to prevent hangups while compiling
9898- tmp = {
9999- # /tmp on tmpfs, lets it live on your ram
100100- useTmpfs = cfg.tmpOnTmpfs;
6969+ kernelModules = [
7070+ "nvme"
7171+ "xhci_pci"
7272+ "ahci"
7373+ "btrfs"
7474+ "sd_mod"
7575+ "dm_mod"
7676+ ];
10177102102- # If not using tmpfs, which is naturally purged on reboot, we must clean
103103- # we have to clean /tmp
104104- cleanOnBoot = mkDefault (!config.boot.tmp.useTmpfs);
105105-106106- # this defaults to 50% of your ram
107107- # but i want to build code sooo
108108- # tmpfsSize = mkDefault "75%";
109109-110110- # enable huge pages on tmpfs for better performance
111111- tmpfsHugeMemoryPages = "within_size";
112112- };
113113-114114- # initrd and kernel tweaks
115115- # read what each parameter or module does before doing so, it will defo break something otherwise
116116- initrd = mkMerge [
117117- (mkIf cfg.initrd.enableTweaks {
118118- # Verbosity of the initrd
119119- # disabling verbosity removes only the mandatory messages generated by the NixOS
120120- verbose = false;
121121-122122- # enable systemd in initrd (experimental)
123123- systemd.enable = true;
124124-125125- kernelModules = [
126126- "nvme"
127127- "xhci_pci"
128128- "ahci"
129129- "btrfs"
130130- "sd_mod"
131131- "dm_mod"
132132- ];
133133-134134- availableKernelModules = [
135135- "vmd"
136136- "usbhid"
137137- "sd_mod"
138138- "sr_mod"
139139- "dm_mod"
140140- "uas"
141141- "usb_storage"
142142- "rtsx_usb_sdmmc"
143143- "rtsx_pci_sdmmc" # Realtek SD card interface (btw i hate realtek)
144144- "ata_piix"
145145- "virtio_pci"
146146- "virtio_scsi"
147147- "ehci_pci"
148148- ];
149149- })
150150-151151- (mkIf cfg.initrd.optimizeCompressor {
152152- compressor = "zstd";
153153- compressorArgs = [
154154- "-19"
155155- "-T0"
156156- ];
157157- })
158158- ];
159159-160160- # https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html
161161- kernelParams =
162162- optionals cfg.enableKernelTweaks [
163163- # https://en.wikipedia.org/wiki/Kernel_page-table_isolation
164164- # auto means kernel will automatically decide the pti state
165165- "pti=auto" # on || off
166166-167167- # disable the intel_idle (it stinks anyway) driver and use acpi_idle instead
168168- "idle=nomwait"
169169-170170- # enable IOMMU for devices used in passthrough and provide better host performance
171171- "iommu=pt"
172172-173173- # disable usb autosuspend
174174- "usbcore.autosuspend=-1"
175175-176176- # isables resume and restores original swap space
177177- "noresume"
7878+ availableKernelModules = [
7979+ "vmd"
8080+ "usbhid"
8181+ "sd_mod"
8282+ "sr_mod"
8383+ "dm_mod"
8484+ "uas"
8585+ "usb_storage"
8686+ "rtsx_usb_sdmmc"
8787+ "rtsx_pci_sdmmc" # Realtek SD card interface (btw i hate realtek)
8888+ "ata_piix"
8989+ "virtio_pci"
9090+ "virtio_scsi"
9191+ "ehci_pci"
9292+ ];
9393+ })
17894179179- # allow systemd to set and save the backlight state
180180- "acpi_backlight=native"
181181-182182- # prevent the kernel from blanking plymouth out of the fb
183183- "fbcon=nodefer"
184184-185185- # disable boot logo
186186- "logo.nologo"
187187-188188- # disable the cursor in vt to get a black screen during intermissions
189189- "vt.global_cursor_default=0"
190190- ]
191191- ++ optionals cfg.silentBoot [
192192- # tell the kernel to not be verbose, the voices are too loud
193193- "quiet"
194194-195195- # kernel log message level
196196- "loglevel=3" # 1: system is unusable | 3: error condition | 7: very verbose
197197-198198- # udev log message level
199199- "udev.log_level=3"
200200-201201- # lower the udev log level to show only errors or worse
202202- "rd.udev.log_level=3"
203203-204204- # disable systemd status messages
205205- # rd prefix means systemd-udev will be used instead of initrd
206206- "systemd.show_status=auto"
207207- "rd.systemd.show_status=auto"
9595+ (mkIf cfg.initrd.optimizeCompressor {
9696+ compressor = "zstd";
9797+ compressorArgs = [
9898+ "-19"
9999+ "-T0"
208100 ];
209209- };
101101+ })
102102+ ];
210103 };
211104}
+32
modules/nixos/boot/tmpfs.nix
···11+{ lib, config, ... }:
22+let
33+ inherit (lib) mkEnableOption mkDefault;
44+55+ cfg = config.garden.system.boot;
66+in
77+{
88+ options.garden.system.boot = {
99+ tmpOnTmpfs =
1010+ mkEnableOption "`/tmp` living on tmpfs. false means it will be cleared manually on each reboot"
1111+ // {
1212+ default = true;
1313+ };
1414+ };
1515+1616+ # if you have a lack of ram, you should avoid tmpfs to prevent hangups while compiling
1717+ config.boot.tmp = {
1818+ # /tmp on tmpfs, lets it live on your ram
1919+ useTmpfs = cfg.tmpOnTmpfs;
2020+2121+ # If not using tmpfs, which is naturally purged on reboot, we must clean
2222+ # we have to clean /tmp
2323+ cleanOnBoot = mkDefault (!config.boot.tmp.useTmpfs);
2424+2525+ # this defaults to 50% of your ram
2626+ # but i want to build code sooo
2727+ # tmpfsSize = mkDefault "75%";
2828+2929+ # enable huge pages on tmpfs for better performance
3030+ tmpfsHugeMemoryPages = "within_size";
3131+ };
3232+}
···11+{
22+ lib,
33+ pkgs,
44+ config,
55+ ...
66+}:
77+let
88+ inherit (lib.types) raw;
99+ inherit (lib.options) mkOption;
1010+ inherit (lib.modules) mkOverride;
1111+1212+ cfg = config.garden.system.kernel;
1313+in
1414+{
1515+ options.garden.system.kernel = {
1616+ packages = mkOption {
1717+ type = raw;
1818+ default = pkgs.linuxPackages_latest;
1919+ defaultText = "pkgs.linuxPackages_latest";
2020+ description = "The kernel to use for the system.";
2121+ };
2222+ };
2323+2424+ config = {
2525+ # we set the kernel to be defaulted to the one set by our settings
2626+ # we happen to default this to the latest kernel sooo:
2727+ # always use the latest kernel, love the unstablity
2828+ boot.kernelPackages = mkOverride 500 cfg.packages;
2929+ };
3030+}
+66-2
modules/nixos/kernel/params.nix
···11+# the holy handbook to kernel parameters
22+# <https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html>
33+{ lib, config, ... }:
44+let
55+ inherit (lib) mkEnableOption optionals;
66+ cfg = config.garden.system;
77+in
18{
22- # https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html
33- boot.kernelParams = [
99+ options.garden.system = {
1010+ boot.silent = mkEnableOption ''
1111+ almost entirely silent boot process through `quiet` kernel parameter
1212+ '';
1313+1414+ kernel.tweaks.enable = mkEnableOption "security and performance related kernel parameters" // {
1515+ default = config.garden.profiles.workstation.enable;
1616+ defaultText = "config.garden.profiles.workstation.enable";
1717+ };
1818+ };
1919+2020+ config.boot.kernelParams = [
421 # NixOS produces many wakeups per second, which is bad for battery life.
522 # This kernel parameter disables the timer tick on the last 4 cores
623 "nohz_full=4-7"
···47644865 # prevent the kernel from blanking plymouth out of the fb
4966 "fbcon=nodefer"
6767+ ]
6868+ ++ optionals cfg.kernel.tweaks.enable [
6969+ # https://en.wikipedia.org/wiki/Kernel_page-table_isolation
7070+ # auto means kernel will automatically decide the pti state
7171+ "pti=auto" # on || off
7272+7373+ # disable the intel_idle (it stinks anyway) driver and use acpi_idle instead
7474+ "idle=nomwait"
7575+7676+ # enable IOMMU for devices used in passthrough and provide better host performance
7777+ "iommu=pt"
7878+7979+ # disable usb autosuspend
8080+ "usbcore.autosuspend=-1"
8181+8282+ # isables resume and restores original swap space
8383+ "noresume"
8484+8585+ # allow systemd to set and save the backlight state
8686+ "acpi_backlight=native"
8787+8888+ # prevent the kernel from blanking plymouth out of the fb
8989+ "fbcon=nodefer"
9090+9191+ # disable boot logo
9292+ "logo.nologo"
9393+9494+ # disable the cursor in vt to get a black screen during intermissions
9595+ "vt.global_cursor_default=0"
9696+ ]
9797+ ++ optionals cfg.boot.silent [
9898+ # tell the kernel to not be verbose, the voices are too loud
9999+ "quiet"
100100+101101+ # kernel log message level
102102+ "loglevel=3" # 1: system is unusable | 3: error condition | 7: very verbose
103103+104104+ # udev log message level
105105+ "udev.log_level=3"
106106+107107+ # lower the udev log level to show only errors or worse
108108+ "rd.udev.log_level=3"
109109+110110+ # disable systemd status messages
111111+ # rd prefix means systemd-udev will be used instead of initrd
112112+ "systemd.show_status=auto"
113113+ "rd.systemd.show_status=auto"
50114 ];
51115}
+4
modules/nixos/kernel/sysctl.nix
···9999 # unless the user ID of the follower matches the symlink, or the
100100 # directory owner matches the symlink
101101 "fs.protected_symlinks" = 1;
102102+103103+ # increase the map count, this is important for applications that require a lot of memory mappings
104104+ # such as games and emulators
105105+ "vm.max_map_count" = 2147483642;
102106 };
103107}