Add a separate option for mounts

This commit is contained in:
Manuel Thalmann 2024-12-07 19:11:41 +01:00
parent 3790fd267e
commit f1ed876cc1
2 changed files with 122 additions and 61 deletions

View file

@ -2,14 +2,53 @@
let let
inherit (lib) types mkOption; inherit (lib) types mkOption;
cfg = config.valhalla.fileSystems; 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 = [ imports = [
./fileSystems/btrfs.nix
./fileSystems/disks.nix ./fileSystems/disks.nix
]; ];
options = { options = {
valhalla = { valhalla = {
fileSystems = { fileSystems = {
mounts = mkOption {
type = types.attrsOf mountType;
description = "The devices to mount.";
default = { };
};
script = mkOption { script = mkOption {
type = types.str; type = types.str;
description = "The script for preparing the system's mounts."; description = "The script for preparing the system's mounts.";
@ -21,26 +60,28 @@ in {
config = { config = {
valhalla = { valhalla = {
fileSystems = { fileSystems = {
script = let script =
devices = (builtins.attrValues cfg.diskSetup.devices); let
in '' devices = (builtins.attrValues cfg.diskSetup.devices);
#!/bin/bash in
set -o errexit ''
${cfg.diskSetup.scripts.init} #!/bin/bash
${lib.strings.concatLines (lib.optionals ((builtins.length devices) > 0) [ set -o errexit
''echo "$(tput setaf 3)=== WARNING ====$(tput sgr0)"'' ${cfg.diskSetup.scripts.init}
(''echo "Continuing this script will alter the partitions of '' ${lib.strings.concatLines (lib.optionals ((builtins.length devices) > 0) [
+ (lib.strings.concatStringsSep ", " (builtins.map (_: "${_.deviceVariable}") (lib.lists.init devices))) ''echo "$(tput setaf 3)=== WARNING ====$(tput sgr0)"''
+ (if (builtins.length devices) > 1 then " and " else "") + (lib.lists.last devices).deviceVariable + ''"'') (''echo "Continuing this script will alter the partitions of ''
'' + (lib.strings.concatStringsSep ", " (builtins.map (_: "${_.deviceVariable}") (lib.lists.init devices)))
if ! fish ${./fileSystems/confirm.fish} "Are you sure you want to continue?" "n"; then + (if (builtins.length devices) > 1 then " and " else "") + (lib.lists.last devices).deviceVariable + ''"'')
exit 1 ''
fi 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} ])}
''; ${cfg.diskSetup.scripts.partition}
${cfg.diskSetup.scripts.mount}
'';
}; };
}; };
}; };

View file

@ -4,6 +4,7 @@ let
fs = import ./fs.nix; fs = import ./fs.nix;
cfg = config.valhalla.fileSystems;
deviceListVarName = "myDevices"; deviceListVarName = "myDevices";
isSwap = partition: builtins.elem partition.type [ fs.swap 19 ]; isSwap = partition: builtins.elem partition.type [ fs.swap 19 ];
@ -333,48 +334,67 @@ in
config = { config = {
valhalla = { valhalla = {
fileSystems.diskSetup = { fileSystems = {
scripts = mounts = (lib.attrsets.concatMapAttrs
let (
cfg = config.valhalla.fileSystems; name: device:
inherit (cfg) rootDir; lib.attrsets.concatMapAttrs
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
( (
_: [ name: partition:
probeScript if partition.mountPoint != null then {
(builtins.concatStringsSep " " ( ${partition.mountPoint} = {
[ "sudo" "mount" "--mkdir" ] ++ device = "/dev/disk/by-label/${partition.label}";
(lib.optionals (_.format == "ntfs") [ "-t" "ntfs3" ]) ++ fsType = partition.format;
[ options = partition.mountOptions;
(builtins.concatStringsSep " " (builtins.map (_: "-o ${_}") _.mountOptions)) };
(partPath _) } else { }
(normalizePath "/${rootDir}/${_.mountPoint}")
]
))
]
) )
(lib.lists.sortOn device.partitions
(_: normalizePath "/${_.mountPoint}") )
(builtins.filter (_: _.mountPoint != null) partitions))) ++ cfg.diskSetup.devices);
(builtins.map
( diskSetup = {
_: '' scripts =
${probeScript} let
sudo swapon ${partPath _} inherit (cfg) rootDir;
'' inherit (lib.strings) normalizePath;
) partPath = part: "/dev/disk/by-label/${part.label}";
(builtins.filter (_: _.useSwap) partitions)) 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))
);
};
};
}; };
}; };
}; };