PortValhalla/lib/modules/fileSystems.nix

121 lines
3.6 KiB
Nix
Raw Permalink Normal View History

2024-12-07 11:24:10 +00:00
{ lib, config, ... }:
let
inherit (lib) types mkOption;
cfg = config.valhalla.fileSystems;
2024-12-07 18:11:41 +00:00
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
{
2024-12-07 11:24:10 +00:00
imports = [
2024-12-07 18:11:41 +00:00
./fileSystems/btrfs.nix
2024-12-07 17:36:19 +00:00
./fileSystems/disks.nix
2024-12-07 11:24:10 +00:00
];
options = {
valhalla = {
fileSystems = {
rootDir = mkOption {
type = types.str;
description = "The root of the installation directory to mount disks into.";
default = "/mnt";
};
2024-12-07 18:11:41 +00:00
mounts = mkOption {
type = types.attrsOf mountType;
description = "The devices to mount.";
default = { };
};
2024-12-07 11:24:10 +00:00
script = mkOption {
type = types.str;
description = "The script for preparing the system's mounts.";
};
};
};
};
config = {
valhalla = {
fileSystems = {
2024-12-07 18:11:41 +00:00
script =
let
2024-12-07 20:09:18 +00:00
inherit (lib.strings) normalizePath removeSuffix;
2024-12-07 18:11:41 +00:00
devices = (builtins.attrValues cfg.diskSetup.devices);
mountScript = lib.strings.concatLines (
(builtins.concatMap
(
_: [
"partprobe 2> /dev/null || true"
"udevadm trigger"
(builtins.concatStringsSep " " (
[ "sudo" "mount" "--mkdir" ] ++
(lib.optionals (_.fsType == "ntfs") [ "-t" "ntfs3" ]) ++
[
(builtins.concatStringsSep " " (builtins.map (_: "-o ${_}") _.options))
(_.device)
2024-12-07 20:09:18 +00:00
(removeSuffix "/" (normalizePath "/${cfg.rootDir}/${_.mountPoint}"))
]
))
]
)
(builtins.attrValues cfg.mounts))
);
2024-12-07 21:18:10 +00:00
affected = (builtins.map (_: _.deviceVariable) devices) ++
(builtins.concatMap
(_: builtins.map (_: lib.escapeShellArg _) _.devices)
(builtins.attrValues cfg.btrfs.volumes));
2024-12-07 18:11:41 +00:00
in
''
#!/bin/bash
set -o errexit
${cfg.diskSetup.scripts.init}
${lib.strings.concatLines (lib.optionals ((builtins.length devices) > 0) [
''echo "$(tput setaf 3)=== WARNING ====$(tput sgr0)"''
(''echo "Continuing this script will alter the partitions of ''
2024-12-07 21:18:10 +00:00
+ (lib.strings.concatStringsSep ", " (lib.lists.init affected))
+ (if (builtins.length affected) > 1 then " and " else "") + (lib.lists.last affected) + ''"'')
2024-12-07 18:11:41 +00:00
''
if ! fish ${./fileSystems/confirm.fish} "Are you sure you want to continue?" "n"; then
exit 1
fi
''
2024-12-07 21:18:10 +00:00
])}
2024-12-07 18:11:41 +00:00
${cfg.diskSetup.scripts.partition}
${cfg.btrfs.script}
${mountScript}
${cfg.diskSetup.scripts.swap}
2024-12-07 18:11:41 +00:00
'';
2024-12-07 11:24:10 +00:00
};
};
};
}