From 045858b5361aa1ed7d85e7c720dbc3ebde1397dc Mon Sep 17 00:00:00 2001 From: Daniel Langbein Date: Mon, 9 Oct 2023 10:46:57 +0200 Subject: [PATCH] refactor: btrfs-mount-options module --- hosts/yodaNas/configuration.nix | 4 ++- hosts/yodaTab/configuration.nix | 4 ++- hosts/yodaTux/configuration.nix | 4 ++- hosts/yodaYoga/configuration.nix | 4 ++- modules/base.nix | 18 ------------- modules/btrfs-mount-options.nix | 43 ++++++++++++++++++++++++++++++++ modules/btrfs-scrub.nix | 4 +-- 7 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 modules/btrfs-mount-options.nix diff --git a/hosts/yodaNas/configuration.nix b/hosts/yodaNas/configuration.nix index 05fd98f..f22a254 100644 --- a/hosts/yodaNas/configuration.nix +++ b/hosts/yodaNas/configuration.nix @@ -61,11 +61,13 @@ in ../../modules/spin-down.nix ../../modules/btrfs-scrub.nix + ../../modules/btrfs-mount-options.nix ]; networking.hostName = "yodaNas"; boot.initrd.luks.devices."luks-3d974bd0-f373-469b-8e9c-2d5516e9f0f5".allowDiscards = true; - yoda.btrfs-scrub = ["/" "/mnt/data" "/mnt/backup"]; + yoda.btrfsFileSystems = ["/" "/mnt/data" "/mnt/backup"]; + #yoda.btrfsMounts = yoda.btrfsFileSystems; boot.kernelParams = []; diff --git a/hosts/yodaTab/configuration.nix b/hosts/yodaTab/configuration.nix index 289c2ff..2a8d4f8 100644 --- a/hosts/yodaTab/configuration.nix +++ b/hosts/yodaTab/configuration.nix @@ -60,12 +60,14 @@ in #../../modules/spin-down.nix ../../modules/btrfs-scrub.nix + ../../modules/btrfs-mount-options.nix ]; networking.hostName = "yodaTab"; boot.initrd.luks.devices."luks-ba8b94d0-7f70-496a-ad87-eadc5e852aad".allowDiscards = true; boot.initrd.luks.devices."512gb".allowDiscards = true; - yoda.btrfs-scrub = ["/"]; + yoda.btrfsFileSystems = ["/"]; + #yoda.btrfsMounts = yoda.btrfsFileSystems; boot.kernelParams = []; diff --git a/hosts/yodaTux/configuration.nix b/hosts/yodaTux/configuration.nix index c0aadf5..c5bcbd2 100644 --- a/hosts/yodaTux/configuration.nix +++ b/hosts/yodaTux/configuration.nix @@ -60,11 +60,13 @@ in #../../modules/spin-down.nix ../../modules/btrfs-scrub.nix + ../../modules/btrfs-mount-options.nix ]; networking.hostName = "yodaTux"; boot.initrd.luks.devices."luks-ea7099e3-320d-4eb3-a4c3-9910a9af817b".allowDiscards = true; - yoda.btrfs-scrub = ["/"]; + yoda.btrfsFileSystems = ["/"]; + #yoda.btrfsMounts = yoda.btrfsFileSystems; # Systemd Journal entry: # S Sat Sep 23 16:11:52 2023 p4 kernel: TSC found unstable after boot, most likely due to broken BIOS. Use 'tsc=unstable'. diff --git a/hosts/yodaYoga/configuration.nix b/hosts/yodaYoga/configuration.nix index 0854447..9f3477d 100644 --- a/hosts/yodaYoga/configuration.nix +++ b/hosts/yodaYoga/configuration.nix @@ -61,11 +61,13 @@ in #../../modules/spin-down.nix ../../modules/btrfs-scrub.nix + ../../modules/btrfs-mount-options.nix ]; networking.hostName = "yodaYoga"; boot.initrd.luks.devices."luks-a8521407-e25b-4f26-8e7a-a56fcbfd2e35".allowDiscards = true; - yoda.btrfs-scrub = ["/"]; + yoda.btrfsFileSystems = ["/"]; + #yoda.btrfsMounts = yoda.btrfsFileSystems; boot.kernelParams = []; diff --git a/modules/base.nix b/modules/base.nix index 3de06c6..dcb74cf 100644 --- a/modules/base.nix +++ b/modules/base.nix @@ -80,24 +80,6 @@ MaxFileSec=7day ''; - # BTRFS mount options. - # - # compress= - # `nixos-generate-config` does not detect mount options, so we add them here. - # https://nixos.wiki/wiki/Btrfs#Compression - # - # noatime - # Under read intensive work-loads, specifying noatime significantly improves performance because no new access time information needs to be written. - # https://man.archlinux.org/man/btrfs.5#NOTES_ON_GENERIC_MOUNT_OPTIONS - # - # commit= - # The number of seconds between periodic commits to the filesystem. This is 30 seconds by default. Increasing this value reduces the frequency of periodic writes which can reduce wear on the disk. However, this also increases the risk of data loss during the event of an untimely crash. - # https://www.jwillikers.com/btrfs-mount-options - # - fileSystems = { - "/".options = [ "compress=zstd" "noatime" "commit=120" ]; - }; - nix.settings.auto-optimise-store = true; # https://nixos.wiki/wiki/Storage_optimization#Automation diff --git a/modules/btrfs-mount-options.nix b/modules/btrfs-mount-options.nix new file mode 100644 index 0000000..f360f12 --- /dev/null +++ b/modules/btrfs-mount-options.nix @@ -0,0 +1,43 @@ +# BTRFS mount options. +# +# compress= +# `nixos-generate-config` does not detect mount options, so we add them here. +# https://nixos.wiki/wiki/Btrfs#Compression +# +# noatime +# Under read intensive work-loads, specifying noatime significantly improves performance because no new access time information needs to be written. +# https://man.archlinux.org/man/btrfs.5#NOTES_ON_GENERIC_MOUNT_OPTIONS +# +# commit= +# The number of seconds between periodic commits to the filesystem. This is 30 seconds by default. Increasing this value reduces the frequency of periodic writes which can reduce wear on the disk. However, this also increases the risk of data loss during the event of an untimely crash. +# https://www.jwillikers.com/btrfs-mount-options +# + + +{ lib, config, options, pkgs, modulesPath, ... }: +with lib; +let + cfg = config.yoda.btrfsMounts; +in +{ + options = { + yoda.btrfsMounts = mkOption { + type = types.listOf types.path; + default = config.yoda.btrfsFileSystems; + example = ["/" "/mnt/data"]; + description = '' + List containing each BTRFS mountpoint. + If each BTRFS filesystem is only mounted once, this can be left at its default value. + ''; + }; + }; + + config = mkIf (length cfg > 0) { + # For each element (e.g. "/") in `cfg` create this config: + # fileSystems."/".options = [ "compress=zstd" "noatime" "commit=120" ]; + fileSystems = builtins.listToAttrs (builtins.map (fs: { + name = fs; + value = { options = [ "compress=zstd" "noatime" "commit=120" ]; }; + }) cfg); + }; +} diff --git a/modules/btrfs-scrub.nix b/modules/btrfs-scrub.nix index f8313e4..7b5f124 100644 --- a/modules/btrfs-scrub.nix +++ b/modules/btrfs-scrub.nix @@ -17,11 +17,11 @@ { lib, config, options, pkgs, modulesPath, ... }: with lib; let - cfg = config.yoda.btrfs-scrub; + cfg = config.yoda.btrfsFileSystems; in { options = { - yoda.btrfs-scrub = mkOption { + yoda.btrfsFileSystems = mkOption { type = types.listOf types.path; default = ["/"]; example = ["/" "/mnt/data"];