From 8d56d55a05e72f8243b43ad7e633a3b050bc029c Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 7 Dec 2024 19:11:41 +0100 Subject: [PATCH] Add a separate option for mounts --- lib/modules/fileSystems.nix | 83 ++++++++++++++++++------- lib/modules/fileSystems/disks.nix | 100 ++++++++++++++++++------------ 2 files changed, 122 insertions(+), 61 deletions(-) diff --git a/lib/modules/fileSystems.nix b/lib/modules/fileSystems.nix index 657e8276..353fd658 100644 --- a/lib/modules/fileSystems.nix +++ b/lib/modules/fileSystems.nix @@ -2,14 +2,53 @@ let inherit (lib) types mkOption; cfg = config.valhalla.fileSystems; -in { + + mountType = types.submodule ( + { config, name, ... }: { + options = { + device = mkOption { + type = types.nullOr types.str; + description = "The device to mount."; + default = null; + }; + + mountPoint = mkOption { + type = types.str; + description = "The path to mount the device to."; + default = name; + }; + + fsType = mkOption { + type = types.nullOr types.str; + description = "The file system type of the mount."; + default = null; + }; + + options = mkOption { + type = types.listOf types.str; + description = "The options of the mount."; + default = [ ]; + }; + }; + } + ); +in +{ imports = [ + ./fileSystems/btrfs.nix ./fileSystems/disks.nix ]; options = { valhalla = { + fileSystems = { + mounts = mkOption { + type = types.attrsOf mountType; + description = "The devices to mount."; + default = { }; + }; + script = mkOption { type = types.str; description = "The script for preparing the system's mounts."; @@ -21,26 +60,28 @@ in { config = { valhalla = { fileSystems = { - script = let - devices = (builtins.attrValues cfg.diskSetup.devices); - in '' - #!/bin/bash - set -o errexit - ${cfg.diskSetup.scripts.init} - ${lib.strings.concatLines (lib.optionals ((builtins.length devices) > 0) [ - ''echo $(tput setaf 3)=== WARNING ====$(jtput sgr0)"'' - (''echo "Continuing this script will alter the partitions of '' - + (lib.strings.concatStringsSep ", " (builtins.map (_: "${_.deviceVariable}") (lib.lists.init devices))) - + (if (builtins.length devices) > 1 then " and " else "") + (lib.lists.last devices).deviceVariable + ''"'') - '' - if ! fish ${./fileSystems/confirm.fish} "Are you sure you want to continue?" "n"; then - exit 1 - fi - '' - ])} - ${cfg.diskSetup.scripts.partition} - ${cfg.diskSetup.scripts.mount} - ''; + script = + let + devices = (builtins.attrValues cfg.diskSetup.devices); + in + '' + #!/bin/bash + set -o errexit + ${cfg.diskSetup.scripts.init} + ${lib.strings.concatLines (lib.optionals ((builtins.length devices) > 0) [ + ''echo $(tput setaf 3)=== WARNING ====$(jtput sgr0)"'' + (''echo "Continuing this script will alter the partitions of '' + + (lib.strings.concatStringsSep ", " (builtins.map (_: "${_.deviceVariable}") (lib.lists.init devices))) + + (if (builtins.length devices) > 1 then " and " else "") + (lib.lists.last devices).deviceVariable + ''"'') + '' + if ! fish ${./fileSystems/confirm.fish} "Are you sure you want to continue?" "n"; then + exit 1 + fi + '' + ])} + ${cfg.diskSetup.scripts.partition} + ${cfg.diskSetup.scripts.mount} + ''; }; }; }; diff --git a/lib/modules/fileSystems/disks.nix b/lib/modules/fileSystems/disks.nix index c80bdfff..22b31a01 100644 --- a/lib/modules/fileSystems/disks.nix +++ b/lib/modules/fileSystems/disks.nix @@ -4,6 +4,7 @@ let fs = import ./fs.nix; + cfg = config.valhalla.fileSystems; deviceListVarName = "myDevices"; isSwap = partition: builtins.elem partition.type [ fs.swap 19 ]; @@ -333,48 +334,67 @@ in config = { valhalla = { - fileSystems.diskSetup = { - scripts = - let - cfg = config.valhalla.fileSystems; - inherit (cfg) rootDir; - inherit (lib.strings) normalizePath; - partPath = part: "/dev/disk/by-label/${part.label}"; - disks = ((builtins.attrValues cfg.diskSetup.devices)); - partitions = (builtins.concatMap (_: (builtins.attrValues _.partitions)) disks); - in - { - init = lib.strings.concatLines (builtins.map (_: _.scripts.init) disks); - partition = lib.strings.concatLines (builtins.map (_: _.scripts.partition) disks); - mount = lib.strings.concatLines ( - (builtins.concatMap + fileSystems = { + mounts = (lib.attrsets.concatMapAttrs + ( + name: device: + lib.attrsets.concatMapAttrs ( - _: [ - probeScript - (builtins.concatStringsSep " " ( - [ "sudo" "mount" "--mkdir" ] ++ - (lib.optionals (_.format == "ntfs") [ "-t" "ntfs3" ]) ++ - [ - (builtins.concatStringsSep " " (builtins.map (_: "-o ${_}") _.mountOptions)) - (partPath _) - (normalizePath "/${rootDir}/${_.mountPoint}") - ] - )) - ] + name: partition: + if partition.mountPoint != null then { + ${partition.mountPoint} = { + device = "/dev/disk/by-label/${partition.label}"; + fsType = partition.format; + options = partition.mountOptions; + }; + } else { } ) - (lib.lists.sortOn - (_: normalizePath "/${_.mountPoint}") - (builtins.filter (_: _.mountPoint != null) partitions))) ++ - (builtins.map - ( - _: '' - ${probeScript} - sudo swapon ${partPath _} - '' - ) - (builtins.filter (_: _.useSwap) partitions)) - ); - }; + device.partitions + ) + cfg.diskSetup.devices); + + diskSetup = { + scripts = + let + inherit (cfg) rootDir; + inherit (lib.strings) normalizePath; + partPath = part: "/dev/disk/by-label/${part.label}"; + disks = ((builtins.attrValues cfg.diskSetup.devices)); + partitions = (builtins.concatMap (_: (builtins.attrValues _.partitions)) disks); + in + { + init = lib.strings.concatLines (builtins.map (_: _.scripts.init) disks); + partition = lib.strings.concatLines (builtins.map (_: _.scripts.partition) disks); + mount = lib.strings.concatLines ( + (builtins.concatMap + ( + _: [ + probeScript + (builtins.concatStringsSep " " ( + [ "sudo" "mount" "--mkdir" ] ++ + (lib.optionals (_.format == "ntfs") [ "-t" "ntfs3" ]) ++ + [ + (builtins.concatStringsSep " " (builtins.map (_: "-o ${_}") _.mountOptions)) + (partPath _) + (normalizePath "/${rootDir}/${_.mountPoint}") + ] + )) + ] + ) + (lib.lists.sortOn + (_: normalizePath "/${_.mountPoint}") + (builtins.filter (_: _.mountPoint != null) partitions))) ++ + (builtins.map + ( + _: '' + ${probeScript} + sudo swapon ${partPath _} + '' + ) + (builtins.filter (_: _.useSwap) partitions)) + ); + }; + }; }; }; };