lib: better formatting of generated shell output

This commit is contained in:
lassulus 2023-07-02 00:17:48 +02:00 committed by mergify[bot]
parent 5333c4034a
commit c9c2fa9d3a
14 changed files with 72 additions and 60 deletions

View file

@ -5,35 +5,29 @@
type = "disk";
device = builtins.elemAt disks 0;
content = {
type = "table";
format = "gpt";
partitions = [
{
name = "ESP";
type = "gpt";
partitions = {
ESP = {
start = "1MiB";
end = "128MiB";
fs-type = "fat32";
bootable = true;
content = {
type = "filesystem";
format = "vfat";
mountpoint = "/boot";
};
}
];
};
};
};
};
disk1 = {
type = "disk";
device = builtins.elemAt disks 1;
content = {
type = "table";
format = "gpt";
partitions = [
{
type = "gpt";
partitions = {
luks = {
start = "1M";
end = "100%";
name = "luks";
size = "100%";
content = {
type = "luks";
name = "crypted1";
@ -46,21 +40,19 @@
vg = "pool";
};
};
}
];
};
};
};
};
disk2 = {
type = "disk";
device = builtins.elemAt disks 2;
content = {
type = "table";
format = "gpt";
partitions = [
{
type = "gpt";
partitions = {
luks = {
start = "1M";
end = "100%";
name = "luks";
size = "100%";
content = {
type = "luks";
name = "crypted2";
@ -73,8 +65,8 @@
vg = "pool";
};
};
}
];
};
};
};
};
};
@ -83,20 +75,18 @@
type = "mdadm";
level = 1;
content = {
type = "table";
format = "gpt";
partitions = [
{
name = "bla";
type = "gpt";
partitions = {
bla = {
start = "1MiB";
end = "100%";
size = "100%";
content = {
type = "filesystem";
format = "ext4";
mountpoint = "/ext4_mdadm_lvm";
};
}
];
};
};
};
};
};

View file

@ -98,6 +98,17 @@ let
in
iter 1 list;
/* indent takes a multiline string and indents it by 2 spaces starting on the second line
indent :: str -> str
Example:
indent "test\nbla"
=> "test\n bla"
*/
indent = replaceStrings ["\n"] ["\n "];
/* A nix option type representing a json datastructure, vendored from nixpkgs to avoid dependency on pkgs */
jsonType =
let
@ -203,10 +214,10 @@ let
readOnly = true;
type = lib.types.str;
default = ''
( # ${config.type} ${concatMapStringsSep " " (n: toString (config.${n} or "")) ["name" "device" "format" "mountpoint"]}
${diskoLib.defineHookVariables { inherit config options; }}
( # ${config.type} ${concatMapStringsSep " " (n: toString (config.${n} or "")) ["name" "device" "format" "mountpoint"]} #
${diskoLib.indent (diskoLib.defineHookVariables { inherit config options; })}
${config.preCreateHook}
${attrs.default}
${diskoLib.indent attrs.default}
${config.postCreateHook}
)
'';
@ -228,7 +239,10 @@ let
*/
writeCheckedBash = { pkgs, checked ? false, noDeps ? false }: pkgs.writers.makeScriptWriter {
interpreter = if noDeps then "/usr/bin/env bash" else "${pkgs.bash}/bin/bash";
check = lib.optionalString checked "${pkgs.shellcheck}/bin/shellcheck -e SC2034";
check = lib.optionalString checked (pkgs.writeScript "check" ''
set -efu
${pkgs.shellcheck}/bin/shellcheck -e SC2034 "$1"
'');
};

View file

@ -53,7 +53,7 @@
inherit config options;
default = lib.optionalAttrs (config.mountpoint != null) {
fs.${config.mountpoint} = ''
if ! findmnt ${config.device} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then
if ! findmnt ${config.device} "${rootMountPoint}${config.mountpoint}" >/dev/null 2>&1; then
mount ${config.device} "${rootMountPoint}${config.mountpoint}" \
-t "${config.format}" \
${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \

View file

@ -104,7 +104,8 @@ in
--typecode=${toString partition._index}:${partition.type} \
${config.device}
# ensure /dev/disk/by-path/..-partN exists before continuing
udevadm trigger --subsystem-match=block; udevadm settle
udevadm trigger --subsystem-match=block
udevadm settle
${lib.optionalString (partition.content != null) partition.content._create}
'') sortedPartitions)}

View file

@ -55,7 +55,9 @@
inherit config options;
default = ''
cryptsetup -q luksFormat ${config.device} ${diskoLib.maybeStr config.keyFile} ${toString config.extraFormatArgs}
cryptsetup luksOpen ${config.device} ${config.name} ${toString config.extraOpenArgs} ${lib.optionalString (config.keyFile != null) "--key-file ${config.keyFile}"}
cryptsetup luksOpen ${config.device} ${config.name} \
${toString config.extraOpenArgs} \
${lib.optionalString (config.keyFile != null) "--key-file ${config.keyFile}"}
${lib.optionalString (config.content != null) config.content._create}
'';
};

View file

@ -32,7 +32,7 @@
inherit config options;
default = ''
pvcreate ${config.device}
echo "${config.device}" >> "$disko_devices_dir"/lvm_${config.vg}
echo "${config.device}" >>"$disko_devices_dir"/lvm_${config.vg}
'';
};
_mount = diskoLib.mkMountOption {

View file

@ -42,7 +42,8 @@
--force \
--homehost=any \
"''${disk_devices[@]}"
udevadm trigger --subsystem-match=block; udevadm settle
udevadm trigger --subsystem-match=block
udevadm settle
${lib.optionalString (config.content != null) config.content._create}
'';
};

View file

@ -32,7 +32,7 @@
_create = diskoLib.mkCreateOption {
inherit config options;
default = ''
echo "${config.device}" >> "$disko_devices_dir"/raid_${config.name}
echo "${config.device}" >>"$disko_devices_dir"/raid_${config.name}
'';
};
_mount = diskoLib.mkMountOption {

View file

@ -91,7 +91,8 @@
parted -s ${config.device} -- mkpart ${partition.part-type} ${diskoLib.maybeStr partition.fs-type} ${diskoLib.maybeStr partition.fs-type} ${partition.start} ${partition.end}
''}
# ensure /dev/disk/by-path/..-partN exists before continuing
udevadm trigger --subsystem-match=block; udevadm settle
udevadm trigger --subsystem-match=block
udevadm settle
${lib.optionalString partition.bootable ''
parted -s ${config.device} -- set ${toString partition._index} boot on
''}
@ -99,7 +100,8 @@
parted -s ${config.device} -- set ${toString partition._index} ${flag} on
'') partition.flags}
# ensure further operations can detect new partitions
udevadm trigger --subsystem-match=block; udevadm settle
udevadm trigger --subsystem-match=block
udevadm settle
${lib.optionalString (partition.content != null) partition.content._create}
'') config.partitions)}
'';

View file

@ -31,7 +31,7 @@
_create = diskoLib.mkCreateOption {
inherit config options;
default = ''
echo "${config.device}" >> "$disko_devices_dir"/zfs_${config.pool}
echo "${config.device}" >>"$disko_devices_dir"/zfs_${config.pool}
'';
};
_mount = diskoLib.mkMountOption {

View file

@ -56,7 +56,7 @@
default =
lib.optionalAttrs (config.options.mountpoint or "" != "none") {
fs.${config.mountpoint} = ''
if ! findmnt ${config._parent.name}/${config.name} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then
if ! findmnt ${config._parent.name}/${config.name} "${rootMountPoint}${config.mountpoint}" >/dev/null 2>&1; then
mount ${config._parent.name}/${config.name} "${rootMountPoint}${config.mountpoint}" \
-o X-mount.mkdir \
${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \

View file

@ -48,8 +48,10 @@
inherit config options;
default = ''
zfs create ${config._parent.name}/${config.name} \
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} -V ${config.size}
udevadm trigger --subsystem-match=block; udevadm settle
${lib.concatStringsSep " " (lib.mapAttrsToList (n: v: "-o ${n}=${v}") config.options)} \
-V ${config.size}
udevadm trigger --subsystem-match=block
udevadm settle
${lib.optionalString (config.content != null) config.content._create}
'';
};

View file

@ -83,13 +83,13 @@
in
{
dev = ''
zpool list '${config.name}' >/dev/null 2>/dev/null || \
zpool list '${config.name}' >/dev/null 2>/dev/null ||
zpool import -l -R ${config.mountRoot} '${config.name}'
${lib.concatMapStrings (x: x.dev or "") (lib.attrValues datasetMounts)}
'';
fs = (datasetMounts.fs or { }) // lib.optionalAttrs (config.mountpoint != null) {
${config.mountpoint} = ''
if ! findmnt ${config.name} "${rootMountPoint}${config.mountpoint}" > /dev/null 2>&1; then
if ! findmnt ${config.name} "${rootMountPoint}${config.mountpoint}" >/dev/null 2>&1; then
mount ${config.name} "${rootMountPoint}${config.mountpoint}" \
${lib.optionalString ((config.options.mountpoint or "") != "legacy") "-o zfsutil"} \
${lib.concatMapStringsSep " " (opt: "-o ${opt}") config.mountOptions} \