Compare commits

...

14 commits

21 changed files with 386 additions and 170 deletions

View file

@ -1,14 +1,59 @@
{ lib, config, ... }:
let
inherit (lib) types mkOption;
in {
cfg = config.valhalla.fileSystems;
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 = [
./partition/disks.nix
./fileSystems/btrfs.nix
./fileSystems/disks.nix
];
options = {
valhalla = {
fileSystems = {
rootDir = mkOption {
type = types.str;
description = "The root of the installation directory to mount disks into.";
default = "/mnt";
};
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.";
@ -20,7 +65,50 @@ in {
config = {
valhalla = {
fileSystems = {
script = config.valhalla.fileSystems.diskSetup.script;
script =
let
inherit (lib.strings) normalizePath removeSuffix;
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)
(removeSuffix "/" (normalizePath "/${cfg.rootDir}/${_.mountPoint}"))
]
))
]
)
(builtins.attrValues cfg.mounts))
);
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.btrfs.script}
${mountScript}
${cfg.diskSetup.scripts.swap}
'';
};
};
};

View file

@ -0,0 +1,108 @@
{ lib, config, ... }:
let
inherit (lib) types mkOption;
cfg = config.valhalla.fileSystems.btrfs;
profileType = types.enum [
"raid0"
"raid1"
"raid1c3"
"raid1c4"
"raid5"
"raid6"
"raid10"
"dup"
"single"
];
volumeType = types.submodule (
{ config, name, ... }: {
options = {
mountPoint = mkOption {
type = types.nullOr types.str;
description = "The path to mount the volume to.";
default = null;
};
devices = mkOption {
type = types.listOf types.str;
description = "The devices of the btrfs volume.";
};
label = mkOption {
type = types.nullOr types.str;
description = "The label of the volume.";
default = name;
};
dataProfile = mkOption {
type = types.nullOr profileType;
description = "The data profile.";
default = null;
};
metadataProfile = mkOption {
type = types.nullOr profileType;
description = "The metadata profile.";
default = null;
};
};
}
);
in
{
options = {
valhalla = {
fileSystems.btrfs = {
volumes = mkOption {
type = types.attrsOf volumeType;
description = "The btrfs volumes of the system.";
default = { };
};
script = mkOption {
type = types.str;
description = "The script for creating the btrfs volumes.";
default = lib.strings.concatLines (
builtins.map
(
_: builtins.concatStringsSep " " (
[ "mkfs.btrfs" ] ++
(lib.optionals (_.metadataProfile != null) [ "--metadata" "${_.metadataProfile}" ]) ++
(lib.optionals (_.dataProfile != null) [ "--data" "${_.dataProfile}" ]) ++
(lib.optionals (_.label != null) [ "--label" "${_.label}" ]) ++
_.devices
)
)
(builtins.attrValues cfg.volumes)
);
};
};
};
};
config = {
valhalla = {
linux.programs.btrfs = lib.optionalAttrs
(builtins.any
(_: (builtins.length _.devices) > 1)
(builtins.attrValues cfg.volumes))
{
enable = true;
pools = true;
};
fileSystems.mounts = lib.attrsets.concatMapAttrs
(
name: volume:
if (volume.mountPoint != null) then {
${volume.mountPoint} = {
device = builtins.elemAt volume.devices 0;
fsType = "btrfs";
};
} else { }
)
cfg.volumes;
};
};
}

View file

@ -22,7 +22,7 @@ function chooseDisk -a outFile message selectScript
end
end
select "$header" "$outFile" "$message" "No valid disk found!" "$(string collect $disks)" false
select "$header" "$outFile" "$message" "No valid device found!" "$(string collect $disks)" false
and begin
set -l disk (string split -n " " (cat "$outFile"))
echo "/dev/$disk[1]" >$outFile

View file

@ -4,7 +4,8 @@ let
fs = import ./fs.nix;
diskListVarName = "myDisks";
cfg = config.valhalla.fileSystems;
deviceListVarName = "myDevices";
isSwap = partition: builtins.elem partition.type [ fs.swap 19 ];
probeScript = builtins.concatStringsSep "\n" [
@ -12,35 +13,35 @@ let
"udevadm trigger"
];
mkDiskType = types.submodule (
mkDeviceType = types.submodule (
{ config, name, ... }: {
options = {
id = mkOption {
type = types.str;
description = "The internal identifier of the disk.";
description = "The internal identifier of the device.";
internal = true;
};
wipe = mkOption {
type = types.bool;
description = "A value indicating whether the disk should be wiped.";
description = "A value indicating whether the device should be wiped.";
default = !(lib.lists.any (_: _.keepExisting) (builtins.attrValues config.partitions));
};
deviceName = mkOption {
name = mkOption {
type = types.nullOr types.str;
description = "The name of the device.";
default = name;
};
devicePath = mkOption {
path = mkOption {
type = types.nullOr types.str;
description = "The path to the device.";
default =
if config.deviceName == null then
if config.name == null then
null
else
"/dev/${config.deviceName}";
"/dev/${config.name}";
};
deviceScript = mkOption {
@ -61,21 +62,28 @@ let
default = { };
};
script = mkOption {
scripts = {
init = mkOption {
type = types.str;
description = "The script for formatting the disk.";
description = "A script for loading the device path into the device variable";
};
partition = mkOption {
type = types.str;
description = "A script for partitioning and formatting the device.";
};
};
};
config =
let
diskVarName = "${diskListVarName}[${config.id}]";
diskVar = "\${${diskVarName}}";
deviceVarName = "${deviceListVarName}[${config.id}]";
deviceVar = "\${${deviceVarName}}";
diskSelector = ''
deviceSelector = ''
result="$(mktemp)"
fish ${./choose-disk.fish} "$result" "Please specify the \"${name}\" disk:" ${./select.fish}
${diskVarName}="$(cat "$result")"
fish ${./choose-device.fish} "$result" "Please select the \"${name}\" device:" ${./select.fish}
${deviceVarName}="$(cat "$result")"
'';
partitions = lib.lists.sortOn (_: _.index)
@ -95,13 +103,12 @@ let
"echo ${script} | ${
fdiskCommand "${builtins.concatStringsSep " " args} ${
if append then "--append" else ""
} ${diskVar}"
} ${deviceVar}"
}";
wipeScript = script: fdiskScript script [ ] false;
appendScript = index: script: fdiskScript script [ "-N" (builtins.toString index) ] true;
cleanup = lib.strings.concatLines (builtins.map
(partition: "${fdiskCommand "--delete ${diskVar} ${toString partition.index}"} || true")
(partition: "${fdiskCommand "--delete ${deviceVar} ${toString partition.index}"} || true")
(lib.lists.sortOn
(partition: partition.index * -1)
(builtins.filter (_: !_.keepExisting) partitions)));
@ -109,7 +116,7 @@ let
fdiskCommands = lib.strings.concatLines
(lib.optionals config.wipe [
cleanup
(wipeScript "label: gpt")
(fdiskScript "label: gpt" [ ] false)
] ++ (builtins.concatMap
(
partition:
@ -125,6 +132,7 @@ let
formatScripts = {
${fs.ext4} = "mkfs.ext4 -F ${partVar}";
${fs.btrfs} = "mkfs.btrfs ${partVar}";
${fs.swap} = "mkswap ${partVar}";
${fs.ntfs} = "mkfs.ntfs -F ${partVar}";
${fs.fat32} = "mkfs.fat -F 32 ${partVar}";
@ -132,16 +140,18 @@ let
labelScripts = {
${fs.ext4} = label: "e2label ${partVar} ${label}";
${fs.btrfs} = label: "btrfs filesystem label ${partVar} ${label}";
${fs.swap} = label: "swaplabel ${partVar} --label ${label}";
${fs.ntfs} = label: "ntfslabel ${partVar} ${label}";
${fs.fat32} = label: "fatlabel ${partVar} ${label}";
};
create = lib.strings.concatLines [
create = lib.strings.concatLines ([
(appendScript index ''${toString index}: "$(${sizeOption})" type=${mkType type}'')
probeScript
] ++ (lib.optionals (format != null) [
"sudo ${formatScripts.${format}}"
];
]));
fallback = ''
if ! { ls "${partVar}" 2>&1; } > /dev/null
@ -151,11 +161,12 @@ let
'';
in
[
''local diskPath="$(find -L /dev/disk/by-diskseq -samefile ${diskVar})"''
''local diskPath="$(find -L /dev/disk/by-diskseq -samefile ${deviceVar})"''
''local ${partVarName}="$diskPath-part${toString index}"''
(if keepExisting then fallback else create)
] ++ (lib.optionals (format != null) [
"sudo ${labelScripts.${format} label}"
]
])
)
partitions));
@ -164,24 +175,25 @@ let
partition:
lib.optional
(partition.keepExisting && !(builtins.isNull partition.type))
''sudo sfdisk --part-type ${diskVar} ${toString partition.index} ${mkType partition.type}''
''sudo sfdisk --part-type ${deviceVar} ${toString partition.index} ${mkType partition.type}''
)
partitions);
in
{
id = "disk-${name}";
deviceVariable = diskVar;
deviceVariable = deviceVar;
deviceScript =
if config.devicePath == null then ''
${diskSelector}
scripts = {
init =
if config.path == null then ''
${deviceSelector}
'' else ''
${diskVarName}=${config.devicePath}
if [ ! -b ${diskVar} ]; then
${deviceVarName}=${config.path}
if [ ! -b ${deviceVar} ]; then
function fallback() {
echo "Couldn't find the specified disk \"${diskVar}\"."
echo "Couldn't find the specified disk \"${deviceVar}\"."
if fish ${./confirm.fish} "Do you want to choose a different \"${name}\" disk?"; then
${diskSelector}
${deviceSelector}
else
exit 1
fi
@ -191,7 +203,7 @@ let
fi
'';
script = lib.mkDefault ''
partition = lib.mkDefault ''
function partition() {
${if (!config.wipe) then cleanup else ""}
${probeScript}
@ -202,6 +214,7 @@ let
partition
'';
};
};
}
);
@ -232,13 +245,13 @@ let
};
format = mkOption {
type = types.enum (builtins.attrValues fs);
type = types.nullOr (types.enum (builtins.attrValues fs));
description = "The file system format of the partition.";
default =
if (isSwap config) then
fs.swap
else
throw ("Partition format not specified.");
null;
};
size = mkOption {
@ -285,16 +298,10 @@ in
options = {
valhalla = {
fileSystems = {
rootDir = mkOption {
type = types.str;
description = "The root of the installation directory to mount disks into.";
default = "/mnt";
};
diskSetup = {
disks = mkOption {
type = types.attrsOf (mkDiskType);
description = "The additional disks to format.";
devices = mkOption {
type = types.attrsOf (mkDeviceType);
description = "The disk devices to format.";
default = { };
};
@ -309,40 +316,11 @@ in
description = "The script for partitioning the disks.";
};
mount = mkOption {
swap = mkOption {
type = types.str;
description = "The script for mounting the partitioned disks.";
description = "The script for enabling swap devices.";
};
};
script = mkOption {
type = types.str;
description = "The script for partitioning the system's disks.";
default =
let
cfg = config.valhalla.fileSystems.diskSetup;
inherit (cfg) scripts;
disks = (builtins.attrValues cfg.disks);
in
''
#!/bin/bash
set -o errexit
${scripts.init}
${lib.strings.concatLines (lib.optionals ((builtins.length disks) > 0) [
''echo "$(tput setaf 3)==== WARNING ====$(tput sgr0)"''
(''echo "Continuing this script will alter the partitions of ''
+ (lib.strings.concatStringsSep ", " (builtins.map (_: "${_.deviceVariable}") (lib.lists.init disks)))
+ (if (builtins.length disks) > 1 then " and " else "") + (lib.lists.last disks).deviceVariable + ''"'')
''
if ! fish ${./confirm.fish} "Are you sure you want to continue?" "n"; then
exit 1
fi
''
])}
${scripts.partition}
${scripts.mount}
'';
};
};
};
};
@ -350,38 +328,36 @@ in
config = {
valhalla = {
fileSystems.diskSetup = {
fileSystems = {
mounts = (lib.attrsets.concatMapAttrs
(
name: device:
lib.attrsets.concatMapAttrs
(
name: partition:
if partition.mountPoint != null then {
${partition.mountPoint} = {
device = "/dev/disk/by-label/${partition.label}";
fsType = partition.format;
options = partition.mountOptions;
};
} else { }
)
device.partitions
)
cfg.diskSetup.devices);
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.disks));
disks = ((builtins.attrValues cfg.diskSetup.devices));
partitions = (builtins.concatMap (_: (builtins.attrValues _.partitions)) disks);
in
{
init = lib.strings.concatLines (builtins.map (_: _.deviceScript) disks);
partition = lib.strings.concatLines (builtins.map (_: _.script) 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))) ++
init = lib.strings.concatLines (builtins.map (_: _.scripts.init) disks);
partition = lib.strings.concatLines (builtins.map (_: _.scripts.partition) disks);
swap = lib.strings.concatLines (
(builtins.map
(
_: ''
@ -389,7 +365,9 @@ in
sudo swapon ${partPath _}
''
)
(builtins.filter (_: _.useSwap) partitions)));
(builtins.filter (_: _.useSwap) partitions))
);
};
};
};
};

View file

@ -1,5 +1,6 @@
{
ext4 = "ext4";
btrfs = "btrfs";
swap = "swap";
ntfs = "ntfs";
fat32 = "fat32";

View file

@ -1,5 +0,0 @@
{ ... }: {
imports = [
./partition/disks.nix
];
}

View file

@ -91,6 +91,7 @@ let
in
{
imports = [
./programs/btrfs.nix
./programs/docker.nix
./programs/git.nix
./programs/nextcloud.nix

View file

@ -0,0 +1,14 @@
{ lib, ... }:
let
inherit (lib) mkEnableOption;
in
{
options = {
valhalla = {
linux.programs.btrfs = {
enable = mkEnableOption "btrfs tools";
pools = mkEnableOption "btrfs pool support in bootloaders";
};
};
};
}

View file

@ -6,7 +6,6 @@ in {
./hardware.nix
./i18n.nix
./os.nix
./partition.nix
./programs.nix
./software.nix
./users.nix

View file

@ -25,7 +25,7 @@
};
};
fileSystems.diskSetup.disks.OS.partitions = {
fileSystems.diskSetup.devices.OS.partitions = {
# Keep Windows' boot partition
Boot.keepExisting = true;

View file

@ -1,11 +1,11 @@
{ lib, config, ... }:
let fs = import ../../../../lib/modules/partition/fs.nix;
let fs = import ../../../../lib/modules/fileSystems/fs.nix;
in {
imports = [ ../defaults.nix ];
config = {
valhalla = {
fileSystems.diskSetup.disks = {
fileSystems.diskSetup.devices = {
OS = {
partitions = {
Boot = {

View file

@ -1,12 +1,15 @@
{ lib, config, ... }:
let fs = import ../../../lib/modules/partition/fs.nix;
let fs = import ../../../lib/modules/fileSystems/fs.nix;
in {
imports = [ ../../users/manuel/config.nix ];
config = {
valhalla = {
fileSystems.diskSetup.disks = {
fileSystems = {
diskSetup.devices = {
OS = {
path = "/dev/sda";
partitions = {
Boot = {
index = 1;
@ -25,8 +28,16 @@ in {
index = 3;
label = lib.mkDefault config.valhalla.boot.label;
type = "linux";
format = fs.ext4;
};
};
};
};
btrfs = {
volumes = {
OS = {
mountPoint = "/";
devices = [ "/dev/sda3" "/dev/sdb" ];
};
};
};

View file

@ -91,6 +91,7 @@ begin
and echo (getOSConfig hostname) | arch-chroot "$mountDir" tee /etc/hostname >/dev/null
and runInOS fish "$tempDir/../Software/btrfs/main.fish"
and arch-chroot "$mountDir" mkinitcpio -P
and runInOS fish "$tempDir/../Software/grub/main.fish"

View file

@ -0,0 +1,19 @@
#!/bin/env fish
begin
set -l dir (status dirname)
source "$dir/../../lib/software.fish"
function installSW
pacinst btrfs-progs
end
function configureSW -V dir
source "$dir/../../../lib/settings.fish"
if isOSEnabled "programs.btrfs.pools"
echo "HOOKS+=(btrfs)" | sudo tee /etc/mkinitcpio.conf.d/btrfs.conf >/dev/null
end
end
runInstaller $argv
end

View file

@ -71,6 +71,7 @@ function deploySoftware -d "Deploys a the specified software action" -a action
end
and source "$dir/../../Common/Software/bash/main.fish" $argv
and source "$dir/../Software/btrfs/main.fish" $argv
and source "$dir/../../Common/Software/nuke-usb/main.fish" $argv
and source "$dir/../Software/sudo/main.fish" $argv
and source "$dir/../Software/aliae/main.fish" $argv

View file

@ -7,7 +7,7 @@ function initBackupConfig -V dir -d "Fetches the configuration by prompting the
if [ -z "$VALHALLA_BACKUP_DIR" ]
argparse -i "action=" -- $argv
set -l index
set -l confirm fish "$dir/../../lib/modules/partition/confirm.fish"
set -l confirm fish "$dir/../../lib/modules/fileSystems/confirm.fish"
switch "$_flag_action"
case backup

View file

@ -4,7 +4,7 @@ function isConfigured -S
end
function selectProfile -S -a result
source "$(status dirname)/../../lib/modules/partition/select.fish"
source "$(status dirname)/../../lib/modules/fileSystems/select.fish"
set -l file (mktemp)
set -l header NAME
set -l profiles

View file

@ -1,6 +1,6 @@
#!/bin/env fish
function chooseDisk -a result message
set -l file (mktemp)
fish "$(status dirname)/../../lib/modules/partition/choose-disk.fish" "$file" "$message"
fish "$(status dirname)/../../lib/modules/fileSystems/choose-disk.fish" "$file" "$message"
set -g "$result" (cat "$file")
end

View file

@ -1,4 +1,4 @@
#!/bin/env fish
function confirm -a message default
fish "$(status dirname)/../../lib/modules/partition/confirm.fish" $argv
fish "$(status dirname)/../../lib/modules/fileSystems/confirm.fish" $argv
end